[79a0317] | 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 NormalModule = require("./NormalModule");
|
---|
| 9 | const createHash = require("./util/createHash");
|
---|
| 10 | const memoize = require("./util/memoize");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 13 | /** @typedef {import("./Module")} Module */
|
---|
| 14 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
---|
| 15 | /** @typedef {typeof import("./util/Hash")} Hash */
|
---|
| 16 |
|
---|
| 17 | /** @typedef {string | RegExp | (string | RegExp)[]} Matcher */
|
---|
| 18 | /** @typedef {{test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
|
---|
| 19 |
|
---|
| 20 | const ModuleFilenameHelpers = module.exports;
|
---|
| 21 |
|
---|
| 22 | // TODO webpack 6: consider removing these
|
---|
| 23 | ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
|
---|
| 24 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
|
---|
| 25 | /\[all-?loaders\]\[resource\]/gi;
|
---|
| 26 | ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
|
---|
| 27 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
|
---|
| 28 | ModuleFilenameHelpers.RESOURCE = "[resource]";
|
---|
| 29 | ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
|
---|
| 30 | ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
|
---|
| 31 | // cSpell:words olute
|
---|
| 32 | ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
|
---|
| 33 | /\[abs(olute)?-?resource-?path\]/gi;
|
---|
| 34 | ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
|
---|
| 35 | ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
|
---|
| 36 | ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
|
---|
| 37 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
|
---|
| 38 | ModuleFilenameHelpers.LOADERS = "[loaders]";
|
---|
| 39 | ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
|
---|
| 40 | ModuleFilenameHelpers.QUERY = "[query]";
|
---|
| 41 | ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
|
---|
| 42 | ModuleFilenameHelpers.ID = "[id]";
|
---|
| 43 | ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
|
---|
| 44 | ModuleFilenameHelpers.HASH = "[hash]";
|
---|
| 45 | ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
|
---|
| 46 | ModuleFilenameHelpers.NAMESPACE = "[namespace]";
|
---|
| 47 | ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
|
---|
| 48 |
|
---|
| 49 | /** @typedef {() => string} ReturnStringCallback */
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Returns a function that returns the part of the string after the token
|
---|
| 53 | * @param {ReturnStringCallback} strFn the function to get the string
|
---|
| 54 | * @param {string} token the token to search for
|
---|
| 55 | * @returns {ReturnStringCallback} a function that returns the part of the string after the token
|
---|
| 56 | */
|
---|
| 57 | const getAfter = (strFn, token) => () => {
|
---|
| 58 | const str = strFn();
|
---|
| 59 | const idx = str.indexOf(token);
|
---|
| 60 | return idx < 0 ? "" : str.slice(idx);
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Returns a function that returns the part of the string before the token
|
---|
| 65 | * @param {ReturnStringCallback} strFn the function to get the string
|
---|
| 66 | * @param {string} token the token to search for
|
---|
| 67 | * @returns {ReturnStringCallback} a function that returns the part of the string before the token
|
---|
| 68 | */
|
---|
| 69 | const getBefore = (strFn, token) => () => {
|
---|
| 70 | const str = strFn();
|
---|
| 71 | const idx = str.lastIndexOf(token);
|
---|
| 72 | return idx < 0 ? "" : str.slice(0, idx);
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Returns a function that returns a hash of the string
|
---|
| 77 | * @param {ReturnStringCallback} strFn the function to get the string
|
---|
| 78 | * @param {string | Hash=} hashFunction the hash function to use
|
---|
| 79 | * @returns {ReturnStringCallback} a function that returns the hash of the string
|
---|
| 80 | */
|
---|
| 81 | const getHash =
|
---|
| 82 | (strFn, hashFunction = "md4") =>
|
---|
| 83 | () => {
|
---|
| 84 | const hash = createHash(hashFunction);
|
---|
| 85 | hash.update(strFn());
|
---|
| 86 | const digest = /** @type {string} */ (hash.digest("hex"));
|
---|
| 87 | return digest.slice(0, 4);
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Returns a function that returns the string with the token replaced with the replacement
|
---|
| 92 | * @param {string|RegExp} test A regular expression string or Regular Expression object
|
---|
| 93 | * @returns {RegExp} A regular expression object
|
---|
| 94 | * @example
|
---|
| 95 | * ```js
|
---|
| 96 | * const test = asRegExp("test");
|
---|
| 97 | * test.test("test"); // true
|
---|
| 98 | *
|
---|
| 99 | * const test2 = asRegExp(/test/);
|
---|
| 100 | * test2.test("test"); // true
|
---|
| 101 | * ```
|
---|
| 102 | */
|
---|
| 103 | const asRegExp = test => {
|
---|
| 104 | if (typeof test === "string") {
|
---|
| 105 | // Escape special characters in the string to prevent them from being interpreted as special characters in a regular expression. Do this by
|
---|
| 106 | // adding a backslash before each special character
|
---|
| 107 | test = new RegExp(`^${test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")}`);
|
---|
| 108 | }
|
---|
| 109 | return test;
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * @template T
|
---|
| 114 | * Returns a lazy object. The object is lazy in the sense that the properties are
|
---|
| 115 | * only evaluated when they are accessed. This is only obtained by setting a function as the value for each key.
|
---|
| 116 | * @param {Record<string, () => T>} obj the object to convert to a lazy access object
|
---|
| 117 | * @returns {object} the lazy access object
|
---|
| 118 | */
|
---|
| 119 | const lazyObject = obj => {
|
---|
| 120 | const newObj = {};
|
---|
| 121 | for (const key of Object.keys(obj)) {
|
---|
| 122 | const fn = obj[key];
|
---|
| 123 | Object.defineProperty(newObj, key, {
|
---|
| 124 | get: () => fn(),
|
---|
| 125 | set: v => {
|
---|
| 126 | Object.defineProperty(newObj, key, {
|
---|
| 127 | value: v,
|
---|
| 128 | enumerable: true,
|
---|
| 129 | writable: true
|
---|
| 130 | });
|
---|
| 131 | },
|
---|
| 132 | enumerable: true,
|
---|
| 133 | configurable: true
|
---|
| 134 | });
|
---|
| 135 | }
|
---|
| 136 | return newObj;
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/gi;
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * @param {Module | string} module the module
|
---|
| 143 | * @param {TODO} options options
|
---|
| 144 | * @param {object} contextInfo context info
|
---|
| 145 | * @param {RequestShortener} contextInfo.requestShortener requestShortener
|
---|
| 146 | * @param {ChunkGraph} contextInfo.chunkGraph chunk graph
|
---|
| 147 | * @param {string | Hash=} contextInfo.hashFunction the hash function to use
|
---|
| 148 | * @returns {string} the filename
|
---|
| 149 | */
|
---|
| 150 | ModuleFilenameHelpers.createFilename = (
|
---|
| 151 | // eslint-disable-next-line default-param-last
|
---|
| 152 | module = "",
|
---|
| 153 | options,
|
---|
| 154 | { requestShortener, chunkGraph, hashFunction = "md4" }
|
---|
| 155 | ) => {
|
---|
| 156 | const opts = {
|
---|
| 157 | namespace: "",
|
---|
| 158 | moduleFilenameTemplate: "",
|
---|
| 159 | ...(typeof options === "object"
|
---|
| 160 | ? options
|
---|
| 161 | : {
|
---|
| 162 | moduleFilenameTemplate: options
|
---|
| 163 | })
|
---|
| 164 | };
|
---|
| 165 |
|
---|
| 166 | let absoluteResourcePath;
|
---|
| 167 | let hash;
|
---|
| 168 | /** @type {ReturnStringCallback} */
|
---|
| 169 | let identifier;
|
---|
| 170 | /** @type {ReturnStringCallback} */
|
---|
| 171 | let moduleId;
|
---|
| 172 | /** @type {ReturnStringCallback} */
|
---|
| 173 | let shortIdentifier;
|
---|
| 174 | if (typeof module === "string") {
|
---|
| 175 | shortIdentifier =
|
---|
| 176 | /** @type {ReturnStringCallback} */
|
---|
| 177 | (memoize(() => requestShortener.shorten(module)));
|
---|
| 178 | identifier = shortIdentifier;
|
---|
| 179 | moduleId = () => "";
|
---|
| 180 | absoluteResourcePath = () => module.split("!").pop();
|
---|
| 181 | hash = getHash(identifier, hashFunction);
|
---|
| 182 | } else {
|
---|
| 183 | shortIdentifier = memoize(() =>
|
---|
| 184 | module.readableIdentifier(requestShortener)
|
---|
| 185 | );
|
---|
| 186 | identifier =
|
---|
| 187 | /** @type {ReturnStringCallback} */
|
---|
| 188 | (memoize(() => requestShortener.shorten(module.identifier())));
|
---|
| 189 | moduleId =
|
---|
| 190 | /** @type {ReturnStringCallback} */
|
---|
| 191 | (() => chunkGraph.getModuleId(module));
|
---|
| 192 | absoluteResourcePath = () =>
|
---|
| 193 | module instanceof NormalModule
|
---|
| 194 | ? module.resource
|
---|
| 195 | : module.identifier().split("!").pop();
|
---|
| 196 | hash = getHash(identifier, hashFunction);
|
---|
| 197 | }
|
---|
| 198 | const resource =
|
---|
| 199 | /** @type {ReturnStringCallback} */
|
---|
| 200 | (memoize(() => shortIdentifier().split("!").pop()));
|
---|
| 201 |
|
---|
| 202 | const loaders = getBefore(shortIdentifier, "!");
|
---|
| 203 | const allLoaders = getBefore(identifier, "!");
|
---|
| 204 | const query = getAfter(resource, "?");
|
---|
| 205 | const resourcePath = () => {
|
---|
| 206 | const q = query().length;
|
---|
| 207 | return q === 0 ? resource() : resource().slice(0, -q);
|
---|
| 208 | };
|
---|
| 209 | if (typeof opts.moduleFilenameTemplate === "function") {
|
---|
| 210 | return opts.moduleFilenameTemplate(
|
---|
| 211 | lazyObject({
|
---|
| 212 | identifier,
|
---|
| 213 | shortIdentifier,
|
---|
| 214 | resource,
|
---|
| 215 | resourcePath: memoize(resourcePath),
|
---|
| 216 | absoluteResourcePath: memoize(absoluteResourcePath),
|
---|
| 217 | loaders: memoize(loaders),
|
---|
| 218 | allLoaders: memoize(allLoaders),
|
---|
| 219 | query: memoize(query),
|
---|
| 220 | moduleId: memoize(moduleId),
|
---|
| 221 | hash: memoize(hash),
|
---|
| 222 | namespace: () => opts.namespace
|
---|
| 223 | })
|
---|
| 224 | );
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | // TODO webpack 6: consider removing alternatives without dashes
|
---|
| 228 | /** @type {Map<string, function(): string>} */
|
---|
| 229 | const replacements = new Map([
|
---|
| 230 | ["identifier", identifier],
|
---|
| 231 | ["short-identifier", shortIdentifier],
|
---|
| 232 | ["resource", resource],
|
---|
| 233 | ["resource-path", resourcePath],
|
---|
| 234 | // cSpell:words resourcepath
|
---|
| 235 | ["resourcepath", resourcePath],
|
---|
| 236 | ["absolute-resource-path", absoluteResourcePath],
|
---|
| 237 | ["abs-resource-path", absoluteResourcePath],
|
---|
| 238 | // cSpell:words absoluteresource
|
---|
| 239 | ["absoluteresource-path", absoluteResourcePath],
|
---|
| 240 | // cSpell:words absresource
|
---|
| 241 | ["absresource-path", absoluteResourcePath],
|
---|
| 242 | // cSpell:words resourcepath
|
---|
| 243 | ["absolute-resourcepath", absoluteResourcePath],
|
---|
| 244 | // cSpell:words resourcepath
|
---|
| 245 | ["abs-resourcepath", absoluteResourcePath],
|
---|
| 246 | // cSpell:words absoluteresourcepath
|
---|
| 247 | ["absoluteresourcepath", absoluteResourcePath],
|
---|
| 248 | // cSpell:words absresourcepath
|
---|
| 249 | ["absresourcepath", absoluteResourcePath],
|
---|
| 250 | ["all-loaders", allLoaders],
|
---|
| 251 | // cSpell:words allloaders
|
---|
| 252 | ["allloaders", allLoaders],
|
---|
| 253 | ["loaders", loaders],
|
---|
| 254 | ["query", query],
|
---|
| 255 | ["id", moduleId],
|
---|
| 256 | ["hash", hash],
|
---|
| 257 | ["namespace", () => opts.namespace]
|
---|
| 258 | ]);
|
---|
| 259 |
|
---|
| 260 | // TODO webpack 6: consider removing weird double placeholders
|
---|
| 261 | return /** @type {string} */ (opts.moduleFilenameTemplate)
|
---|
| 262 | .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
|
---|
| 263 | .replace(
|
---|
| 264 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
|
---|
| 265 | "[short-identifier]"
|
---|
| 266 | )
|
---|
| 267 | .replace(SQUARE_BRACKET_TAG_REGEXP, (match, content) => {
|
---|
| 268 | if (content.length + 2 === match.length) {
|
---|
| 269 | const replacement = replacements.get(content.toLowerCase());
|
---|
| 270 | if (replacement !== undefined) {
|
---|
| 271 | return replacement();
|
---|
| 272 | }
|
---|
| 273 | } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
|
---|
| 274 | return `[${match.slice(2, -2)}]`;
|
---|
| 275 | }
|
---|
| 276 | return match;
|
---|
| 277 | });
|
---|
| 278 | };
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * Replaces duplicate items in an array with new values generated by a callback function.
|
---|
| 282 | * The callback function is called with the duplicate item, the index of the duplicate item, and the number of times the item has been replaced.
|
---|
| 283 | * The callback function should return the new value for the duplicate item.
|
---|
| 284 | * @template T
|
---|
| 285 | * @param {T[]} array the array with duplicates to be replaced
|
---|
| 286 | * @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
|
---|
| 287 | * @param {(firstElement:T, nextElement:T) => -1 | 0 | 1} [comparator] optional comparator function to sort the duplicate items
|
---|
| 288 | * @returns {T[]} the array with duplicates replaced
|
---|
| 289 | * @example
|
---|
| 290 | * ```js
|
---|
| 291 | * const array = ["a", "b", "c", "a", "b", "a"];
|
---|
| 292 | * const result = ModuleFilenameHelpers.replaceDuplicates(array, (item, index, count) => `${item}-${count}`);
|
---|
| 293 | * // result: ["a-1", "b-1", "c", "a-2", "b-2", "a-3"]
|
---|
| 294 | * ```
|
---|
| 295 | */
|
---|
| 296 | ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
|
---|
| 297 | const countMap = Object.create(null);
|
---|
| 298 | const posMap = Object.create(null);
|
---|
| 299 |
|
---|
| 300 | for (const [idx, item] of array.entries()) {
|
---|
| 301 | countMap[item] = countMap[item] || [];
|
---|
| 302 | countMap[item].push(idx);
|
---|
| 303 | posMap[item] = 0;
|
---|
| 304 | }
|
---|
| 305 | if (comparator) {
|
---|
| 306 | for (const item of Object.keys(countMap)) {
|
---|
| 307 | countMap[item].sort(comparator);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | return array.map((item, i) => {
|
---|
| 311 | if (countMap[item].length > 1) {
|
---|
| 312 | if (comparator && countMap[item][0] === i) return item;
|
---|
| 313 | return fn(item, i, posMap[item]++);
|
---|
| 314 | }
|
---|
| 315 | return item;
|
---|
| 316 | });
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | /**
|
---|
| 320 | * Tests if a string matches a RegExp or an array of RegExp.
|
---|
| 321 | * @param {string} str string to test
|
---|
| 322 | * @param {Matcher} test value which will be used to match against the string
|
---|
| 323 | * @returns {boolean} true, when the RegExp matches
|
---|
| 324 | * @example
|
---|
| 325 | * ```js
|
---|
| 326 | * ModuleFilenameHelpers.matchPart("foo.js", "foo"); // true
|
---|
| 327 | * ModuleFilenameHelpers.matchPart("foo.js", "foo.js"); // true
|
---|
| 328 | * ModuleFilenameHelpers.matchPart("foo.js", "foo."); // false
|
---|
| 329 | * ModuleFilenameHelpers.matchPart("foo.js", "foo*"); // false
|
---|
| 330 | * ModuleFilenameHelpers.matchPart("foo.js", "foo.*"); // true
|
---|
| 331 | * ModuleFilenameHelpers.matchPart("foo.js", /^foo/); // true
|
---|
| 332 | * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
|
---|
| 333 | * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
|
---|
| 334 | * ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, /^bar/]); // true
|
---|
| 335 | * ModuleFilenameHelpers.matchPart("foo.js", [/^baz/, /^bar/]); // false
|
---|
| 336 | * ```
|
---|
| 337 | */
|
---|
| 338 | ModuleFilenameHelpers.matchPart = (str, test) => {
|
---|
| 339 | if (!test) return true;
|
---|
| 340 |
|
---|
| 341 | if (Array.isArray(test)) {
|
---|
| 342 | return test.map(asRegExp).some(regExp => regExp.test(str));
|
---|
| 343 | }
|
---|
| 344 | return asRegExp(test).test(str);
|
---|
| 345 | };
|
---|
| 346 |
|
---|
| 347 | /**
|
---|
| 348 | * Tests if a string matches a match object. The match object can have the following properties:
|
---|
| 349 | * - `test`: a RegExp or an array of RegExp
|
---|
| 350 | * - `include`: a RegExp or an array of RegExp
|
---|
| 351 | * - `exclude`: a RegExp or an array of RegExp
|
---|
| 352 | *
|
---|
| 353 | * The `test` property is tested first, then `include` and then `exclude`.
|
---|
| 354 | * @param {MatchObject} obj a match object to test against the string
|
---|
| 355 | * @param {string} str string to test against the matching object
|
---|
| 356 | * @returns {boolean} true, when the object matches
|
---|
| 357 | * @example
|
---|
| 358 | * ```js
|
---|
| 359 | * ModuleFilenameHelpers.matchObject({ test: "foo.js" }, "foo.js"); // true
|
---|
| 360 | * ModuleFilenameHelpers.matchObject({ test: /^foo/ }, "foo.js"); // true
|
---|
| 361 | * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "foo.js"); // true
|
---|
| 362 | * ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "baz.js"); // false
|
---|
| 363 | * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "foo.js"); // true
|
---|
| 364 | * ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "bar.js"); // false
|
---|
| 365 | * ModuleFilenameHelpers.matchObject({ include: /^foo/ }, "foo.js"); // true
|
---|
| 366 | * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "foo.js"); // true
|
---|
| 367 | * ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "baz.js"); // false
|
---|
| 368 | * ModuleFilenameHelpers.matchObject({ exclude: "foo.js" }, "foo.js"); // false
|
---|
| 369 | * ModuleFilenameHelpers.matchObject({ exclude: [/^foo/, "bar"] }, "foo.js"); // false
|
---|
| 370 | * ```
|
---|
| 371 | */
|
---|
| 372 | ModuleFilenameHelpers.matchObject = (obj, str) => {
|
---|
| 373 | if (obj.test && !ModuleFilenameHelpers.matchPart(str, obj.test)) {
|
---|
| 374 | return false;
|
---|
| 375 | }
|
---|
| 376 | if (obj.include && !ModuleFilenameHelpers.matchPart(str, obj.include)) {
|
---|
| 377 | return false;
|
---|
| 378 | }
|
---|
| 379 | if (obj.exclude && ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
|
---|
| 380 | return false;
|
---|
| 381 | }
|
---|
| 382 | return true;
|
---|
| 383 | };
|
---|