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 |
|
28 |
|
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 | let isSupportAbsoluteURL = false;
|
53 |
|
54 | if (this._compilation && this._compilation.options && this._compilation.options.experiments && this._compilation.options.experiments.buildHttp) {
|
55 | isSupportAbsoluteURL = true;
|
56 | }
|
57 |
|
58 | const isSupportDataURL = options.esModule && Boolean("fsStartTime" in this._compiler);
|
59 |
|
60 | if ((0, _utils.shouldUseImportPlugin)(options)) {
|
61 | plugins.push((0, _plugins.importParser)({
|
62 | isSupportAbsoluteURL: false,
|
63 | isSupportDataURL: false,
|
64 | isCSSStyleSheet: options.exportType === "css-style-sheet",
|
65 | loaderContext: this,
|
66 | imports: importPluginImports,
|
67 | api: importPluginApi,
|
68 | filter: options.import.filter,
|
69 | urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
|
70 | }));
|
71 | }
|
72 |
|
73 | const urlPluginImports = [];
|
74 |
|
75 | if ((0, _utils.shouldUseURLPlugin)(options)) {
|
76 | const needToResolveURL = !options.esModule;
|
77 | plugins.push((0, _plugins.urlParser)({
|
78 | isSupportAbsoluteURL,
|
79 | isSupportDataURL,
|
80 | imports: urlPluginImports,
|
81 | replacements,
|
82 | context: this.context,
|
83 | rootContext: this.rootContext,
|
84 | filter: (0, _utils.getFilter)(options.url.filter, this.resourcePath),
|
85 | resolver: needToResolveURL ? this.getResolve({
|
86 | mainFiles: [],
|
87 | extensions: []
|
88 | }) :
|
89 | undefined,
|
90 | urlHandler: url => (0, _utils.stringifyRequest)(this, url)
|
91 |
|
92 | }));
|
93 | }
|
94 |
|
95 | const icssPluginImports = [];
|
96 | const icssPluginApi = [];
|
97 | const needToUseIcssPlugin = (0, _utils.shouldUseIcssPlugin)(options);
|
98 |
|
99 | if (needToUseIcssPlugin) {
|
100 | plugins.push((0, _plugins.icssParser)({
|
101 | loaderContext: this,
|
102 | imports: icssPluginImports,
|
103 | api: icssPluginApi,
|
104 | replacements,
|
105 | exports,
|
106 | urlHandler: url => (0, _utils.stringifyRequest)(this, (0, _utils.combineRequests)((0, _utils.getPreRequester)(this)(options.importLoaders), url))
|
107 | }));
|
108 | }
|
109 |
|
110 |
|
111 | if (meta) {
|
112 | const {
|
113 | ast
|
114 | } = meta;
|
115 |
|
116 | if (ast && ast.type === "postcss" && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
|
117 |
|
118 | content = ast.root;
|
119 | }
|
120 | }
|
121 |
|
122 | const {
|
123 | resourcePath
|
124 | } = this;
|
125 | let result;
|
126 |
|
127 | try {
|
128 | result = await (0, _postcss.default)(plugins).process(content, {
|
129 | hideNothingWarning: true,
|
130 | from: resourcePath,
|
131 | to: resourcePath,
|
132 | map: options.sourceMap ? {
|
133 | prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
|
134 | inline: false,
|
135 | annotation: false
|
136 | } : false
|
137 | });
|
138 | } catch (error) {
|
139 | if (error.file) {
|
140 | this.addDependency(error.file);
|
141 | }
|
142 |
|
143 | callback(error.name === "CssSyntaxError" ? new _CssSyntaxError.default(error) : error);
|
144 | return;
|
145 | }
|
146 |
|
147 | for (const warning of result.warnings()) {
|
148 | this.emitWarning(new _Warning.default(warning));
|
149 | }
|
150 |
|
151 | const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
|
152 | const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
|
153 |
|
154 | if (options.modules.exportOnlyLocals !== true) {
|
155 | imports.unshift({
|
156 | type: "api_import",
|
157 | importName: "___CSS_LOADER_API_IMPORT___",
|
158 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/api"))
|
159 | });
|
160 |
|
161 | if (options.sourceMap) {
|
162 | imports.unshift({
|
163 | importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
|
164 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/sourceMaps"))
|
165 | });
|
166 | } else {
|
167 | imports.unshift({
|
168 | importName: "___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___",
|
169 | url: (0, _utils.stringifyRequest)(this, require.resolve("./runtime/noSourceMaps"))
|
170 | });
|
171 | }
|
172 | }
|
173 |
|
174 | const importCode = (0, _utils.getImportCode)(imports, options);
|
175 | let moduleCode;
|
176 |
|
177 | try {
|
178 | moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
|
179 | } catch (error) {
|
180 | callback(error);
|
181 | return;
|
182 | }
|
183 |
|
184 | const exportCode = (0, _utils.getExportCode)(exports, replacements, needToUseIcssPlugin, options);
|
185 | callback(null, `${importCode}${moduleCode}${exportCode}`);
|
186 | } |
\ | No newline at end of file |