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