Skip to content

Commit 4272871

Browse files
author
Matthew Dean
committedFeb 15, 2018
Fixes #3116 - lessc not loading plugins in 3.0
1 parent ba5ad9c commit 4272871

14 files changed

+91
-72
lines changed
 

‎Gruntfile.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function (grunt) {
44

55

66
grunt.option('stack', true)
7-
7+
88
// Report the elapsed execution time of tasks.
99
require('time-grunt')(grunt);
1010

@@ -67,19 +67,19 @@ module.exports = function (grunt) {
6767
platform: 'Linux'
6868
}
6969
];
70-
70+
7171
var sauceJobs = {};
7272

7373
var browserTests = [ "filemanager-plugin",
7474
"visitor-plugin",
75-
"global-vars",
76-
"modify-vars",
77-
"production",
75+
"global-vars",
76+
"modify-vars",
77+
"production",
7878
"rootpath-relative",
79-
"rootpath",
80-
"relative-urls",
81-
"browser",
82-
"no-js-errors",
79+
"rootpath",
80+
"relative-urls",
81+
"browser",
82+
"no-js-errors",
8383
"legacy"
8484
];
8585

@@ -116,7 +116,7 @@ module.exports = function (grunt) {
116116
// test. Passing undefined does not alter the test result. Please note that this
117117
// only affects the grunt task's result. You have to explicitly update the Sauce
118118
// Labs job's status via its REST API, if you want so.
119-
119+
120120
// This should be the encrypted value in Travis
121121
var user = process.env.SAUCE_USERNAME;
122122
var pass = process.env.SAUCE_ACCESS_KEY;
@@ -142,7 +142,7 @@ module.exports = function (grunt) {
142142
}
143143
});
144144
});
145-
145+
146146
}
147147
}
148148
};
@@ -174,7 +174,7 @@ module.exports = function (grunt) {
174174

175175
shell: {
176176
options: {
177-
stdout: true,
177+
stdout: true,
178178
failOnError: true,
179179
execOptions: {
180180
maxBuffer: Infinity
@@ -186,6 +186,9 @@ module.exports = function (grunt) {
186186
benchmark: {
187187
command: 'node benchmark/index.js'
188188
},
189+
plugin: {
190+
command: 'node bin/lessc --clean-css="--s1 --advanced" test/less/lazy-eval.less tmp/lazy-eval.css'
191+
},
189192
"sourcemap-test": {
190193
command: [
191194
'node bin/lessc --source-map=test/sourcemaps/maps/import-map.map test/less/import.less test/sourcemaps/import.css',
@@ -258,10 +261,10 @@ module.exports = function (grunt) {
258261
},
259262

260263
eslint: {
261-
target: ["Gruntfile.js",
262-
"test/**/*.js",
263-
"lib/less*/**/*.js",
264-
"bin/lessc",
264+
target: ["Gruntfile.js",
265+
"test/**/*.js",
266+
"lib/less*/**/*.js",
267+
"bin/lessc",
265268
"!test/browser/jasmine-jsreporter.js",
266269
"!test/less/errors/plugin/plugin-error.js"
267270
],
@@ -289,11 +292,11 @@ module.exports = function (grunt) {
289292
// src is used to build list of less files to compile
290293
src: [
291294
'test/less/*.less',
292-
// Don't test NPM import, obviously
295+
// Don't test NPM import, obviously
293296
'!test/less/plugin-module.less',
294297
'!test/less/import-module.less',
295-
'!test/less/javascript.less',
296-
'!test/less/urls.less',
298+
'!test/less/javascript.less',
299+
'!test/less/urls.less',
297300
'!test/less/empty.less'
298301
],
299302
options: {
@@ -422,7 +425,7 @@ module.exports = function (grunt) {
422425
specs: 'test/browser/runner-filemanagerPlugin.js',
423426
outfile: 'tmp/browser/test-runner-filemanager-plugin.html'
424427
}
425-
}
428+
}
426429
},
427430

428431
'saucelabs-jasmine': sauceJobs,
@@ -519,6 +522,7 @@ module.exports = function (grunt) {
519522
'clean',
520523
'eslint',
521524
'shell:test',
525+
'shell:plugin',
522526
'browsertest'
523527
];
524528

‎bin/lessc

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var path = require('path'),
44
fs = require('../lib/less-node/fs'),
55
os = require("os"),
6+
utils = require('../lib/less/utils'),
67
errno,
78
mkdirp;
89

@@ -13,7 +14,8 @@ try {
1314
}
1415

1516
var less = require('../lib/less-node'),
16-
pluginLoader = new less.PluginLoader(less),
17+
pluginManager = new less.PluginManager(less),
18+
fileManager = new less.FileManager(),
1719
plugins = [],
1820
queuePlugins = [],
1921
args = process.argv.slice(1),
@@ -22,6 +24,7 @@ var less = require('../lib/less-node'),
2224
options = less.options;
2325

2426
options.plugins = plugins;
27+
options.reUsePluginManager = true;
2528

2629
var sourceMapOptions = {};
2730
var continueProcessing = true;
@@ -75,7 +78,7 @@ var sourceMapFileInline = false;
7578

7679
function printUsage() {
7780
less.lesscHelper.printUsage();
78-
pluginLoader.printUsage(plugins);
81+
pluginManager.Loader.printUsage(plugins);
7982
continueProcessing = false;
8083
}
8184
function render() {
@@ -137,6 +140,7 @@ function render() {
137140
sourceMapOptions.sourceMapRootpath = path.relative(pathToMap, pathToInput);
138141
}
139142

143+
140144
if (!input) {
141145
console.error("lessc: no input files");
142146
console.error("");
@@ -328,18 +332,18 @@ function processPluginQueue() {
328332
}
329333
}
330334
queuePlugins.forEach(function(queue) {
331-
pluginLoader.tryLoadPlugin(queue.name, function(err, data) {
332-
if (err) {
333-
pluginError(queue.name);
334-
}
335-
else {
335+
var context = utils.clone(options);
336+
pluginManager.Loader.loadPlugin(queue.name, process.cwd(), context, less.environment, fileManager)
337+
.then(function(data) {
336338
pluginFinished({
337339
fileContent: data.contents,
338340
filename: data.filename,
339341
options: queue.options
340342
});
341-
}
342-
});
343+
})
344+
.catch(function() {
345+
pluginError(queue.name);
346+
});
343347
});
344348
}
345349

@@ -414,7 +418,7 @@ function processPluginQueue() {
414418
options.javascriptEnabled = true;
415419
break;
416420
case 'no-js':
417-
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' +
421+
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' +
418422
'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
419423
break;
420424
case 'include-path':

‎dist/less.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Less - Leaner CSS v3.0.0
2+
* Less - Leaner CSS v3.0.1
33
* http://lesscss.org
44
*
55
* Copyright (c) 2009-2018, Alexis Sellier <self@cloudhead.net>
@@ -895,7 +895,7 @@ PluginLoader.prototype.loadPlugin = function(filename, basePath, context, enviro
895895
return new Promise(function(fulfill, reject) {
896896
fileManager.loadFile(filename, basePath, context, environment)
897897
.then(fulfill).catch(reject);
898-
});
898+
});
899899
};
900900

901901
module.exports = PluginLoader;
@@ -1473,7 +1473,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
14731473
fileInfo: fileInfo
14741474
};
14751475
registry = functionRegistry.create();
1476-
1476+
14771477
var registerPlugin = function(obj) {
14781478
pluginObj = obj;
14791479
};
@@ -1484,7 +1484,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
14841484
} catch (e) {
14851485
return new this.less.LessError(e, imports, filename);
14861486
}
1487-
1487+
14881488
if (!pluginObj) {
14891489
pluginObj = localModule.exports;
14901490
}
@@ -1509,7 +1509,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
15091509
e.message = 'Error during @plugin call';
15101510
return new this.less.LessError(e, imports, filename);
15111511
}
1512-
1512+
15131513
}
15141514
else {
15151515
return new this.less.LessError({ message: "Not a valid plugin" });
@@ -1586,6 +1586,11 @@ module.exports = AbstractPluginLoader;
15861586

15871587

15881588
},{"../functions/function-registry":26,"../less-error":36}],19:[function(require,module,exports){
1589+
/**
1590+
* @todo Document why this abstraction exists, and the relationship between
1591+
* environment, file managers, and plugin manager
1592+
*/
1593+
15891594
var logger = require("../logger");
15901595
var environment = function(externalEnvironment, fileManagers) {
15911596
this.fileManagers = fileManagers || [];
@@ -2838,7 +2843,7 @@ module.exports = function(environment, fileManagers) {
28382843
var SourceMapOutput, SourceMapBuilder, ParseTree, ImportManager, Environment;
28392844

28402845
var initial = {
2841-
version: [3, 0, 0],
2846+
version: [3, 0, 1],
28422847
data: require('./data'),
28432848
tree: require('./tree'),
28442849
Environment: (Environment = require("./environment/environment")),
@@ -2911,24 +2916,24 @@ var utils = require('./utils');
29112916
* @prop {string[]} extract
29122917
*
29132918
* @param {Object} e - An error object to wrap around or just a descriptive object
2914-
* @param {Object} importManager - An instance of ImportManager (see import-manager.js)
2919+
* @param {Object} fileContentMap - An object with file contents in 'contents' property (like importManager) @todo - move to fileManager?
29152920
* @param {string} [currentFilename]
29162921
*/
2917-
var LessError = module.exports = function LessError(e, importManager, currentFilename) {
2922+
var LessError = module.exports = function LessError(e, fileContentMap, currentFilename) {
29182923
Error.call(this);
29192924

29202925
var filename = e.filename || currentFilename;
29212926

29222927
this.message = e.message;
29232928
this.stack = e.stack;
29242929

2925-
if (importManager && filename) {
2926-
var input = importManager.contents[filename],
2930+
if (fileContentMap && filename) {
2931+
var input = fileContentMap.contents[filename],
29272932
loc = utils.getLocation(e.index, input),
29282933
line = loc.line,
29292934
col = loc.column,
29302935
callLine = e.call && utils.getLocation(e.call, input).line,
2931-
lines = input.split('\n');
2936+
lines = input ? input.split('\n') : '';
29322937

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

29522957
this.callLine = callLine + 1;
29532958
this.callExtract = lines[callLine];
2954-
2959+
29552960
this.extract = [
29562961
lines[this.line - 2],
29572962
lines[this.line - 1],
@@ -3167,7 +3172,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
31673172
} else {
31683173
var context,
31693174
rootFileInfo,
3170-
pluginManager = new PluginManager(this, true);
3175+
pluginManager = new PluginManager(this, !options.reUsePluginManager);
31713176

31723177
options.pluginManager = pluginManager;
31733178

@@ -3213,7 +3218,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
32133218
}
32143219
});
32153220
}
3216-
3221+
32173222
new Parser(context, imports, rootFileInfo)
32183223
.parse(input, function (e, root) {
32193224
if (e) { return callback(e); }
@@ -5755,7 +5760,7 @@ PluginManager.prototype.getFileManagers = function() {
57555760
return this.fileManagers;
57565761
};
57575762

5758-
//
5763+
//
57595764
module.exports = PluginManagerFactory;
57605765

57615766
},{}],44:[function(require,module,exports){

‎dist/less.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/less-browser/plugin-loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PluginLoader.prototype.loadPlugin = function(filename, basePath, context, enviro
1818
return new Promise(function(fulfill, reject) {
1919
fileManager.loadFile(filename, basePath, context, environment)
2020
.then(fulfill).catch(reject);
21-
});
21+
});
2222
};
2323

2424
module.exports = PluginLoader;

‎lib/less-node/file-manager.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var path = require('path'),
44
AbstractFileManager = require("../less/environment/abstract-file-manager.js");
55

66
var FileManager = function() {
7-
this.files = {};
7+
this.contents = {};
88
};
99

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

3535
// Search node_modules
3636
if (!explicit) { paths.push.apply(paths, this.modulePaths); }
37-
37+
3838
if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }
3939

4040
var prefixes = options.prefixes || [''];
@@ -82,11 +82,11 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
8282
}
8383
catch (e) {}
8484
}
85-
85+
8686
fullFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;
8787

88-
if (self.files[fullFilename]) {
89-
fulfill({ contents: self.files[fullFilename], filename: fullFilename});
88+
if (self.contents[fullFilename]) {
89+
fulfill({ contents: self.contents[fullFilename], filename: fullFilename});
9090
}
9191
else {
9292
var readFileArgs = [fullFilename];
@@ -96,7 +96,7 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
9696
if (options.syncImport) {
9797
try {
9898
var data = fs.readFileSync.apply(this, readFileArgs);
99-
self.files[fullFilename] = data;
99+
self.contents[fullFilename] = data;
100100
fulfill({ contents: data, filename: fullFilename});
101101
}
102102
catch (e) {
@@ -106,11 +106,11 @@ FileManager.prototype.loadFile = function(filename, currentDirectory, options, e
106106
}
107107
else {
108108
readFileArgs.push(function(e, data) {
109-
if (e) {
109+
if (e) {
110110
filenamesTried.push(fullFilename);
111111
return tryPrefix(j + 1);
112112
}
113-
self.files[fullFilename] = data;
113+
self.contents[fullFilename] = data;
114114
fulfill({ contents: data, filename: fullFilename});
115115
});
116116
fs.readFile.apply(this, readFileArgs);

‎lib/less-node/plugin-loader.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ PluginLoader.prototype.loadPlugin = function(filename, basePath, context, enviro
4040
fulfill(data);
4141
}
4242
catch (e) {
43+
console.log(e);
4344
reject(e);
4445
}
4546
}
4647
).catch(function(err) {
4748
reject(err);
4849
});
4950
});
50-
51+
5152
};
5253

5354
module.exports = PluginLoader;

‎lib/less/environment/abstract-plugin-loader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
5656
fileInfo: fileInfo
5757
};
5858
registry = functionRegistry.create();
59-
59+
6060
var registerPlugin = function(obj) {
6161
pluginObj = obj;
6262
};
@@ -67,7 +67,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
6767
} catch (e) {
6868
return new this.less.LessError(e, imports, filename);
6969
}
70-
70+
7171
if (!pluginObj) {
7272
pluginObj = localModule.exports;
7373
}
@@ -92,7 +92,7 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, imports,
9292
e.message = 'Error during @plugin call';
9393
return new this.less.LessError(e, imports, filename);
9494
}
95-
95+
9696
}
9797
else {
9898
return new this.less.LessError({ message: "Not a valid plugin" });

‎lib/less/environment/environment.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @todo Document why this abstraction exists, and the relationship between
3+
* environment, file managers, and plugin manager
4+
*/
5+
16
var logger = require("../logger");
27
var environment = function(externalEnvironment, fileManagers) {
38
this.fileManagers = fileManagers || [];

‎lib/less/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function(environment, fileManagers) {
22
var SourceMapOutput, SourceMapBuilder, ParseTree, ImportManager, Environment;
33

44
var initial = {
5-
version: [3, 0, 0],
5+
version: [3, 0, 1],
66
data: require('./data'),
77
tree: require('./tree'),
88
Environment: (Environment = require("./environment/environment")),

‎lib/less/less-error.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ var utils = require('./utils');
1818
* @prop {string[]} extract
1919
*
2020
* @param {Object} e - An error object to wrap around or just a descriptive object
21-
* @param {Object} importManager - An instance of ImportManager (see import-manager.js)
21+
* @param {Object} fileContentMap - An object with file contents in 'contents' property (like importManager) @todo - move to fileManager?
2222
* @param {string} [currentFilename]
2323
*/
24-
var LessError = module.exports = function LessError(e, importManager, currentFilename) {
24+
var LessError = module.exports = function LessError(e, fileContentMap, currentFilename) {
2525
Error.call(this);
2626

2727
var filename = e.filename || currentFilename;
2828

2929
this.message = e.message;
3030
this.stack = e.stack;
3131

32-
if (importManager && filename) {
33-
var input = importManager.contents[filename],
32+
if (fileContentMap && filename) {
33+
var input = fileContentMap.contents[filename],
3434
loc = utils.getLocation(e.index, input),
3535
line = loc.line,
3636
col = loc.column,
3737
callLine = e.call && utils.getLocation(e.call, input).line,
38-
lines = input.split('\n');
38+
lines = input ? input.split('\n') : '';
3939

4040
this.type = e.type || 'Syntax';
4141
this.filename = filename;
@@ -58,7 +58,7 @@ var LessError = module.exports = function LessError(e, importManager, currentFil
5858

5959
this.callLine = callLine + 1;
6060
this.callExtract = lines[callLine];
61-
61+
6262
this.extract = [
6363
lines[this.line - 2],
6464
lines[this.line - 1],

‎lib/less/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
3333
} else {
3434
var context,
3535
rootFileInfo,
36-
pluginManager = new PluginManager(this, true);
36+
pluginManager = new PluginManager(this, !options.reUsePluginManager);
3737

3838
options.pluginManager = pluginManager;
3939

@@ -79,7 +79,7 @@ module.exports = function(environment, ParseTree, ImportManager) {
7979
}
8080
});
8181
}
82-
82+
8383
new Parser(context, imports, rootFileInfo)
8484
.parse(input, function (e, root) {
8585
if (e) { return callback(e); }

‎lib/less/plugin-manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ PluginManager.prototype.getFileManagers = function() {
151151
return this.fileManagers;
152152
};
153153

154-
//
154+
//
155155
module.exports = PluginManagerFactory;

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "less",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Leaner CSS",
55
"homepage": "http://lesscss.org",
66
"author": {

0 commit comments

Comments
 (0)
Please sign in to comment.