source: frontend/node_modules/postcss-loader/dist/utils.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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