1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const createHash = require("./util/createHash");
|
---|
9 | const memoize = require("./util/memoize");
|
---|
10 |
|
---|
11 | const ModuleFilenameHelpers = exports;
|
---|
12 |
|
---|
13 | // TODO webpack 6: consider removing these
|
---|
14 | ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
|
---|
15 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
|
---|
16 | /\[all-?loaders\]\[resource\]/gi;
|
---|
17 | ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
|
---|
18 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
|
---|
19 | ModuleFilenameHelpers.RESOURCE = "[resource]";
|
---|
20 | ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
|
---|
21 | ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
|
---|
22 | // cSpell:words olute
|
---|
23 | ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
|
---|
24 | /\[abs(olute)?-?resource-?path\]/gi;
|
---|
25 | ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
|
---|
26 | ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
|
---|
27 | ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
|
---|
28 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
|
---|
29 | ModuleFilenameHelpers.LOADERS = "[loaders]";
|
---|
30 | ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
|
---|
31 | ModuleFilenameHelpers.QUERY = "[query]";
|
---|
32 | ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
|
---|
33 | ModuleFilenameHelpers.ID = "[id]";
|
---|
34 | ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
|
---|
35 | ModuleFilenameHelpers.HASH = "[hash]";
|
---|
36 | ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
|
---|
37 | ModuleFilenameHelpers.NAMESPACE = "[namespace]";
|
---|
38 | ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
|
---|
39 |
|
---|
40 | const getAfter = (strFn, token) => {
|
---|
41 | return () => {
|
---|
42 | const str = strFn();
|
---|
43 | const idx = str.indexOf(token);
|
---|
44 | return idx < 0 ? "" : str.substr(idx);
|
---|
45 | };
|
---|
46 | };
|
---|
47 |
|
---|
48 | const getBefore = (strFn, token) => {
|
---|
49 | return () => {
|
---|
50 | const str = strFn();
|
---|
51 | const idx = str.lastIndexOf(token);
|
---|
52 | return idx < 0 ? "" : str.substr(0, idx);
|
---|
53 | };
|
---|
54 | };
|
---|
55 |
|
---|
56 | const getHash = strFn => {
|
---|
57 | return () => {
|
---|
58 | const hash = createHash("md4");
|
---|
59 | hash.update(strFn());
|
---|
60 | const digest = /** @type {string} */ (hash.digest("hex"));
|
---|
61 | return digest.substr(0, 4);
|
---|
62 | };
|
---|
63 | };
|
---|
64 |
|
---|
65 | const asRegExp = test => {
|
---|
66 | if (typeof test === "string") {
|
---|
67 | test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
---|
68 | }
|
---|
69 | return test;
|
---|
70 | };
|
---|
71 |
|
---|
72 | const lazyObject = obj => {
|
---|
73 | const newObj = {};
|
---|
74 | for (const key of Object.keys(obj)) {
|
---|
75 | const fn = obj[key];
|
---|
76 | Object.defineProperty(newObj, key, {
|
---|
77 | get: () => fn(),
|
---|
78 | set: v => {
|
---|
79 | Object.defineProperty(newObj, key, {
|
---|
80 | value: v,
|
---|
81 | enumerable: true,
|
---|
82 | writable: true
|
---|
83 | });
|
---|
84 | },
|
---|
85 | enumerable: true,
|
---|
86 | configurable: true
|
---|
87 | });
|
---|
88 | }
|
---|
89 | return newObj;
|
---|
90 | };
|
---|
91 |
|
---|
92 | const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
|
---|
93 |
|
---|
94 | ModuleFilenameHelpers.createFilename = (
|
---|
95 | module,
|
---|
96 | options,
|
---|
97 | { requestShortener, chunkGraph }
|
---|
98 | ) => {
|
---|
99 | const opts = {
|
---|
100 | namespace: "",
|
---|
101 | moduleFilenameTemplate: "",
|
---|
102 | ...(typeof options === "object"
|
---|
103 | ? options
|
---|
104 | : {
|
---|
105 | moduleFilenameTemplate: options
|
---|
106 | })
|
---|
107 | };
|
---|
108 |
|
---|
109 | let absoluteResourcePath;
|
---|
110 | let hash;
|
---|
111 | let identifier;
|
---|
112 | let moduleId;
|
---|
113 | let shortIdentifier;
|
---|
114 | if (module === undefined) module = "";
|
---|
115 | if (typeof module === "string") {
|
---|
116 | shortIdentifier = memoize(() => requestShortener.shorten(module));
|
---|
117 | identifier = shortIdentifier;
|
---|
118 | moduleId = () => "";
|
---|
119 | absoluteResourcePath = () => module.split("!").pop();
|
---|
120 | hash = getHash(identifier);
|
---|
121 | } else {
|
---|
122 | shortIdentifier = memoize(() =>
|
---|
123 | module.readableIdentifier(requestShortener)
|
---|
124 | );
|
---|
125 | identifier = memoize(() => requestShortener.shorten(module.identifier()));
|
---|
126 | moduleId = () => chunkGraph.getModuleId(module);
|
---|
127 | absoluteResourcePath = () => module.identifier().split("!").pop();
|
---|
128 | hash = getHash(identifier);
|
---|
129 | }
|
---|
130 | const resource = memoize(() => shortIdentifier().split("!").pop());
|
---|
131 |
|
---|
132 | const loaders = getBefore(shortIdentifier, "!");
|
---|
133 | const allLoaders = getBefore(identifier, "!");
|
---|
134 | const query = getAfter(resource, "?");
|
---|
135 | const resourcePath = () => {
|
---|
136 | const q = query().length;
|
---|
137 | return q === 0 ? resource() : resource().slice(0, -q);
|
---|
138 | };
|
---|
139 | if (typeof opts.moduleFilenameTemplate === "function") {
|
---|
140 | return opts.moduleFilenameTemplate(
|
---|
141 | lazyObject({
|
---|
142 | identifier: identifier,
|
---|
143 | shortIdentifier: shortIdentifier,
|
---|
144 | resource: resource,
|
---|
145 | resourcePath: memoize(resourcePath),
|
---|
146 | absoluteResourcePath: memoize(absoluteResourcePath),
|
---|
147 | allLoaders: memoize(allLoaders),
|
---|
148 | query: memoize(query),
|
---|
149 | moduleId: memoize(moduleId),
|
---|
150 | hash: memoize(hash),
|
---|
151 | namespace: () => opts.namespace
|
---|
152 | })
|
---|
153 | );
|
---|
154 | }
|
---|
155 |
|
---|
156 | // TODO webpack 6: consider removing alternatives without dashes
|
---|
157 | /** @type {Map<string, function(): string>} */
|
---|
158 | const replacements = new Map([
|
---|
159 | ["identifier", identifier],
|
---|
160 | ["short-identifier", shortIdentifier],
|
---|
161 | ["resource", resource],
|
---|
162 | ["resource-path", resourcePath],
|
---|
163 | // cSpell:words resourcepath
|
---|
164 | ["resourcepath", resourcePath],
|
---|
165 | ["absolute-resource-path", absoluteResourcePath],
|
---|
166 | ["abs-resource-path", absoluteResourcePath],
|
---|
167 | // cSpell:words absoluteresource
|
---|
168 | ["absoluteresource-path", absoluteResourcePath],
|
---|
169 | // cSpell:words absresource
|
---|
170 | ["absresource-path", absoluteResourcePath],
|
---|
171 | // cSpell:words resourcepath
|
---|
172 | ["absolute-resourcepath", absoluteResourcePath],
|
---|
173 | // cSpell:words resourcepath
|
---|
174 | ["abs-resourcepath", absoluteResourcePath],
|
---|
175 | // cSpell:words absoluteresourcepath
|
---|
176 | ["absoluteresourcepath", absoluteResourcePath],
|
---|
177 | // cSpell:words absresourcepath
|
---|
178 | ["absresourcepath", absoluteResourcePath],
|
---|
179 | ["all-loaders", allLoaders],
|
---|
180 | // cSpell:words allloaders
|
---|
181 | ["allloaders", allLoaders],
|
---|
182 | ["loaders", loaders],
|
---|
183 | ["query", query],
|
---|
184 | ["id", moduleId],
|
---|
185 | ["hash", hash],
|
---|
186 | ["namespace", () => opts.namespace]
|
---|
187 | ]);
|
---|
188 |
|
---|
189 | // TODO webpack 6: consider removing weird double placeholders
|
---|
190 | return opts.moduleFilenameTemplate
|
---|
191 | .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
|
---|
192 | .replace(
|
---|
193 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
|
---|
194 | "[short-identifier]"
|
---|
195 | )
|
---|
196 | .replace(REGEXP, (match, content) => {
|
---|
197 | if (content.length + 2 === match.length) {
|
---|
198 | const replacement = replacements.get(content.toLowerCase());
|
---|
199 | if (replacement !== undefined) {
|
---|
200 | return replacement();
|
---|
201 | }
|
---|
202 | } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
|
---|
203 | return `[${match.slice(2, -2)}]`;
|
---|
204 | }
|
---|
205 | return match;
|
---|
206 | });
|
---|
207 | };
|
---|
208 |
|
---|
209 | ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
|
---|
210 | const countMap = Object.create(null);
|
---|
211 | const posMap = Object.create(null);
|
---|
212 | array.forEach((item, idx) => {
|
---|
213 | countMap[item] = countMap[item] || [];
|
---|
214 | countMap[item].push(idx);
|
---|
215 | posMap[item] = 0;
|
---|
216 | });
|
---|
217 | if (comparator) {
|
---|
218 | Object.keys(countMap).forEach(item => {
|
---|
219 | countMap[item].sort(comparator);
|
---|
220 | });
|
---|
221 | }
|
---|
222 | return array.map((item, i) => {
|
---|
223 | if (countMap[item].length > 1) {
|
---|
224 | if (comparator && countMap[item][0] === i) return item;
|
---|
225 | return fn(item, i, posMap[item]++);
|
---|
226 | } else {
|
---|
227 | return item;
|
---|
228 | }
|
---|
229 | });
|
---|
230 | };
|
---|
231 |
|
---|
232 | ModuleFilenameHelpers.matchPart = (str, test) => {
|
---|
233 | if (!test) return true;
|
---|
234 | test = asRegExp(test);
|
---|
235 | if (Array.isArray(test)) {
|
---|
236 | return test.map(asRegExp).some(regExp => regExp.test(str));
|
---|
237 | } else {
|
---|
238 | return test.test(str);
|
---|
239 | }
|
---|
240 | };
|
---|
241 |
|
---|
242 | ModuleFilenameHelpers.matchObject = (obj, str) => {
|
---|
243 | if (obj.test) {
|
---|
244 | if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | if (obj.include) {
|
---|
249 | if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | if (obj.exclude) {
|
---|
254 | if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | return true;
|
---|
259 | };
|
---|