1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var contexts_1 = tslib_1.__importDefault(require("../contexts"));
|
5 | var visitor_1 = tslib_1.__importDefault(require("./visitor"));
|
6 | var import_sequencer_1 = tslib_1.__importDefault(require("./import-sequencer"));
|
7 | var utils = tslib_1.__importStar(require("../utils"));
|
8 | var ImportVisitor = function (importer, finish) {
|
9 | this._visitor = new visitor_1.default(this);
|
10 | this._importer = importer;
|
11 | this._finish = finish;
|
12 | this.context = new contexts_1.default.Eval();
|
13 | this.importCount = 0;
|
14 | this.onceFileDetectionMap = {};
|
15 | this.recursionDetector = {};
|
16 | this._sequencer = new import_sequencer_1.default(this._onSequencerEmpty.bind(this));
|
17 | };
|
18 | ImportVisitor.prototype = {
|
19 | isReplacing: false,
|
20 | run: function (root) {
|
21 | try {
|
22 |
|
23 | this._visitor.visit(root);
|
24 | }
|
25 | catch (e) {
|
26 | this.error = e;
|
27 | }
|
28 | this.isFinished = true;
|
29 | this._sequencer.tryRun();
|
30 | },
|
31 | _onSequencerEmpty: function () {
|
32 | if (!this.isFinished) {
|
33 | return;
|
34 | }
|
35 | this._finish(this.error);
|
36 | },
|
37 | visitImport: function (importNode, visitArgs) {
|
38 | var inlineCSS = importNode.options.inline;
|
39 | if (!importNode.css || inlineCSS) {
|
40 | var context = new contexts_1.default.Eval(this.context, utils.copyArray(this.context.frames));
|
41 | var importParent = context.frames[0];
|
42 | this.importCount++;
|
43 | if (importNode.isVariableImport()) {
|
44 | this._sequencer.addVariableImport(this.processImportNode.bind(this, importNode, context, importParent));
|
45 | }
|
46 | else {
|
47 | this.processImportNode(importNode, context, importParent);
|
48 | }
|
49 | }
|
50 | visitArgs.visitDeeper = false;
|
51 | },
|
52 | processImportNode: function (importNode, context, importParent) {
|
53 | var evaldImportNode;
|
54 | var inlineCSS = importNode.options.inline;
|
55 | try {
|
56 | evaldImportNode = importNode.evalForImport(context);
|
57 | }
|
58 | catch (e) {
|
59 | if (!e.filename) {
|
60 | e.index = importNode.getIndex();
|
61 | e.filename = importNode.fileInfo().filename;
|
62 | }
|
63 |
|
64 | importNode.css = true;
|
65 |
|
66 | importNode.error = e;
|
67 | }
|
68 | if (evaldImportNode && (!evaldImportNode.css || inlineCSS)) {
|
69 | if (evaldImportNode.options.multiple) {
|
70 | context.importMultiple = true;
|
71 | }
|
72 |
|
73 | var tryAppendLessExtension = evaldImportNode.css === undefined;
|
74 | for (var i = 0; i < importParent.rules.length; i++) {
|
75 | if (importParent.rules[i] === importNode) {
|
76 | importParent.rules[i] = evaldImportNode;
|
77 | break;
|
78 | }
|
79 | }
|
80 | var onImported = this.onImported.bind(this, evaldImportNode, context), sequencedOnImported = this._sequencer.addImport(onImported);
|
81 | this._importer.push(evaldImportNode.getPath(), tryAppendLessExtension, evaldImportNode.fileInfo(), evaldImportNode.options, sequencedOnImported);
|
82 | }
|
83 | else {
|
84 | this.importCount--;
|
85 | if (this.isFinished) {
|
86 | this._sequencer.tryRun();
|
87 | }
|
88 | }
|
89 | },
|
90 | onImported: function (importNode, context, e, root, importedAtRoot, fullPath) {
|
91 | if (e) {
|
92 | if (!e.filename) {
|
93 | e.index = importNode.getIndex();
|
94 | e.filename = importNode.fileInfo().filename;
|
95 | }
|
96 | this.error = e;
|
97 | }
|
98 | var importVisitor = this, inlineCSS = importNode.options.inline, isPlugin = importNode.options.isPlugin, isOptional = importNode.options.optional, duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;
|
99 | if (!context.importMultiple) {
|
100 | if (duplicateImport) {
|
101 | importNode.skip = true;
|
102 | }
|
103 | else {
|
104 | importNode.skip = function () {
|
105 | if (fullPath in importVisitor.onceFileDetectionMap) {
|
106 | return true;
|
107 | }
|
108 | importVisitor.onceFileDetectionMap[fullPath] = true;
|
109 | return false;
|
110 | };
|
111 | }
|
112 | }
|
113 | if (!fullPath && isOptional) {
|
114 | importNode.skip = true;
|
115 | }
|
116 | if (root) {
|
117 | importNode.root = root;
|
118 | importNode.importedFilename = fullPath;
|
119 | if (!inlineCSS && !isPlugin && (context.importMultiple || !duplicateImport)) {
|
120 | importVisitor.recursionDetector[fullPath] = true;
|
121 | var oldContext = this.context;
|
122 | this.context = context;
|
123 | try {
|
124 | this._visitor.visit(root);
|
125 | }
|
126 | catch (e) {
|
127 | this.error = e;
|
128 | }
|
129 | this.context = oldContext;
|
130 | }
|
131 | }
|
132 | importVisitor.importCount--;
|
133 | if (importVisitor.isFinished) {
|
134 | importVisitor._sequencer.tryRun();
|
135 | }
|
136 | },
|
137 | visitDeclaration: function (declNode, visitArgs) {
|
138 | if (declNode.value.type === 'DetachedRuleset') {
|
139 | this.context.frames.unshift(declNode);
|
140 | }
|
141 | else {
|
142 | visitArgs.visitDeeper = false;
|
143 | }
|
144 | },
|
145 | visitDeclarationOut: function (declNode) {
|
146 | if (declNode.value.type === 'DetachedRuleset') {
|
147 | this.context.frames.shift();
|
148 | }
|
149 | },
|
150 | visitAtRule: function (atRuleNode, visitArgs) {
|
151 | this.context.frames.unshift(atRuleNode);
|
152 | },
|
153 | visitAtRuleOut: function (atRuleNode) {
|
154 | this.context.frames.shift();
|
155 | },
|
156 | visitMixinDefinition: function (mixinDefinitionNode, visitArgs) {
|
157 | this.context.frames.unshift(mixinDefinitionNode);
|
158 | },
|
159 | visitMixinDefinitionOut: function (mixinDefinitionNode) {
|
160 | this.context.frames.shift();
|
161 | },
|
162 | visitRuleset: function (rulesetNode, visitArgs) {
|
163 | this.context.frames.unshift(rulesetNode);
|
164 | },
|
165 | visitRulesetOut: function (rulesetNode) {
|
166 | this.context.frames.shift();
|
167 | },
|
168 | visitMedia: function (mediaNode, visitArgs) {
|
169 | this.context.frames.unshift(mediaNode.rules[0]);
|
170 | },
|
171 | visitMediaOut: function (mediaNode) {
|
172 | this.context.frames.shift();
|
173 | }
|
174 | };
|
175 | exports.default = ImportVisitor;
|
176 |
|
\ | No newline at end of file |