index.js 5.6 KB

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