Skip to content

Commit

Permalink
Merge pull request #3168 from matthew-dean/master
Browse files Browse the repository at this point in the history
Fixes #3116 - lessc not loading plugins in 3.0
  • Loading branch information
matthew-dean authored Feb 15, 2018
2 parents ba5ad9c + 4272871 commit 2f1386f
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 72 deletions.
44 changes: 24 additions & 20 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (grunt) {


grunt.option('stack', true)

// Report the elapsed execution time of tasks.
require('time-grunt')(grunt);

Expand Down Expand Up @@ -67,19 +67,19 @@ module.exports = function (grunt) {
platform: 'Linux'
}
];

var sauceJobs = {};

var browserTests = [ "filemanager-plugin",
"visitor-plugin",
"global-vars",
"modify-vars",
"production",
"global-vars",
"modify-vars",
"production",
"rootpath-relative",
"rootpath",
"relative-urls",
"browser",
"no-js-errors",
"rootpath",
"relative-urls",
"browser",
"no-js-errors",
"legacy"
];

Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = function (grunt) {
// test. Passing undefined does not alter the test result. Please note that this
// only affects the grunt task's result. You have to explicitly update the Sauce
// Labs job's status via its REST API, if you want so.

// This should be the encrypted value in Travis
var user = process.env.SAUCE_USERNAME;
var pass = process.env.SAUCE_ACCESS_KEY;
Expand All @@ -142,7 +142,7 @@ module.exports = function (grunt) {
}
});
});

}
}
};
Expand Down Expand Up @@ -174,7 +174,7 @@ module.exports = function (grunt) {

shell: {
options: {
stdout: true,
stdout: true,
failOnError: true,
execOptions: {
maxBuffer: Infinity
Expand All @@ -186,6 +186,9 @@ module.exports = function (grunt) {
benchmark: {
command: 'node benchmark/index.js'
},
plugin: {
command: 'node bin/lessc --clean-css="--s1 --advanced" test/less/lazy-eval.less tmp/lazy-eval.css'
},
"sourcemap-test": {
command: [
'node bin/lessc --source-map=test/sourcemaps/maps/import-map.map test/less/import.less test/sourcemaps/import.css',
Expand Down Expand Up @@ -258,10 +261,10 @@ module.exports = function (grunt) {
},

eslint: {
target: ["Gruntfile.js",
"test/**/*.js",
"lib/less*/**/*.js",
"bin/lessc",
target: ["Gruntfile.js",
"test/**/*.js",
"lib/less*/**/*.js",
"bin/lessc",
"!test/browser/jasmine-jsreporter.js",
"!test/less/errors/plugin/plugin-error.js"
],
Expand Down Expand Up @@ -289,11 +292,11 @@ module.exports = function (grunt) {
// src is used to build list of less files to compile
src: [
'test/less/*.less',
// Don't test NPM import, obviously
// Don't test NPM import, obviously
'!test/less/plugin-module.less',
'!test/less/import-module.less',
'!test/less/javascript.less',
'!test/less/urls.less',
'!test/less/javascript.less',
'!test/less/urls.less',
'!test/less/empty.less'
],
options: {
Expand Down Expand Up @@ -422,7 +425,7 @@ module.exports = function (grunt) {
specs: 'test/browser/runner-filemanagerPlugin.js',
outfile: 'tmp/browser/test-runner-filemanager-plugin.html'
}
}
}
},

'saucelabs-jasmine': sauceJobs,
Expand Down Expand Up @@ -519,6 +522,7 @@ module.exports = function (grunt) {
'clean',
'eslint',
'shell:test',
'shell:plugin',
'browsertest'
];

Expand Down
24 changes: 14 additions & 10 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path'),
fs = require('../lib/less-node/fs'),
os = require("os"),
utils = require('../lib/less/utils'),
errno,
mkdirp;

Expand All @@ -13,7 +14,8 @@ try {
}

var less = require('../lib/less-node'),
pluginLoader = new less.PluginLoader(less),
pluginManager = new less.PluginManager(less),
fileManager = new less.FileManager(),
plugins = [],
queuePlugins = [],
args = process.argv.slice(1),
Expand All @@ -22,6 +24,7 @@ var less = require('../lib/less-node'),
options = less.options;

options.plugins = plugins;
options.reUsePluginManager = true;

var sourceMapOptions = {};
var continueProcessing = true;
Expand Down Expand Up @@ -75,7 +78,7 @@ var sourceMapFileInline = false;

function printUsage() {
less.lesscHelper.printUsage();
pluginLoader.printUsage(plugins);
pluginManager.Loader.printUsage(plugins);
continueProcessing = false;
}
function render() {
Expand Down Expand Up @@ -137,6 +140,7 @@ function render() {
sourceMapOptions.sourceMapRootpath = path.relative(pathToMap, pathToInput);
}


if (!input) {
console.error("lessc: no input files");
console.error("");
Expand Down Expand Up @@ -328,18 +332,18 @@ function processPluginQueue() {
}
}
queuePlugins.forEach(function(queue) {
pluginLoader.tryLoadPlugin(queue.name, function(err, data) {
if (err) {
pluginError(queue.name);
}
else {
var context = utils.clone(options);
pluginManager.Loader.loadPlugin(queue.name, process.cwd(), context, less.environment, fileManager)
.then(function(data) {
pluginFinished({
fileContent: data.contents,
filename: data.filename,
options: queue.options
});
}
});
})
.catch(function() {
pluginError(queue.name);
});
});
}

Expand Down Expand Up @@ -414,7 +418,7 @@ function processPluginQueue() {
options.javascriptEnabled = true;
break;
case 'no-js':
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' +
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' +
'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
break;
case 'include-path':
Expand Down
35 changes: 20 additions & 15 deletions dist/less.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Less - Leaner CSS v3.0.0
* Less - Leaner CSS v3.0.1
* http://lesscss.org
*
* Copyright (c) 2009-2018, Alexis Sellier <[email protected]>
Expand Down Expand Up @@ -895,7 +895,7 @@ PluginLoader.prototype.loadPlugin = function(filename, basePath, context, enviro
return new Promise(function(fulfill, reject) {
fileManager.loadFile(filename, basePath, context, environment)
.then(fulfill).catch(reject);
});
});
};

module.exports = PluginLoader;
Expand Down Expand Up @@ -1473,7 +1473,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
fileInfo: fileInfo
};
registry = functionRegistry.create();

var registerPlugin = function(obj) {
pluginObj = obj;
};
Expand All @@ -1484,7 +1484,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
} catch (e) {
return new this.less.LessError(e, imports, filename);
}

if (!pluginObj) {
pluginObj = localModule.exports;
}
Expand All @@ -1509,7 +1509,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
e.message = 'Error during @plugin call';
return new this.less.LessError(e, imports, filename);
}

}
else {
return new this.less.LessError({ message: "Not a valid plugin" });
Expand Down Expand Up @@ -1586,6 +1586,11 @@ module.exports = AbstractPluginLoader;


},{"../functions/function-registry":26,"../less-error":36}],19:[function(require,module,exports){
/**
* @todo Document why this abstraction exists, and the relationship between
* environment, file managers, and plugin manager
*/

var logger = require("../logger");
var environment = function(externalEnvironment, fileManagers) {
this.fileManagers = fileManagers || [];
Expand Down Expand Up @@ -2838,7 +2843,7 @@ module.exports = function(environment, fileManagers) {
var SourceMapOutput, SourceMapBuilder, ParseTree, ImportManager, Environment;

var initial = {
version: [3, 0, 0],
version: [3, 0, 1],
data: require('./data'),
tree: require('./tree'),
Environment: (Environment = require("./environment/environment")),
Expand Down Expand Up @@ -2911,24 +2916,24 @@ var utils = require('./utils');
* @prop {string[]} extract
*
* @param {Object} e - An error object to wrap around or just a descriptive object
* @param {Object} importManager - An instance of ImportManager (see import-manager.js)
* @param {Object} fileContentMap - An object with file contents in 'contents' property (like importManager) @todo - move to fileManager?
* @param {string} [currentFilename]
*/
var LessError = module.exports = function LessError(e, importManager, currentFilename) {
var LessError = module.exports = function LessError(e, fileContentMap, currentFilename) {
Error.call(this);

var filename = e.filename || currentFilename;

this.message = e.message;
this.stack = e.stack;

if (importManager && filename) {
var input = importManager.contents[filename],
if (fileContentMap && filename) {
var input = fileContentMap.contents[filename],
loc = utils.getLocation(e.index, input),
line = loc.line,
col = loc.column,
callLine = e.call && utils.getLocation(e.call, input).line,
lines = input.split('\n');
lines = input ? input.split('\n') : '';

this.type = e.type || 'Syntax';
this.filename = filename;
Expand All @@ -2951,7 +2956,7 @@ var LessError = module.exports = function LessError(e, importManager, currentFil

this.callLine = callLine + 1;
this.callExtract = lines[callLine];

this.extract = [
lines[this.line - 2],
lines[this.line - 1],
Expand Down Expand Up @@ -3167,7 +3172,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
} else {
var context,
rootFileInfo,
pluginManager = new PluginManager(this, true);
pluginManager = new PluginManager(this, !options.reUsePluginManager);

options.pluginManager = pluginManager;

Expand Down Expand Up @@ -3213,7 +3218,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
}
});
}

new Parser(context, imports, rootFileInfo)
.parse(input, function (e, root) {
if (e) { return callback(e); }
Expand Down Expand Up @@ -5755,7 +5760,7 @@ PluginManager.prototype.getFileManagers = function() {
return this.fileManagers;
};

//
//
module.exports = PluginManagerFactory;

},{}],44:[function(require,module,exports){
Expand Down
6 changes: 3 additions & 3 deletions dist/less.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/less-browser/plugin-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PluginLoader.prototype.loadPlugin = function(filename, basePath, context, enviro
return new Promise(function(fulfill, reject) {
fileManager.loadFile(filename, basePath, context, environment)
.then(fulfill).catch(reject);
});
});
};

module.exports = PluginLoader;
Expand Down
16 changes: 8 additions & 8 deletions lib/less-node/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var path = require('path'),
AbstractFileManager = require("../less/environment/abstract-file-manager.js");

var FileManager = function() {
this.files = {};
this.contents = {};
};

FileManager.prototype = new AbstractFileManager();
Expand Down Expand Up @@ -34,7 +34,7 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e

// Search node_modules
if (!explicit) { paths.push.apply(paths, this.modulePaths); }

if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }

var prefixes = options.prefixes || [''];
Expand Down Expand Up @@ -82,11 +82,11 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
}
catch (e) {}
}

fullFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;

if (self.files[fullFilename]) {
fulfill({ contents: self.files[fullFilename], filename: fullFilename});
if (self.contents[fullFilename]) {
fulfill({ contents: self.contents[fullFilename], filename: fullFilename});
}
else {
var readFileArgs = [fullFilename];
Expand All @@ -96,7 +96,7 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
if (options.syncImport) {
try {
var data = fs.readFileSync.apply(this, readFileArgs);
self.files[fullFilename] = data;
self.contents[fullFilename] = data;
fulfill({ contents: data, filename: fullFilename});
}
catch (e) {
Expand All @@ -106,11 +106,11 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
}
else {
readFileArgs.push(function(e, data) {
if (e) {
if (e) {
filenamesTried.push(fullFilename);
return tryPrefix(j + 1);
}
self.files[fullFilename] = data;
self.contents[fullFilename] = data;
fulfill({ contents: data, filename: fullFilename});
});
fs.readFile.apply(this, readFileArgs);
Expand Down
Loading

0 comments on commit 2f1386f

Please sign in to comment.