From 83db6e8d9039e2aed05972bd777ad0c84bfbef8d Mon Sep 17 00:00:00 2001 From: rjgotten Date: Wed, 25 Feb 2015 17:36:00 +0100 Subject: [PATCH] Allow adding plugins via @import (plugin) --- lib/less/import-manager.js | 16 ++++++++++++++-- lib/less/parser/parser.js | 2 +- lib/less/tree/import.js | 6 ++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/less/import-manager.js b/lib/less/import-manager.js index 22784d77..eac052e1 100644 --- a/lib/less/import-manager.js +++ b/lib/less/import-manager.js @@ -64,7 +64,7 @@ module.exports = function(environment) { return; } - if (tryAppendLessExtension) { + if (tryAppendLessExtension && !importOptions.plugin) { path = fileManager.tryAppendLessExtension(path); } @@ -101,7 +101,19 @@ module.exports = function(environment) { newFileInfo.reference = true; } - if (importOptions.inline) { + if (importOptions.plugin) { + try { + var plugin = require(resolvedFilename); + + if ( -1 === newEnv.pluginManager.installedPlugins.indexOf( plugin )) { + newEnv.pluginManager.addPlugin(plugin); + } + fileParsedFunc(null, "", resolvedFilename); + } catch(e) { + fileParsedFunc(e, "", resolvedFilename); + } + + } else if (importOptions.inline) { fileParsedFunc(null, contents, resolvedFilename); } else { new Parser(newEnv, importManager, newFileInfo).parse(contents, function (e, root) { diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js index df309789..a6c22433 100644 --- a/lib/less/parser/parser.js +++ b/lib/less/parser/parser.js @@ -1212,7 +1212,7 @@ var Parser = function Parser(context, imports, fileInfo) { }, importOption: function() { - var opt = parserInput.$re(/^(less|css|multiple|once|inline|reference|optional)/); + var opt = parserInput.$re(/^(less|css|multiple|once|inline|reference|optional|plugin)/); if (opt) { return opt[1]; } diff --git a/lib/less/tree/import.js b/lib/less/tree/import.js index e88e6dce..1bacbee2 100644 --- a/lib/less/tree/import.js +++ b/lib/less/tree/import.js @@ -50,7 +50,7 @@ Import.prototype.accept = function (visitor) { this.features = visitor.visit(this.features); } this.path = visitor.visit(this.path); - if (!this.options.inline && this.root) { + if (!this.options.plugin && !this.options.inline && this.root) { this.root = visitor.visit(this.root); } }; @@ -120,7 +120,9 @@ Import.prototype.eval = function (context) { } } - if (this.options.inline) { + if (this.options.plugin) { + return []; + } else if (this.options.inline) { var contents = new Anonymous(this.root, 0, {filename: this.importedFilename}, true, true); return this.features ? new Media([contents], this.features.value) : [contents]; } else if (this.css) {