UNPKG

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