|
1 | 1 | (function (tree) {
|
2 |
| - tree.importVisitor = function(importer, finish, evalEnv) { |
| 2 | + tree.importVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) { |
3 | 3 | this._visitor = new tree.visitor(this);
|
4 | 4 | this._importer = importer;
|
5 | 5 | this._finish = finish;
|
6 | 6 | this.env = evalEnv || new tree.evalEnv();
|
7 | 7 | 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 | + } |
8 | 17 | };
|
9 | 18 |
|
10 | 19 | tree.importVisitor.prototype = {
|
|
51 | 60 | env.importMultiple = true;
|
52 | 61 | }
|
53 | 62 |
|
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) { |
55 | 64 | if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
|
56 | 65 |
|
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 | + } |
58 | 79 |
|
59 | 80 | var subFinish = function(e) {
|
60 | 81 | importVisitor.importCount--;
|
|
67 | 88 | if (root) {
|
68 | 89 | importNode.root = root;
|
69 | 90 | 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) |
72 | 96 | .run(root);
|
73 | 97 | return;
|
74 | 98 | }
|
|
0 commit comments