UNPKG

6.77 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = loader;
7var _postcss = _interopRequireDefault(require("postcss"));
8var _package = _interopRequireDefault(require("postcss/package.json"));
9var _semver = require("semver");
10var _options = _interopRequireDefault(require("./options.json"));
11var _plugins = require("./plugins");
12var _utils = require("./utils");
13function _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
19async 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") {
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
83 const icssPluginImports = [];
84 const icssPluginApi = [];
85 const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
86 if (needToUseIcssPlugin) {
87 plugins.push((0, _plugins.icssParser)({
88 loaderContext: this,
89 imports: icssPluginImports,
90 api: icssPluginApi,
91 replacements,
92 exports,
93 urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
94 }));
95 }
96
97 // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
98 if (meta) {
99 const {
100 ast
101 } = meta;
102 if (ast && ast.type === "postcss" && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
103 // eslint-disable-next-line no-param-reassign
104 content = ast.root;
105 }
106 }
107 const {
108 resourcePath
109 } = this;
110 let result;
111 try {
112 result = await (0, _postcss.default)(plugins).process(content, {
113 hideNothingWarning: true,
114 from: resourcePath,
115 to: resourcePath,
116 map: options.sourceMap ? {
117 prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
118 inline: false,
119 annotation: false
120 } : false
121 });
122 } catch (error) {
123 if (error.file) {
124 this.addDependency(error.file);
125 }
126 callback(error.name === "CssSyntaxError" ? (0, _utils.syntaxErrorFactory)(error) : error);
127 return;
128 }
129 for (const warning of result.warnings()) {
130 this.emitWarning((0, _utils.warningFactory)(warning));
131 }
132 const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
133 const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
134 if (options.modules.exportOnlyLocals !== true) {
135 imports.unshift({
136 type: "api_import",
137 importName: "___CSS_LOADER_API_IMPORT___",
138 url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
139 });
140 if (options.sourceMap) {
141 imports.unshift({
142 importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
143 url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
144 });
145 } else {
146 imports.unshift({
147 importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
148 url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
149 });
150 }
151 }
152 let isTemplateLiteralSupported = false;
153 if (
154 // eslint-disable-next-line no-underscore-dangle
155 this._compilation &&
156 // eslint-disable-next-line no-underscore-dangle
157 this._compilation.options &&
158 // eslint-disable-next-line no-underscore-dangle
159 this._compilation.options.output &&
160 // eslint-disable-next-line no-underscore-dangle
161 this._compilation.options.output.environment &&
162 // eslint-disable-next-line no-underscore-dangle
163 this._compilation.options.output.environment.templateLiteral) {
164 isTemplateLiteralSupported = true;
165 }
166 const importCode = (0, _utils.getImportCode)(imports, options);
167 let moduleCode;
168 try {
169 moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, isTemplateLiteralSupported, this);
170 } catch (error) {
171 callback(error);
172 return;
173 }
174 const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options, isTemplateLiteralSupported);
175 callback(null, `${importCode}${moduleCode}${exportCode}`);
176}
\No newline at end of file