Sequencer working so imports are always processed in the same order

This commit is contained in:
Luke Page
2014-10-23 18:15:25 +01:00
parent 8c89b268b3
commit e793b17751
2 changed files with 57 additions and 52 deletions

View File

@@ -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;
module.exports = ImportSequencer;

View File

@@ -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;