UNPKG

3.57 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.trueFn = trueFn;
7exports.findModuleById = findModuleById;
8exports.evalModuleCode = evalModuleCode;
9exports.compareModulesByIdentifier = compareModulesByIdentifier;
10exports.stringifyRequest = stringifyRequest;
11exports.SINGLE_DOT_PATH_SEGMENT = exports.ABSOLUTE_PUBLIC_PATH = exports.AUTO_PUBLIC_PATH = exports.MODULE_TYPE = void 0;
12
13var _module = _interopRequireDefault(require("module"));
14
15var _path = _interopRequireDefault(require("path"));
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function trueFn() {
20 return true;
21}
22
23function findModuleById(compilation, id) {
24 const {
25 modules,
26 chunkGraph
27 } = compilation;
28
29 for (const module of modules) {
30 const moduleId = typeof chunkGraph !== "undefined" ? chunkGraph.getModuleId(module) : module.id;
31
32 if (moduleId === id) {
33 return module;
34 }
35 }
36
37 return null;
38}
39
40function evalModuleCode(loaderContext, code, filename) {
41 const module = new _module.default(filename, loaderContext);
42 module.paths = _module.default._nodeModulePaths(loaderContext.context); // eslint-disable-line no-underscore-dangle
43
44 module.filename = filename;
45
46 module._compile(code, filename); // eslint-disable-line no-underscore-dangle
47
48
49 return module.exports;
50}
51
52function compareIds(a, b) {
53 if (typeof a !== typeof b) {
54 return typeof a < typeof b ? -1 : 1;
55 }
56
57 if (a < b) {
58 return -1;
59 }
60
61 if (a > b) {
62 return 1;
63 }
64
65 return 0;
66}
67
68function compareModulesByIdentifier(a, b) {
69 return compareIds(a.identifier(), b.identifier());
70}
71
72const MODULE_TYPE = "css/mini-extract";
73exports.MODULE_TYPE = MODULE_TYPE;
74const AUTO_PUBLIC_PATH = "__mini_css_extract_plugin_public_path_auto__";
75exports.AUTO_PUBLIC_PATH = AUTO_PUBLIC_PATH;
76const ABSOLUTE_PUBLIC_PATH = "webpack:///mini-css-extract-plugin/";
77exports.ABSOLUTE_PUBLIC_PATH = ABSOLUTE_PUBLIC_PATH;
78const SINGLE_DOT_PATH_SEGMENT = "__mini_css_extract_plugin_single_dot_path_segment__";
79exports.SINGLE_DOT_PATH_SEGMENT = SINGLE_DOT_PATH_SEGMENT;
80
81function isAbsolutePath(str) {
82 return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str);
83}
84
85const RELATIVE_PATH_REGEXP = /^\.\.?[/\\]/;
86
87function isRelativePath(str) {
88 return RELATIVE_PATH_REGEXP.test(str);
89}
90
91function stringifyRequest(loaderContext, request) {
92 const splitted = request.split("!");
93 const {
94 context
95 } = loaderContext;
96 return JSON.stringify(splitted.map(part => {
97 // First, separate singlePath from query, because the query might contain paths again
98 const splittedPart = part.match(/^(.*?)(\?.*)/);
99 const query = splittedPart ? splittedPart[2] : "";
100 let singlePath = splittedPart ? splittedPart[1] : part;
101
102 if (isAbsolutePath(singlePath) && context) {
103 singlePath = _path.default.relative(context, singlePath);
104
105 if (isAbsolutePath(singlePath)) {
106 // If singlePath still matches an absolute path, singlePath was on a different drive than context.
107 // In this case, we leave the path platform-specific without replacing any separators.
108 // @see https://github.com/webpack/loader-utils/pull/14
109 return singlePath + query;
110 }
111
112 if (isRelativePath(singlePath) === false) {
113 // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
114 singlePath = `./${singlePath}`;
115 }
116 }
117
118 return singlePath.replace(/\\/g, "/") + query;
119 }).join("!"));
120}
\No newline at end of file