1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.loadConfig = loadConfig;
|
---|
7 | exports.getPostcssOptions = getPostcssOptions;
|
---|
8 | exports.exec = exec;
|
---|
9 | exports.normalizeSourceMap = normalizeSourceMap;
|
---|
10 | exports.normalizeSourceMapAfterPostcss = normalizeSourceMapAfterPostcss;
|
---|
11 | exports.findPackageJSONDir = findPackageJSONDir;
|
---|
12 | exports.getPostcssImplementation = getPostcssImplementation;
|
---|
13 |
|
---|
14 | var _path = _interopRequireDefault(require("path"));
|
---|
15 |
|
---|
16 | var _module = _interopRequireDefault(require("module"));
|
---|
17 |
|
---|
18 | var _full = require("klona/full");
|
---|
19 |
|
---|
20 | var _cosmiconfig = require("cosmiconfig");
|
---|
21 |
|
---|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
23 |
|
---|
24 | const parentModule = module;
|
---|
25 |
|
---|
26 | const stat = (inputFileSystem, filePath) => new Promise((resolve, reject) => {
|
---|
27 | inputFileSystem.stat(filePath, (err, stats) => {
|
---|
28 | if (err) {
|
---|
29 | reject(err);
|
---|
30 | }
|
---|
31 |
|
---|
32 | resolve(stats);
|
---|
33 | });
|
---|
34 | });
|
---|
35 |
|
---|
36 | function exec(code, loaderContext) {
|
---|
37 | const {
|
---|
38 | resource,
|
---|
39 | context
|
---|
40 | } = loaderContext;
|
---|
41 | const module = new _module.default(resource, parentModule); // eslint-disable-next-line no-underscore-dangle
|
---|
42 |
|
---|
43 | module.paths = _module.default._nodeModulePaths(context);
|
---|
44 | module.filename = resource; // eslint-disable-next-line no-underscore-dangle
|
---|
45 |
|
---|
46 | module._compile(code, resource);
|
---|
47 |
|
---|
48 | return module.exports;
|
---|
49 | }
|
---|
50 |
|
---|
51 | async function loadConfig(loaderContext, config, postcssOptions) {
|
---|
52 | const searchPath = typeof config === "string" ? _path.default.resolve(config) : _path.default.dirname(loaderContext.resourcePath);
|
---|
53 | let stats;
|
---|
54 |
|
---|
55 | try {
|
---|
56 | stats = await stat(loaderContext.fs, searchPath);
|
---|
57 | } catch (errorIgnore) {
|
---|
58 | throw new Error(`No PostCSS config found in: ${searchPath}`);
|
---|
59 | }
|
---|
60 |
|
---|
61 | const explorer = (0, _cosmiconfig.cosmiconfig)("postcss");
|
---|
62 | let result;
|
---|
63 |
|
---|
64 | try {
|
---|
65 | if (stats.isFile()) {
|
---|
66 | result = await explorer.load(searchPath);
|
---|
67 | } else {
|
---|
68 | result = await explorer.search(searchPath);
|
---|
69 | }
|
---|
70 | } catch (error) {
|
---|
71 | throw error;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!result) {
|
---|
75 | return {};
|
---|
76 | }
|
---|
77 |
|
---|
78 | loaderContext.addBuildDependency(result.filepath);
|
---|
79 |
|
---|
80 | if (result.isEmpty) {
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (typeof result.config === "function") {
|
---|
85 | const api = {
|
---|
86 | mode: loaderContext.mode,
|
---|
87 | file: loaderContext.resourcePath,
|
---|
88 | // For complex use
|
---|
89 | webpackLoaderContext: loaderContext,
|
---|
90 | // Partial compatibility with `postcss-cli`
|
---|
91 | env: loaderContext.mode,
|
---|
92 | options: postcssOptions || {}
|
---|
93 | };
|
---|
94 | result.config = result.config(api);
|
---|
95 | }
|
---|
96 |
|
---|
97 | result = (0, _full.klona)(result);
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | function loadPlugin(plugin, options, file) {
|
---|
102 | try {
|
---|
103 | if (!options || Object.keys(options).length === 0) {
|
---|
104 | // eslint-disable-next-line global-require, import/no-dynamic-require
|
---|
105 | const loadedPlugin = require(plugin);
|
---|
106 |
|
---|
107 | if (loadedPlugin.default) {
|
---|
108 | return loadedPlugin.default;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return loadedPlugin;
|
---|
112 | } // eslint-disable-next-line global-require, import/no-dynamic-require
|
---|
113 |
|
---|
114 |
|
---|
115 | const loadedPlugin = require(plugin);
|
---|
116 |
|
---|
117 | if (loadedPlugin.default) {
|
---|
118 | return loadedPlugin.default(options);
|
---|
119 | }
|
---|
120 |
|
---|
121 | return loadedPlugin(options);
|
---|
122 | } catch (error) {
|
---|
123 | throw new Error(`Loading PostCSS "${plugin}" plugin failed: ${error.message}\n\n(@${file})`);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | function pluginFactory() {
|
---|
128 | const listOfPlugins = new Map();
|
---|
129 | return plugins => {
|
---|
130 | if (typeof plugins === "undefined") {
|
---|
131 | return listOfPlugins;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (Array.isArray(plugins)) {
|
---|
135 | for (const plugin of plugins) {
|
---|
136 | if (Array.isArray(plugin)) {
|
---|
137 | const [name, options] = plugin;
|
---|
138 | listOfPlugins.set(name, options);
|
---|
139 | } else if (plugin && typeof plugin === "function") {
|
---|
140 | listOfPlugins.set(plugin);
|
---|
141 | } else if (plugin && Object.keys(plugin).length === 1 && (typeof plugin[Object.keys(plugin)[0]] === "object" || typeof plugin[Object.keys(plugin)[0]] === "boolean") && plugin[Object.keys(plugin)[0]] !== null) {
|
---|
142 | const [name] = Object.keys(plugin);
|
---|
143 | const options = plugin[name];
|
---|
144 |
|
---|
145 | if (options === false) {
|
---|
146 | listOfPlugins.delete(name);
|
---|
147 | } else {
|
---|
148 | listOfPlugins.set(name, options);
|
---|
149 | }
|
---|
150 | } else if (plugin) {
|
---|
151 | listOfPlugins.set(plugin);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | } else {
|
---|
155 | const objectPlugins = Object.entries(plugins);
|
---|
156 |
|
---|
157 | for (const [name, options] of objectPlugins) {
|
---|
158 | if (options === false) {
|
---|
159 | listOfPlugins.delete(name);
|
---|
160 | } else {
|
---|
161 | listOfPlugins.set(name, options);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | return listOfPlugins;
|
---|
167 | };
|
---|
168 | }
|
---|
169 |
|
---|
170 | async function load(module) {
|
---|
171 | let exports;
|
---|
172 |
|
---|
173 | try {
|
---|
174 | // eslint-disable-next-line import/no-dynamic-require, global-require
|
---|
175 | exports = require(module);
|
---|
176 | return exports;
|
---|
177 | } catch (requireError) {
|
---|
178 | let importESM;
|
---|
179 |
|
---|
180 | try {
|
---|
181 | // eslint-disable-next-line no-new-func
|
---|
182 | importESM = new Function("id", "return import(id);");
|
---|
183 | } catch (e) {
|
---|
184 | importESM = null;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (requireError.code === "ERR_REQUIRE_ESM" && importESM) {
|
---|
188 | exports = await importESM(module);
|
---|
189 | return exports.default;
|
---|
190 | }
|
---|
191 |
|
---|
192 | throw requireError;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | async function getPostcssOptions(loaderContext, loadedConfig = {}, postcssOptions = {}) {
|
---|
197 | const file = loaderContext.resourcePath;
|
---|
198 | let normalizedPostcssOptions = postcssOptions;
|
---|
199 |
|
---|
200 | if (typeof normalizedPostcssOptions === "function") {
|
---|
201 | normalizedPostcssOptions = normalizedPostcssOptions(loaderContext);
|
---|
202 | }
|
---|
203 |
|
---|
204 | let plugins = [];
|
---|
205 |
|
---|
206 | try {
|
---|
207 | const factory = pluginFactory();
|
---|
208 |
|
---|
209 | if (loadedConfig.config && loadedConfig.config.plugins) {
|
---|
210 | factory(loadedConfig.config.plugins);
|
---|
211 | }
|
---|
212 |
|
---|
213 | factory(normalizedPostcssOptions.plugins);
|
---|
214 | plugins = [...factory()].map(item => {
|
---|
215 | const [plugin, options] = item;
|
---|
216 |
|
---|
217 | if (typeof plugin === "string") {
|
---|
218 | return loadPlugin(plugin, options, file);
|
---|
219 | }
|
---|
220 |
|
---|
221 | return plugin;
|
---|
222 | });
|
---|
223 | } catch (error) {
|
---|
224 | loaderContext.emitError(error);
|
---|
225 | }
|
---|
226 |
|
---|
227 | const processOptionsFromConfig = loadedConfig.config || {};
|
---|
228 |
|
---|
229 | if (processOptionsFromConfig.from) {
|
---|
230 | processOptionsFromConfig.from = _path.default.resolve(_path.default.dirname(loadedConfig.filepath), processOptionsFromConfig.from);
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (processOptionsFromConfig.to) {
|
---|
234 | processOptionsFromConfig.to = _path.default.resolve(_path.default.dirname(loadedConfig.filepath), processOptionsFromConfig.to);
|
---|
235 | } // No need them for processOptions
|
---|
236 |
|
---|
237 |
|
---|
238 | delete processOptionsFromConfig.plugins;
|
---|
239 | const processOptionsFromOptions = (0, _full.klona)(normalizedPostcssOptions);
|
---|
240 |
|
---|
241 | if (processOptionsFromOptions.from) {
|
---|
242 | processOptionsFromOptions.from = _path.default.resolve(loaderContext.rootContext, processOptionsFromOptions.from);
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (processOptionsFromOptions.to) {
|
---|
246 | processOptionsFromOptions.to = _path.default.resolve(loaderContext.rootContext, processOptionsFromOptions.to);
|
---|
247 | } // No need them for processOptions
|
---|
248 |
|
---|
249 |
|
---|
250 | delete processOptionsFromOptions.config;
|
---|
251 | delete processOptionsFromOptions.plugins;
|
---|
252 | const processOptions = {
|
---|
253 | from: file,
|
---|
254 | to: file,
|
---|
255 | map: false,
|
---|
256 | ...processOptionsFromConfig,
|
---|
257 | ...processOptionsFromOptions
|
---|
258 | };
|
---|
259 |
|
---|
260 | if (typeof processOptions.parser === "string") {
|
---|
261 | try {
|
---|
262 | processOptions.parser = await load(processOptions.parser);
|
---|
263 | } catch (error) {
|
---|
264 | loaderContext.emitError(new Error(`Loading PostCSS "${processOptions.parser}" parser failed: ${error.message}\n\n(@${file})`));
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (typeof processOptions.stringifier === "string") {
|
---|
269 | try {
|
---|
270 | processOptions.stringifier = await load(processOptions.stringifier);
|
---|
271 | } catch (error) {
|
---|
272 | loaderContext.emitError(new Error(`Loading PostCSS "${processOptions.stringifier}" stringifier failed: ${error.message}\n\n(@${file})`));
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (typeof processOptions.syntax === "string") {
|
---|
277 | try {
|
---|
278 | processOptions.syntax = await load(processOptions.syntax);
|
---|
279 | } catch (error) {
|
---|
280 | loaderContext.emitError(new Error(`Loading PostCSS "${processOptions.syntax}" syntax failed: ${error.message}\n\n(@${file})`));
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (processOptions.map === true) {
|
---|
285 | // https://github.com/postcss/postcss/blob/master/docs/source-maps.md
|
---|
286 | processOptions.map = {
|
---|
287 | inline: true
|
---|
288 | };
|
---|
289 | }
|
---|
290 |
|
---|
291 | return {
|
---|
292 | plugins,
|
---|
293 | processOptions
|
---|
294 | };
|
---|
295 | }
|
---|
296 |
|
---|
297 | const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
|
---|
298 | const ABSOLUTE_SCHEME = /^[a-z0-9+\-.]+:/i;
|
---|
299 |
|
---|
300 | function getURLType(source) {
|
---|
301 | if (source[0] === "/") {
|
---|
302 | if (source[1] === "/") {
|
---|
303 | return "scheme-relative";
|
---|
304 | }
|
---|
305 |
|
---|
306 | return "path-absolute";
|
---|
307 | }
|
---|
308 |
|
---|
309 | if (IS_NATIVE_WIN32_PATH.test(source)) {
|
---|
310 | return "path-absolute";
|
---|
311 | }
|
---|
312 |
|
---|
313 | return ABSOLUTE_SCHEME.test(source) ? "absolute" : "path-relative";
|
---|
314 | }
|
---|
315 |
|
---|
316 | function normalizeSourceMap(map, resourceContext) {
|
---|
317 | let newMap = map; // Some loader emit source map as string
|
---|
318 | // Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
|
---|
319 |
|
---|
320 | if (typeof newMap === "string") {
|
---|
321 | newMap = JSON.parse(newMap);
|
---|
322 | }
|
---|
323 |
|
---|
324 | delete newMap.file;
|
---|
325 | const {
|
---|
326 | sourceRoot
|
---|
327 | } = newMap;
|
---|
328 | delete newMap.sourceRoot;
|
---|
329 |
|
---|
330 | if (newMap.sources) {
|
---|
331 | newMap.sources = newMap.sources.map(source => {
|
---|
332 | const sourceType = getURLType(source); // Do no touch `scheme-relative` and `absolute` URLs
|
---|
333 |
|
---|
334 | if (sourceType === "path-relative" || sourceType === "path-absolute") {
|
---|
335 | const absoluteSource = sourceType === "path-relative" && sourceRoot ? _path.default.resolve(sourceRoot, _path.default.normalize(source)) : _path.default.normalize(source);
|
---|
336 | return _path.default.relative(resourceContext, absoluteSource);
|
---|
337 | }
|
---|
338 |
|
---|
339 | return source;
|
---|
340 | });
|
---|
341 | }
|
---|
342 |
|
---|
343 | return newMap;
|
---|
344 | }
|
---|
345 |
|
---|
346 | function normalizeSourceMapAfterPostcss(map, resourceContext) {
|
---|
347 | const newMap = map; // result.map.file is an optional property that provides the output filename.
|
---|
348 | // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
|
---|
349 | // eslint-disable-next-line no-param-reassign
|
---|
350 |
|
---|
351 | delete newMap.file; // eslint-disable-next-line no-param-reassign
|
---|
352 |
|
---|
353 | newMap.sourceRoot = ""; // eslint-disable-next-line no-param-reassign
|
---|
354 |
|
---|
355 | newMap.sources = newMap.sources.map(source => {
|
---|
356 | if (source.indexOf("<") === 0) {
|
---|
357 | return source;
|
---|
358 | }
|
---|
359 |
|
---|
360 | const sourceType = getURLType(source); // Do no touch `scheme-relative`, `path-absolute` and `absolute` types
|
---|
361 |
|
---|
362 | if (sourceType === "path-relative") {
|
---|
363 | return _path.default.resolve(resourceContext, source);
|
---|
364 | }
|
---|
365 |
|
---|
366 | return source;
|
---|
367 | });
|
---|
368 | return newMap;
|
---|
369 | }
|
---|
370 |
|
---|
371 | function findPackageJSONDir(cwd, statSync) {
|
---|
372 | let dir = cwd;
|
---|
373 |
|
---|
374 | for (;;) {
|
---|
375 | try {
|
---|
376 | if (statSync(_path.default.join(dir, "package.json")).isFile()) {
|
---|
377 | break;
|
---|
378 | }
|
---|
379 | } catch (error) {// Nothing
|
---|
380 | }
|
---|
381 |
|
---|
382 | const parent = _path.default.dirname(dir);
|
---|
383 |
|
---|
384 | if (dir === parent) {
|
---|
385 | dir = null;
|
---|
386 | break;
|
---|
387 | }
|
---|
388 |
|
---|
389 | dir = parent;
|
---|
390 | }
|
---|
391 |
|
---|
392 | return dir;
|
---|
393 | }
|
---|
394 |
|
---|
395 | function getPostcssImplementation(loaderContext, implementation) {
|
---|
396 | let resolvedImplementation = implementation;
|
---|
397 |
|
---|
398 | if (!implementation || typeof implementation === "string") {
|
---|
399 | const postcssImplPkg = implementation || "postcss";
|
---|
400 |
|
---|
401 | try {
|
---|
402 | // eslint-disable-next-line import/no-dynamic-require, global-require
|
---|
403 | resolvedImplementation = require(postcssImplPkg);
|
---|
404 | } catch (error) {
|
---|
405 | loaderContext.emitError(error); // eslint-disable-next-line consistent-return
|
---|
406 |
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | } // eslint-disable-next-line consistent-return
|
---|
410 |
|
---|
411 |
|
---|
412 | return resolvedImplementation;
|
---|
413 | } |
---|