UNPKG

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