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