1 | "use strict";
|
2 |
|
3 | const NativeModule = require("module");
|
4 | const path = require("path");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function trueFn() {
|
14 | return true;
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function findModuleById(compilation, id) {
|
23 | const {
|
24 | modules,
|
25 | chunkGraph
|
26 | } = compilation;
|
27 | for (const module of modules) {
|
28 | const moduleId = typeof chunkGraph !== "undefined" ? chunkGraph.getModuleId(module) : module.id;
|
29 | if (moduleId === id) {
|
30 | return module;
|
31 | }
|
32 | }
|
33 | return null;
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function evalModuleCode(loaderContext, code, filename) {
|
43 |
|
44 | const module = new NativeModule(filename, loaderContext);
|
45 |
|
46 |
|
47 | module.paths = NativeModule._nodeModulePaths(loaderContext.context);
|
48 | module.filename = filename;
|
49 |
|
50 | module._compile(code, filename);
|
51 |
|
52 | return module.exports;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function compareIds(a, b) {
|
61 | if (typeof a !== typeof b) {
|
62 | return typeof a < typeof b ? -1 : 1;
|
63 | }
|
64 | if (a < b) {
|
65 | return -1;
|
66 | }
|
67 | if (a > b) {
|
68 | return 1;
|
69 | }
|
70 | return 0;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function compareModulesByIdentifier(a, b) {
|
79 | return compareIds(a.identifier(), b.identifier());
|
80 | }
|
81 | const MODULE_TYPE = "css/mini-extract";
|
82 | const AUTO_PUBLIC_PATH = "__mini_css_extract_plugin_public_path_auto__";
|
83 | const ABSOLUTE_PUBLIC_PATH = "webpack:///mini-css-extract-plugin/";
|
84 | const BASE_URI = "webpack://";
|
85 | const SINGLE_DOT_PATH_SEGMENT = "__mini_css_extract_plugin_single_dot_path_segment__";
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function isAbsolutePath(str) {
|
92 | return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
|
93 | }
|
94 | const RELATIVE_PATH_REGEXP = /^\.\.?[/\\]/;
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function isRelativePath(str) {
|
101 | return RELATIVE_PATH_REGEXP.test(str);
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function stringifyRequest(loaderContext, request) {
|
111 | if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") {
|
112 | return JSON.stringify(loaderContext.utils.contextify(loaderContext.context || loaderContext.rootContext, request));
|
113 | }
|
114 | const splitted = request.split("!");
|
115 | const {
|
116 | context
|
117 | } = loaderContext;
|
118 | return JSON.stringify(splitted.map(part => {
|
119 |
|
120 | const splittedPart = part.match(/^(.*?)(\?.*)/);
|
121 | const query = splittedPart ? splittedPart[2] : "";
|
122 | let singlePath = splittedPart ? splittedPart[1] : part;
|
123 | if (isAbsolutePath(singlePath) && context) {
|
124 | singlePath = path.relative(context, singlePath);
|
125 | if (isAbsolutePath(singlePath)) {
|
126 |
|
127 |
|
128 |
|
129 | return singlePath + query;
|
130 | }
|
131 | if (isRelativePath(singlePath) === false) {
|
132 |
|
133 | singlePath = `./${singlePath}`;
|
134 | }
|
135 | }
|
136 | return singlePath.replace(/\\/g, "/") + query;
|
137 | }).join("!"));
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function getUndoPath(filename, outputPath, enforceRelative) {
|
147 | let depth = -1;
|
148 | let append = "";
|
149 |
|
150 |
|
151 | outputPath = outputPath.replace(/[\\/]$/, "");
|
152 | for (const part of filename.split(/[/\\]+/)) {
|
153 | if (part === "..") {
|
154 | if (depth > -1) {
|
155 |
|
156 | depth--;
|
157 | } else {
|
158 | const i = outputPath.lastIndexOf("/");
|
159 | const j = outputPath.lastIndexOf("\\");
|
160 | const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
|
161 | if (pos < 0) {
|
162 | return `${outputPath}/`;
|
163 | }
|
164 | append = `${outputPath.slice(pos + 1)}/${append}`;
|
165 |
|
166 |
|
167 | outputPath = outputPath.slice(0, pos);
|
168 | }
|
169 | } else if (part !== ".") {
|
170 |
|
171 | depth++;
|
172 | }
|
173 | }
|
174 | return depth > 0 ? `${"../".repeat(depth)}${append}` : enforceRelative ? `./${append}` : append;
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function stringifyLocal(value) {
|
183 | return typeof value === "function" ? value.toString() : JSON.stringify(value);
|
184 | }
|
185 | module.exports = {
|
186 | trueFn,
|
187 | findModuleById,
|
188 | evalModuleCode,
|
189 | compareModulesByIdentifier,
|
190 | MODULE_TYPE,
|
191 | AUTO_PUBLIC_PATH,
|
192 | ABSOLUTE_PUBLIC_PATH,
|
193 | BASE_URI,
|
194 | SINGLE_DOT_PATH_SEGMENT,
|
195 | stringifyRequest,
|
196 | stringifyLocal,
|
197 | getUndoPath
|
198 | }; |
\ | No newline at end of file |