From e793b17751a7217654cd3abe980bf267bb550613 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Thu, 23 Oct 2014 18:15:25 +0100 Subject: [PATCH] Sequencer working so imports are always processed in the same order --- lib/less/visitors/import-sequencer.js | 4 +- lib/less/visitors/import-visitor.js | 105 ++++++++++++++------------ 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/lib/less/visitors/import-sequencer.js b/lib/less/visitors/import-sequencer.js index e87f26d6..6fb46399 100644 --- a/lib/less/visitors/import-sequencer.js +++ b/lib/less/visitors/import-sequencer.js @@ -12,7 +12,7 @@ ImportSequencer.prototype.addImport = function(callback) { }; this.imports.push(importItem); return function() { - importItem.args = [].concat(arguments); + importItem.args = Array.prototype.slice.call(arguments, 0); importItem.isReady = true; importSequencer.tryRun(); }; @@ -32,4 +32,4 @@ ImportSequencer.prototype.tryRun = function() { } }; -module.exports = ImportSequencer; \ No newline at end of file +module.exports = ImportSequencer; diff --git a/lib/less/visitors/import-visitor.js b/lib/less/visitors/import-visitor.js index 2da7c55d..d719cb2c 100644 --- a/lib/less/visitors/import-visitor.js +++ b/lib/less/visitors/import-visitor.js @@ -17,31 +17,28 @@ var ImportVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, re } } } - this._sequencer = new ImportSequencer(this._onSequencerEmpty.bind(this)); + this._sequencer = new ImportSequencer(); }; ImportVisitor.prototype = { isReplacing: true, - _onSequencerEmpty: function() { - if (this.isFinished) { - this._finish(this.error); - } - }, run: function (root) { + var error; try { // process the contents this._visitor.visit(root); } catch(e) { - this.error = e; + error = e; } this.isFinished = true; - this._sequencer.tryRun(); + if (this.importCount === 0) { + this._finish(error); + } }, visitImport: function (importNode, visitArgs) { - var importVisitor = this, - evaldImportNode, + var evaldImportNode, inlineCSS = importNode.options.inline; if (!importNode.css || inlineCSS) { @@ -67,53 +64,61 @@ ImportVisitor.prototype = { // try appending if we haven't determined if it is css or not var tryAppendLessExtension = importNode.css === undefined; - this._importer.push(importNode.getPath(), tryAppendLessExtension, importNode.currentFileInfo, importNode.options, function (e, root, importedAtRoot, fullPath) { - if (e && !e.filename) { - e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; - } - var duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector; - if (!context.importMultiple) { - if (duplicateImport) { - importNode.skip = true; - } else { - importNode.skip = function() { - if (fullPath in importVisitor.onceFileDetectionMap) { - return true; - } - importVisitor.onceFileDetectionMap[fullPath] = true; - return false; - }; - } - } + var onImported = this.onImported.bind(this, importNode, context), + sequencedOnImported = this._sequencer.addImport(onImported); - var subFinish = function(e) { - importVisitor.importCount--; - - if (importVisitor.importCount === 0 && importVisitor.isFinished) { - importVisitor._finish(e); - } - }; - - if (root) { - importNode.root = root; - importNode.importedFilename = fullPath; - - if (!inlineCSS && (context.importMultiple || !duplicateImport)) { - importVisitor.recursionDetector[fullPath] = true; - new ImportVisitor(importVisitor._importer, subFinish, context, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector) - .run(root); - return; - } - } - - subFinish(); - }); + this._importer.push(importNode.getPath(), tryAppendLessExtension, importNode.currentFileInfo, importNode.options, sequencedOnImported); } } visitArgs.visitDeeper = false; return importNode; }, + onImported: function (importNode, context, e, root, importedAtRoot, fullPath) { + if (e && !e.filename) { + e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; + } + + var importVisitor = this, + inlineCSS = importNode.options.inline, + duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector; + + if (!context.importMultiple) { + if (duplicateImport) { + importNode.skip = true; + } else { + importNode.skip = function() { + if (fullPath in importVisitor.onceFileDetectionMap) { + return true; + } + importVisitor.onceFileDetectionMap[fullPath] = true; + return false; + }; + } + } + + var subFinish = function(e) { + importVisitor.importCount--; + + if (importVisitor.importCount === 0 && importVisitor.isFinished) { + importVisitor._finish(e); + } + }; + + if (root) { + importNode.root = root; + importNode.importedFilename = fullPath; + + if (!inlineCSS && (context.importMultiple || !duplicateImport)) { + importVisitor.recursionDetector[fullPath] = true; + new ImportVisitor(importVisitor._importer, subFinish, context, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector) + .run(root); + return; + } + } + + subFinish(); + }, visitRule: function (ruleNode, visitArgs) { visitArgs.visitDeeper = false; return ruleNode;