Skip to content

Commit ccd8ebb

Browse files
committed
always execute import-once in the same way. Fixes #1898
1 parent beb5273 commit ccd8ebb

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

lib/less/import-visitor.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
(function (tree) {
2-
tree.importVisitor = function(importer, finish, evalEnv) {
2+
tree.importVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
33
this._visitor = new tree.visitor(this);
44
this._importer = importer;
55
this._finish = finish;
66
this.env = evalEnv || new tree.evalEnv();
77
this.importCount = 0;
8+
this.onceFileDetectionMap = onceFileDetectionMap || {};
9+
this.recursionDetector = {};
10+
if (recursionDetector) {
11+
for(var fullFilename in recursionDetector) {
12+
if (recursionDetector.hasOwnProperty(fullFilename)) {
13+
this.recursionDetector[fullFilename] = true;
14+
}
15+
}
16+
}
817
};
918

1019
tree.importVisitor.prototype = {
@@ -51,10 +60,22 @@
5160
env.importMultiple = true;
5261
}
5362

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

57-
if (imported && !env.importMultiple) { importNode.skip = imported; }
66+
if (!env.importMultiple) {
67+
if (importedAtRoot) {
68+
importNode.skip = true;
69+
} else {
70+
importNode.skip = function() {
71+
if (fullPath in importVisitor.onceFileDetectionMap) {
72+
return true;
73+
}
74+
importVisitor.onceFileDetectionMap[fullPath] = true;
75+
return false;
76+
};
77+
}
78+
}
5879

5980
var subFinish = function(e) {
6081
importVisitor.importCount--;
@@ -67,8 +88,11 @@
6788
if (root) {
6889
importNode.root = root;
6990
importNode.importedFilename = fullPath;
70-
if (!inlineCSS && !importNode.skip) {
71-
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
91+
var duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;
92+
93+
if (!inlineCSS && (env.importMultiple || !duplicateImport)) {
94+
importVisitor.recursionDetector[fullPath] = true;
95+
new(tree.importVisitor)(importVisitor._importer, subFinish, env, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector)
7296
.run(root);
7397
return;
7498
}

lib/less/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ less.Parser = function Parser(env) {
7373
var fileParsedFunc = function (e, root, fullPath) {
7474
parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
7575

76-
var importedPreviously = fullPath in parserImports.files || fullPath === rootFilename;
76+
var importedPreviously = fullPath === rootFilename;
7777

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

lib/less/tree/import.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ tree.Import.prototype = {
9292
eval: function (env) {
9393
var ruleset, features = this.features && this.features.eval(env);
9494

95-
if (this.skip) { return []; }
95+
if (this.skip) {
96+
if (typeof this.skip === "function") {
97+
this.skip = this.skip();
98+
}
99+
if (this.skip) {
100+
return [];
101+
}
102+
}
96103

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

0 commit comments

Comments
 (0)