Skip to content

Commit

Permalink
always execute import-once in the same way. Fixes #1898
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Feb 27, 2014
1 parent beb5273 commit ccd8ebb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
34 changes: 29 additions & 5 deletions lib/less/import-visitor.js
@@ -1,10 +1,19 @@
(function (tree) {
tree.importVisitor = function(importer, finish, evalEnv) {
tree.importVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
this._visitor = new tree.visitor(this);
this._importer = importer;
this._finish = finish;
this.env = evalEnv || new tree.evalEnv();
this.importCount = 0;
this.onceFileDetectionMap = onceFileDetectionMap || {};
this.recursionDetector = {};
if (recursionDetector) {
for(var fullFilename in recursionDetector) {
if (recursionDetector.hasOwnProperty(fullFilename)) {
this.recursionDetector[fullFilename] = true;
}
}
}
};

tree.importVisitor.prototype = {
Expand Down Expand Up @@ -51,10 +60,22 @@
env.importMultiple = true;
}

this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, imported, fullPath) {
this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, importedAtRoot, fullPath) {
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }

if (imported && !env.importMultiple) { importNode.skip = imported; }
if (!env.importMultiple) {
if (importedAtRoot) {
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--;
Expand All @@ -67,8 +88,11 @@
if (root) {
importNode.root = root;
importNode.importedFilename = fullPath;
if (!inlineCSS && !importNode.skip) {
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
var duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;

if (!inlineCSS && (env.importMultiple || !duplicateImport)) {
importVisitor.recursionDetector[fullPath] = true;
new(tree.importVisitor)(importVisitor._importer, subFinish, env, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector)
.run(root);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/less/parser.js
Expand Up @@ -73,7 +73,7 @@ less.Parser = function Parser(env) {
var fileParsedFunc = function (e, root, fullPath) {
parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue

var importedPreviously = fullPath in parserImports.files || fullPath === rootFilename;
var importedPreviously = fullPath === rootFilename;

parserImports.files[fullPath] = root; // Store the root

Expand Down
9 changes: 8 additions & 1 deletion lib/less/tree/import.js
Expand Up @@ -92,7 +92,14 @@ tree.Import.prototype = {
eval: function (env) {
var ruleset, features = this.features && this.features.eval(env);

if (this.skip) { return []; }
if (this.skip) {
if (typeof this.skip === "function") {
this.skip = this.skip();
}
if (this.skip) {
return [];
}
}

if (this.options.inline) {
//todo needs to reference css file not import
Expand Down

0 comments on commit ccd8ebb

Please sign in to comment.