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 parser_1 = tslib_1.__importDefault(require("./parser/parser"));
|
6 | var less_error_1 = tslib_1.__importDefault(require("./less-error"));
|
7 | var utils = tslib_1.__importStar(require("./utils"));
|
8 | var logger_1 = tslib_1.__importDefault(require("./logger"));
|
9 | function default_1(environment) {
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var ImportManager = (function () {
|
19 | function ImportManager(less, context, rootFileInfo) {
|
20 | this.less = less;
|
21 | this.rootFilename = rootFileInfo.filename;
|
22 | this.paths = context.paths || [];
|
23 | this.contents = {};
|
24 | this.contentsIgnoredChars = {};
|
25 | this.mime = context.mime;
|
26 | this.error = null;
|
27 | this.context = context;
|
28 |
|
29 | this.queue = [];
|
30 | this.files = {};
|
31 | }
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | ImportManager.prototype.push = function (path, tryAppendExtension, currentFileInfo, importOptions, callback) {
|
41 | var importManager = this, pluginLoader = this.context.pluginManager.Loader;
|
42 | this.queue.push(path);
|
43 | var fileParsedFunc = function (e, root, fullPath) {
|
44 | importManager.queue.splice(importManager.queue.indexOf(path), 1);
|
45 | var importedEqualsRoot = fullPath === importManager.rootFilename;
|
46 | if (importOptions.optional && e) {
|
47 | callback(null, { rules: [] }, false, null);
|
48 | logger_1.default.info("The file " + fullPath + " was skipped because it was not found and the import was marked optional.");
|
49 | }
|
50 | else {
|
51 |
|
52 |
|
53 |
|
54 | if (!importManager.files[fullPath] && !importOptions.inline) {
|
55 | importManager.files[fullPath] = { root: root, options: importOptions };
|
56 | }
|
57 | if (e && !importManager.error) {
|
58 | importManager.error = e;
|
59 | }
|
60 | callback(e, root, importedEqualsRoot, fullPath);
|
61 | }
|
62 | };
|
63 | var newFileInfo = {
|
64 | rewriteUrls: this.context.rewriteUrls,
|
65 | entryPath: currentFileInfo.entryPath,
|
66 | rootpath: currentFileInfo.rootpath,
|
67 | rootFilename: currentFileInfo.rootFilename
|
68 | };
|
69 | var fileManager = environment.getFileManager(path, currentFileInfo.currentDirectory, this.context, environment);
|
70 | if (!fileManager) {
|
71 | fileParsedFunc({ message: "Could not find a file-manager for " + path });
|
72 | return;
|
73 | }
|
74 | var loadFileCallback = function (loadedFile) {
|
75 | var plugin;
|
76 | var resolvedFilename = loadedFile.filename;
|
77 | var contents = loadedFile.contents.replace(/^\uFEFF/, '');
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | newFileInfo.currentDirectory = fileManager.getPath(resolvedFilename);
|
87 | if (newFileInfo.rewriteUrls) {
|
88 | newFileInfo.rootpath = fileManager.join((importManager.context.rootpath || ''), fileManager.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
|
89 | if (!fileManager.isPathAbsolute(newFileInfo.rootpath) && fileManager.alwaysMakePathsAbsolute()) {
|
90 | newFileInfo.rootpath = fileManager.join(newFileInfo.entryPath, newFileInfo.rootpath);
|
91 | }
|
92 | }
|
93 | newFileInfo.filename = resolvedFilename;
|
94 | var newEnv = new contexts_1.default.Parse(importManager.context);
|
95 | newEnv.processImports = false;
|
96 | importManager.contents[resolvedFilename] = contents;
|
97 | if (currentFileInfo.reference || importOptions.reference) {
|
98 | newFileInfo.reference = true;
|
99 | }
|
100 | if (importOptions.isPlugin) {
|
101 | plugin = pluginLoader.evalPlugin(contents, newEnv, importManager, importOptions.pluginArgs, newFileInfo);
|
102 | if (plugin instanceof less_error_1.default) {
|
103 | fileParsedFunc(plugin, null, resolvedFilename);
|
104 | }
|
105 | else {
|
106 | fileParsedFunc(null, plugin, resolvedFilename);
|
107 | }
|
108 | }
|
109 | else if (importOptions.inline) {
|
110 | fileParsedFunc(null, contents, resolvedFilename);
|
111 | }
|
112 | else {
|
113 |
|
114 |
|
115 | if (importManager.files[resolvedFilename]
|
116 | && !importManager.files[resolvedFilename].options.multiple
|
117 | && !importOptions.multiple) {
|
118 | fileParsedFunc(null, importManager.files[resolvedFilename].root, resolvedFilename);
|
119 | }
|
120 | else {
|
121 | new parser_1.default(newEnv, importManager, newFileInfo).parse(contents, function (e, root) {
|
122 | fileParsedFunc(e, root, resolvedFilename);
|
123 | });
|
124 | }
|
125 | }
|
126 | };
|
127 | var loadedFile;
|
128 | var promise;
|
129 | var context = utils.clone(this.context);
|
130 | if (tryAppendExtension) {
|
131 | context.ext = importOptions.isPlugin ? '.js' : '.less';
|
132 | }
|
133 | if (importOptions.isPlugin) {
|
134 | context.mime = 'application/javascript';
|
135 | if (context.syncImport) {
|
136 | loadedFile = pluginLoader.loadPluginSync(path, currentFileInfo.currentDirectory, context, environment, fileManager);
|
137 | }
|
138 | else {
|
139 | promise = pluginLoader.loadPlugin(path, currentFileInfo.currentDirectory, context, environment, fileManager);
|
140 | }
|
141 | }
|
142 | else {
|
143 | if (context.syncImport) {
|
144 | loadedFile = fileManager.loadFileSync(path, currentFileInfo.currentDirectory, context, environment);
|
145 | }
|
146 | else {
|
147 | promise = fileManager.loadFile(path, currentFileInfo.currentDirectory, context, environment, function (err, loadedFile) {
|
148 | if (err) {
|
149 | fileParsedFunc(err);
|
150 | }
|
151 | else {
|
152 | loadFileCallback(loadedFile);
|
153 | }
|
154 | });
|
155 | }
|
156 | }
|
157 | if (loadedFile) {
|
158 | if (!loadedFile.filename) {
|
159 | fileParsedFunc(loadedFile);
|
160 | }
|
161 | else {
|
162 | loadFileCallback(loadedFile);
|
163 | }
|
164 | }
|
165 | else if (promise) {
|
166 | promise.then(loadFileCallback, fileParsedFunc);
|
167 | }
|
168 | };
|
169 | return ImportManager;
|
170 | }());
|
171 | return ImportManager;
|
172 | }
|
173 | exports.default = default_1;
|
174 | ;
|
175 |
|
\ | No newline at end of file |