UNPKG

4.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.getUndoPath = getUndoPath;
12exports.SINGLE_DOT_PATH_SEGMENT = exports.ABSOLUTE_PUBLIC_PATH = exports.AUTO_PUBLIC_PATH = exports.MODULE_TYPE = void 0;
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}
91
92function stringifyRequest(loaderContext, request) {
93 const splitted = request.split("!");
94 const {
95 context
96 } = loaderContext;
97 return JSON.stringify(splitted.map(part => {
98 // First, separate singlePath from query, because the query might contain paths again
99 const splittedPart = part.match(/^(.*?)(\?.*)/);
100 const query = splittedPart ? splittedPart[2] : "";
101 let singlePath = splittedPart ? splittedPart[1] : part;
102
103 if (isAbsolutePath(singlePath) && context) {
104 singlePath = _path.default.relative(context, singlePath);
105
106 if (isAbsolutePath(singlePath)) {
107 // If singlePath still matches an absolute path, singlePath was on a different drive than context.
108 // In this case, we leave the path platform-specific without replacing any separators.
109 // @see https://github.com/webpack/loader-utils/pull/14
110 return singlePath + query;
111 }
112
113 if (isRelativePath(singlePath) === false) {
114 // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
115 singlePath = `./${singlePath}`;
116 }
117 }
118
119 return singlePath.replace(/\\/g, "/") + query;
120 }).join("!"));
121}
122
123function getUndoPath(filename, outputPath, enforceRelative) {
124 let depth = -1;
125 let append = ""; // eslint-disable-next-line no-param-reassign
126
127 outputPath = outputPath.replace(/[\\/]$/, "");
128
129 for (const part of filename.split(/[/\\]+/)) {
130 if (part === "..") {
131 if (depth > -1) {
132 // eslint-disable-next-line no-plusplus
133 depth--;
134 } else {
135 const i = outputPath.lastIndexOf("/");
136 const j = outputPath.lastIndexOf("\\");
137 const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
138
139 if (pos < 0) {
140 return `${outputPath}/`;
141 }
142
143 append = `${outputPath.slice(pos + 1)}/${append}`; // eslint-disable-next-line no-param-reassign
144
145 outputPath = outputPath.slice(0, pos);
146 }
147 } else if (part !== ".") {
148 // eslint-disable-next-line no-plusplus
149 depth++;
150 }
151 }
152
153 return depth > 0 ? `${"../".repeat(depth)}${append}` : enforceRelative ? `./${append}` : append;
154}
\No newline at end of file