import-manager.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var contexts_1 = __importDefault(require("./contexts"));
  26. var parser_1 = __importDefault(require("./parser/parser"));
  27. var less_error_1 = __importDefault(require("./less-error"));
  28. var utils = __importStar(require("./utils"));
  29. var logger_1 = __importDefault(require("./logger"));
  30. exports.default = (function (environment) {
  31. // FileInfo = {
  32. // 'rewriteUrls' - option - whether to adjust URL's to be relative
  33. // 'filename' - full resolved filename of current file
  34. // 'rootpath' - path to append to normal URLs for this node
  35. // 'currentDirectory' - path to the current file, absolute
  36. // 'rootFilename' - filename of the base file
  37. // 'entryPath' - absolute path to the entry file
  38. // 'reference' - whether the file should not be output and only output parts that are referenced
  39. var ImportManager = /** @class */ (function () {
  40. function ImportManager(less, context, rootFileInfo) {
  41. this.less = less;
  42. this.rootFilename = rootFileInfo.filename;
  43. this.paths = context.paths || []; // Search paths, when importing
  44. this.contents = {}; // map - filename to contents of all the files
  45. this.contentsIgnoredChars = {}; // map - filename to lines at the beginning of each file to ignore
  46. this.mime = context.mime;
  47. this.error = null;
  48. this.context = context;
  49. // Deprecated? Unused outside of here, could be useful.
  50. this.queue = []; // Files which haven't been imported yet
  51. this.files = []; // List of files imported
  52. }
  53. /**
  54. * Add an import to be imported
  55. * @param path - the raw path
  56. * @param tryAppendExtension - whether to try appending a file extension (.less or .js if the path has no extension)
  57. * @param currentFileInfo - the current file info (used for instance to work out relative paths)
  58. * @param importOptions - import options
  59. * @param callback - callback for when it is imported
  60. */
  61. ImportManager.prototype.push = function (path, tryAppendExtension, currentFileInfo, importOptions, callback) {
  62. var importManager = this;
  63. var pluginLoader = this.context.pluginManager.Loader;
  64. this.queue.push(path);
  65. var fileParsedFunc = function (e, root, fullPath) {
  66. importManager.queue.splice(importManager.queue.indexOf(path), 1); // Remove the path from the queue
  67. var importedEqualsRoot = fullPath === importManager.rootFilename;
  68. if (importOptions.optional && e) {
  69. callback(null, { rules: [] }, false, null);
  70. logger_1.default.info("The file " + fullPath + " was skipped because it was not found and the import was marked optional.");
  71. }
  72. else {
  73. var files = importManager.files;
  74. if (files.indexOf(fullPath) === -1) {
  75. files.push(fullPath);
  76. }
  77. if (e && !importManager.error) {
  78. importManager.error = e;
  79. }
  80. callback(e, root, importedEqualsRoot, fullPath);
  81. }
  82. };
  83. var newFileInfo = {
  84. rewriteUrls: this.context.rewriteUrls,
  85. entryPath: currentFileInfo.entryPath,
  86. rootpath: currentFileInfo.rootpath,
  87. rootFilename: currentFileInfo.rootFilename
  88. };
  89. var fileManager = environment.getFileManager(path, currentFileInfo.currentDirectory, this.context, environment);
  90. if (!fileManager) {
  91. fileParsedFunc({ message: "Could not find a file-manager for " + path });
  92. return;
  93. }
  94. var loadFileCallback = function (loadedFile) {
  95. var plugin;
  96. var resolvedFilename = loadedFile.filename;
  97. var contents = loadedFile.contents.replace(/^\uFEFF/, '');
  98. // Pass on an updated rootpath if path of imported file is relative and file
  99. // is in a (sub|sup) directory
  100. //
  101. // Examples:
  102. // - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
  103. // then rootpath should become 'less/module/nav/'
  104. // - If path of imported file is '../mixins.less' and rootpath is 'less/',
  105. // then rootpath should become 'less/../'
  106. newFileInfo.currentDirectory = fileManager.getPath(resolvedFilename);
  107. if (newFileInfo.rewriteUrls) {
  108. newFileInfo.rootpath = fileManager.join((importManager.context.rootpath || ''), fileManager.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
  109. if (!fileManager.isPathAbsolute(newFileInfo.rootpath) && fileManager.alwaysMakePathsAbsolute()) {
  110. newFileInfo.rootpath = fileManager.join(newFileInfo.entryPath, newFileInfo.rootpath);
  111. }
  112. }
  113. newFileInfo.filename = resolvedFilename;
  114. var newEnv = new contexts_1.default.Parse(importManager.context);
  115. newEnv.processImports = false;
  116. importManager.contents[resolvedFilename] = contents;
  117. if (currentFileInfo.reference || importOptions.reference) {
  118. newFileInfo.reference = true;
  119. }
  120. if (importOptions.isPlugin) {
  121. plugin = pluginLoader.evalPlugin(contents, newEnv, importManager, importOptions.pluginArgs, newFileInfo);
  122. if (plugin instanceof less_error_1.default) {
  123. fileParsedFunc(plugin, null, resolvedFilename);
  124. }
  125. else {
  126. fileParsedFunc(null, plugin, resolvedFilename);
  127. }
  128. }
  129. else if (importOptions.inline) {
  130. fileParsedFunc(null, contents, resolvedFilename);
  131. }
  132. else {
  133. new parser_1.default(newEnv, importManager, newFileInfo).parse(contents, function (e, root) {
  134. fileParsedFunc(e, root, resolvedFilename);
  135. });
  136. }
  137. };
  138. var loadedFile;
  139. var promise;
  140. var context = utils.clone(this.context);
  141. if (tryAppendExtension) {
  142. context.ext = importOptions.isPlugin ? '.js' : '.less';
  143. }
  144. if (importOptions.isPlugin) {
  145. context.mime = 'application/javascript';
  146. if (context.syncImport) {
  147. loadedFile = pluginLoader.loadPluginSync(path, currentFileInfo.currentDirectory, context, environment, fileManager);
  148. }
  149. else {
  150. promise = pluginLoader.loadPlugin(path, currentFileInfo.currentDirectory, context, environment, fileManager);
  151. }
  152. }
  153. else {
  154. if (context.syncImport) {
  155. loadedFile = fileManager.loadFileSync(path, currentFileInfo.currentDirectory, context, environment);
  156. }
  157. else {
  158. promise = fileManager.loadFile(path, currentFileInfo.currentDirectory, context, environment, function (err, loadedFile) {
  159. if (err) {
  160. fileParsedFunc(err);
  161. }
  162. else {
  163. loadFileCallback(loadedFile);
  164. }
  165. });
  166. }
  167. }
  168. if (loadedFile) {
  169. if (!loadedFile.filename) {
  170. fileParsedFunc(loadedFile);
  171. }
  172. else {
  173. loadFileCallback(loadedFile);
  174. }
  175. }
  176. else if (promise) {
  177. promise.then(loadFileCallback, fileParsedFunc);
  178. }
  179. };
  180. return ImportManager;
  181. }());
  182. return ImportManager;
  183. });
  184. //# sourceMappingURL=import-manager.js.map