index.js 5.6 KB

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