index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. var _postcss = _interopRequireDefault(require("postcss"));
  7. var _package = _interopRequireDefault(require("postcss/package.json"));
  8. var _semver = require("semver");
  9. var _options = _interopRequireDefault(require("./options.json"));
  10. var _plugins = require("./plugins");
  11. var _utils = require("./utils");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /*
  14. MIT License http://www.opensource.org/licenses/mit-license.php
  15. Author Tobias Koppers @sokra
  16. */
  17. async function loader(content, map, meta) {
  18. const rawOptions = this.getOptions(_options.default);
  19. const callback = this.async();
  20. if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && this._module.type === "css") {
  21. this.emitWarning(new Error('You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `css-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `css-loader` in your webpack config (now css-loader does nothing).'));
  22. callback(null, content, map, meta);
  23. return;
  24. }
  25. let options;
  26. try {
  27. options = (0, _utils.normalizeOptions)(rawOptions, this);
  28. } catch (error) {
  29. callback(error);
  30. return;
  31. }
  32. const plugins = [];
  33. const replacements = [];
  34. const exports = [];
  35. if ((0, _utils.shouldUseModulesPlugins)(options)) {
  36. plugins.push(...(0, _utils.getModulesPlugins)(options, this));
  37. }
  38. const importPluginImports = [];
  39. const importPluginApi = [];
  40. let isSupportAbsoluteURL = false;
  41. // TODO enable by default in the next major release
  42. if (this._compilation && this._compilation.options && this._compilation.options.experiments && this._compilation.options.experiments.buildHttp) {
  43. isSupportAbsoluteURL = true;
  44. }
  45. const isSupportDataURL = options.esModule && Boolean("fsStartTime" in this._compiler);
  46. if ((0, _utils.shouldUseImportPlugin)(options)) {
  47. plugins.push((0, _plugins.importParser)({
  48. isSupportAbsoluteURL: false,
  49. isSupportDataURL: false,
  50. isCSSStyleSheet: options.exportType === "css-style-sheet",
  51. loaderContext: this,
  52. imports: importPluginImports,
  53. api: importPluginApi,
  54. filter: options.import.filter,
  55. urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
  56. }));
  57. }
  58. const urlPluginImports = [];
  59. if ((0, _utils.shouldUseURLPlugin)(options)) {
  60. const needToResolveURL = !options.esModule;
  61. plugins.push((0, _plugins.urlParser)({
  62. isSupportAbsoluteURL,
  63. isSupportDataURL,
  64. imports: urlPluginImports,
  65. replacements,
  66. context: this.context,
  67. rootContext: this.rootContext,
  68. filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
  69. resolver: needToResolveURL ? this.getResolve({
  70. mainFiles: [],
  71. extensions: []
  72. }) :
  73. // eslint-disable-next-line no-undefined
  74. undefined,
  75. urlHandler: url => (0, _utils.stringifyRequest)(this, url)
  76. // Support data urls as input in new URL added in webpack@5.38.0
  77. }));
  78. }
  79. const icssPluginImports = [];
  80. const icssPluginApi = [];
  81. const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
  82. if (needToUseIcssPlugin) {
  83. plugins.push((0, _plugins.icssParser)({
  84. loaderContext: this,
  85. imports: icssPluginImports,
  86. api: icssPluginApi,
  87. replacements,
  88. exports,
  89. urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
  90. }));
  91. }
  92. // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
  93. if (meta) {
  94. const {
  95. ast
  96. } = meta;
  97. if (ast && ast.type === "postcss" && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
  98. // eslint-disable-next-line no-param-reassign
  99. content = ast.root;
  100. }
  101. }
  102. const {
  103. resourcePath
  104. } = this;
  105. let result;
  106. try {
  107. result = await (0, _postcss.default)(plugins).process(content, {
  108. hideNothingWarning: true,
  109. from: resourcePath,
  110. to: resourcePath,
  111. map: options.sourceMap ? {
  112. prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
  113. inline: false,
  114. annotation: false
  115. } : false
  116. });
  117. } catch (error) {
  118. if (error.file) {
  119. this.addDependency(error.file);
  120. }
  121. callback(error.name === "CssSyntaxError" ? (0, _utils.syntaxErrorFactory)(error) : error);
  122. return;
  123. }
  124. for (const warning of result.warnings()) {
  125. this.emitWarning((0, _utils.warningFactory)(warning));
  126. }
  127. const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
  128. const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
  129. if (options.modules.exportOnlyLocals !== true) {
  130. imports.unshift({
  131. type: "api_import",
  132. importName: "___CSS_LOADER_API_IMPORT___",
  133. url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
  134. });
  135. if (options.sourceMap) {
  136. imports.unshift({
  137. importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
  138. url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
  139. });
  140. } else {
  141. imports.unshift({
  142. importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
  143. url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
  144. });
  145. }
  146. }
  147. let isTemplateLiteralSupported = false;
  148. if (
  149. // eslint-disable-next-line no-underscore-dangle
  150. this._compilation &&
  151. // eslint-disable-next-line no-underscore-dangle
  152. this._compilation.options &&
  153. // eslint-disable-next-line no-underscore-dangle
  154. this._compilation.options.output &&
  155. // eslint-disable-next-line no-underscore-dangle
  156. this._compilation.options.output.environment &&
  157. // eslint-disable-next-line no-underscore-dangle
  158. this._compilation.options.output.environment.templateLiteral) {
  159. isTemplateLiteralSupported = true;
  160. }
  161. const importCode = (0, _utils.getImportCode)(imports, options);
  162. let moduleCode;
  163. try {
  164. moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, isTemplateLiteralSupported, this);
  165. } catch (error) {
  166. callback(error);
  167. return;
  168. }
  169. const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options, isTemplateLiteralSupported);
  170. callback(null, `${importCode}${moduleCode}${exportCode}`);
  171. }