source: trip-planner-front/node_modules/webpack/lib/ModuleFilenameHelpers.js@ 84d0fbb

Last change on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[6a3a178]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 =
16 /\[all-?loaders\]\[resource\]/gi;
17ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
18ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
19ModuleFilenameHelpers.RESOURCE = "[resource]";
20ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
21ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
22// cSpell:words olute
23ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
24 /\[abs(olute)?-?resource-?path\]/gi;
25ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
26ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
27ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
28ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
29ModuleFilenameHelpers.LOADERS = "[loaders]";
30ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
31ModuleFilenameHelpers.QUERY = "[query]";
32ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
33ModuleFilenameHelpers.ID = "[id]";
34ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
35ModuleFilenameHelpers.HASH = "[hash]";
36ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
37ModuleFilenameHelpers.NAMESPACE = "[namespace]";
38ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
39
40const 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
48const 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
56const 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
65const asRegExp = test => {
66 if (typeof test === "string") {
67 test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
68 }
69 return test;
70};
71
72const 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
92const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
93
94ModuleFilenameHelpers.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
209ModuleFilenameHelpers.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
232ModuleFilenameHelpers.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
242ModuleFilenameHelpers.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};
Note: See TracBrowser for help on using the repository browser.