import.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var node_1 = tslib_1.__importDefault(require("./node"));
  5. var media_1 = tslib_1.__importDefault(require("./media"));
  6. var url_1 = tslib_1.__importDefault(require("./url"));
  7. var quoted_1 = tslib_1.__importDefault(require("./quoted"));
  8. var ruleset_1 = tslib_1.__importDefault(require("./ruleset"));
  9. var anonymous_1 = tslib_1.__importDefault(require("./anonymous"));
  10. var utils = tslib_1.__importStar(require("../utils"));
  11. var less_error_1 = tslib_1.__importDefault(require("../less-error"));
  12. //
  13. // CSS @import node
  14. //
  15. // The general strategy here is that we don't want to wait
  16. // for the parsing to be completed, before we start importing
  17. // the file. That's because in the context of a browser,
  18. // most of the time will be spent waiting for the server to respond.
  19. //
  20. // On creation, we push the import path to our import queue, though
  21. // `import,push`, we also pass it a callback, which it'll call once
  22. // the file has been fetched, and parsed.
  23. //
  24. var Import = function (path, features, options, index, currentFileInfo, visibilityInfo) {
  25. this.options = options;
  26. this._index = index;
  27. this._fileInfo = currentFileInfo;
  28. this.path = path;
  29. this.features = features;
  30. this.allowRoot = true;
  31. if (this.options.less !== undefined || this.options.inline) {
  32. this.css = !this.options.less || this.options.inline;
  33. }
  34. else {
  35. var pathValue = this.getPath();
  36. if (pathValue && /[#.&?]css([?;].*)?$/.test(pathValue)) {
  37. this.css = true;
  38. }
  39. }
  40. this.copyVisibilityInfo(visibilityInfo);
  41. this.setParent(this.features, this);
  42. this.setParent(this.path, this);
  43. };
  44. Import.prototype = Object.assign(new node_1.default(), {
  45. type: 'Import',
  46. accept: function (visitor) {
  47. if (this.features) {
  48. this.features = visitor.visit(this.features);
  49. }
  50. this.path = visitor.visit(this.path);
  51. if (!this.options.isPlugin && !this.options.inline && this.root) {
  52. this.root = visitor.visit(this.root);
  53. }
  54. },
  55. genCSS: function (context, output) {
  56. if (this.css && this.path._fileInfo.reference === undefined) {
  57. output.add('@import ', this._fileInfo, this._index);
  58. this.path.genCSS(context, output);
  59. if (this.features) {
  60. output.add(' ');
  61. this.features.genCSS(context, output);
  62. }
  63. output.add(';');
  64. }
  65. },
  66. getPath: function () {
  67. return (this.path instanceof url_1.default) ?
  68. this.path.value.value : this.path.value;
  69. },
  70. isVariableImport: function () {
  71. var path = this.path;
  72. if (path instanceof url_1.default) {
  73. path = path.value;
  74. }
  75. if (path instanceof quoted_1.default) {
  76. return path.containsVariables();
  77. }
  78. return true;
  79. },
  80. evalForImport: function (context) {
  81. var path = this.path;
  82. if (path instanceof url_1.default) {
  83. path = path.value;
  84. }
  85. return new Import(path.eval(context), this.features, this.options, this._index, this._fileInfo, this.visibilityInfo());
  86. },
  87. evalPath: function (context) {
  88. var path = this.path.eval(context);
  89. var fileInfo = this._fileInfo;
  90. if (!(path instanceof url_1.default)) {
  91. // Add the rootpath if the URL requires a rewrite
  92. var pathValue = path.value;
  93. if (fileInfo &&
  94. pathValue &&
  95. context.pathRequiresRewrite(pathValue)) {
  96. path.value = context.rewritePath(pathValue, fileInfo.rootpath);
  97. }
  98. else {
  99. path.value = context.normalizePath(path.value);
  100. }
  101. }
  102. return path;
  103. },
  104. eval: function (context) {
  105. var result = this.doEval(context);
  106. if (this.options.reference || this.blocksVisibility()) {
  107. if (result.length || result.length === 0) {
  108. result.forEach(function (node) {
  109. node.addVisibilityBlock();
  110. });
  111. }
  112. else {
  113. result.addVisibilityBlock();
  114. }
  115. }
  116. return result;
  117. },
  118. doEval: function (context) {
  119. var ruleset;
  120. var registry;
  121. var features = this.features && this.features.eval(context);
  122. if (this.options.isPlugin) {
  123. if (this.root && this.root.eval) {
  124. try {
  125. this.root.eval(context);
  126. }
  127. catch (e) {
  128. e.message = 'Plugin error during evaluation';
  129. throw new less_error_1.default(e, this.root.imports, this.root.filename);
  130. }
  131. }
  132. registry = context.frames[0] && context.frames[0].functionRegistry;
  133. if (registry && this.root && this.root.functions) {
  134. registry.addMultiple(this.root.functions);
  135. }
  136. return [];
  137. }
  138. if (this.skip) {
  139. if (typeof this.skip === 'function') {
  140. this.skip = this.skip();
  141. }
  142. if (this.skip) {
  143. return [];
  144. }
  145. }
  146. if (this.options.inline) {
  147. var contents = new anonymous_1.default(this.root, 0, {
  148. filename: this.importedFilename,
  149. reference: this.path._fileInfo && this.path._fileInfo.reference
  150. }, true, true);
  151. return this.features ? new media_1.default([contents], this.features.value) : [contents];
  152. }
  153. else if (this.css) {
  154. var newImport = new Import(this.evalPath(context), features, this.options, this._index);
  155. if (!newImport.css && this.error) {
  156. throw this.error;
  157. }
  158. return newImport;
  159. }
  160. else if (this.root) {
  161. ruleset = new ruleset_1.default(null, utils.copyArray(this.root.rules));
  162. ruleset.evalImports(context);
  163. return this.features ? new media_1.default(ruleset.rules, this.features.value) : ruleset.rules;
  164. }
  165. else {
  166. return [];
  167. }
  168. }
  169. });
  170. exports.default = Import;
  171. //# sourceMappingURL=import.js.map