123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- "use strict";
- const mime = require("mime-types");
- const { basename, extname } = require("path");
- const util = require("util");
- const Chunk = require("./Chunk");
- const Module = require("./Module");
- const { parseResource } = require("./util/identifier");
- const REGEXP = /\[\\*([\w:]+)\\*\]/gi;
- const prepareId = id => {
- if (typeof id !== "string") return id;
- if (/^"\s\+*.*\+\s*"$/.test(id)) {
- const match = /^"\s\+*\s*(.*)\s*\+\s*"$/.exec(id);
- return `" + (${
- /** @type {string[]} */ (match)[1]
- } + "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_") + "`;
- }
- return id.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
- };
- const hashLength = (replacer, handler, assetInfo, hashName) => {
-
- const fn = (match, arg, input) => {
- let result;
- const length = arg && parseInt(arg, 10);
- if (length && handler) {
- result = handler(length);
- } else {
- const hash = replacer(match, arg, input);
- result = length ? hash.slice(0, length) : hash;
- }
- if (assetInfo) {
- assetInfo.immutable = true;
- if (Array.isArray(assetInfo[hashName])) {
- assetInfo[hashName] = [...assetInfo[hashName], result];
- } else if (assetInfo[hashName]) {
- assetInfo[hashName] = [assetInfo[hashName], result];
- } else {
- assetInfo[hashName] = result;
- }
- }
- return result;
- };
- return fn;
- };
- const replacer = (value, allowEmpty) => {
-
- const fn = (match, arg, input) => {
- if (typeof value === "function") {
- value = value();
- }
- if (value === null || value === undefined) {
- if (!allowEmpty) {
- throw new Error(
- `Path variable ${match} not implemented in this context: ${input}`
- );
- }
- return "";
- } else {
- return `${value}`;
- }
- };
- return fn;
- };
- const deprecationCache = new Map();
- const deprecatedFunction = (() => () => {})();
- const deprecated = (fn, message, code) => {
- let d = deprecationCache.get(message);
- if (d === undefined) {
- d = util.deprecate(deprecatedFunction, message, code);
- deprecationCache.set(message, d);
- }
- return (...args) => {
- d();
- return fn(...args);
- };
- };
- const replacePathVariables = (path, data, assetInfo) => {
- const chunkGraph = data.chunkGraph;
-
- const replacements = new Map();
-
-
-
-
-
-
-
-
-
-
-
-
- if (typeof data.filename === "string") {
-
- let match = data.filename.match(/^data:([^;,]+)/);
- if (match) {
- const ext = mime.extension(match[1]);
- const emptyReplacer = replacer("", true);
- replacements.set("file", emptyReplacer);
- replacements.set("query", emptyReplacer);
- replacements.set("fragment", emptyReplacer);
- replacements.set("path", emptyReplacer);
- replacements.set("base", emptyReplacer);
- replacements.set("name", emptyReplacer);
- replacements.set("ext", replacer(ext ? `.${ext}` : "", true));
-
- replacements.set(
- "filebase",
- deprecated(
- emptyReplacer,
- "[filebase] is now [base]",
- "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
- )
- );
- } else {
- const { path: file, query, fragment } = parseResource(data.filename);
- const ext = extname(file);
- const base = basename(file);
- const name = base.slice(0, base.length - ext.length);
- const path = file.slice(0, file.length - base.length);
- replacements.set("file", replacer(file));
- replacements.set("query", replacer(query, true));
- replacements.set("fragment", replacer(fragment, true));
- replacements.set("path", replacer(path, true));
- replacements.set("base", replacer(base));
- replacements.set("name", replacer(name));
- replacements.set("ext", replacer(ext, true));
-
- replacements.set(
- "filebase",
- deprecated(
- replacer(base),
- "[filebase] is now [base]",
- "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
- )
- );
- }
- }
-
-
-
-
-
-
-
-
-
- if (data.hash) {
- const hashReplacer = hashLength(
- replacer(data.hash),
- data.hashWithLength,
- assetInfo,
- "fullhash"
- );
- replacements.set("fullhash", hashReplacer);
-
- replacements.set(
- "hash",
- deprecated(
- hashReplacer,
- "[hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)",
- "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH"
- )
- );
- }
-
-
-
-
-
-
-
-
- if (data.chunk) {
- const chunk = data.chunk;
- const contentHashType = data.contentHashType;
- const idReplacer = replacer(chunk.id);
- const nameReplacer = replacer(chunk.name || chunk.id);
- const chunkhashReplacer = hashLength(
- replacer(chunk instanceof Chunk ? chunk.renderedHash : chunk.hash),
- "hashWithLength" in chunk ? chunk.hashWithLength : undefined,
- assetInfo,
- "chunkhash"
- );
- const contenthashReplacer = hashLength(
- replacer(
- data.contentHash ||
- (contentHashType &&
- chunk.contentHash &&
- chunk.contentHash[contentHashType])
- ),
- data.contentHashWithLength ||
- ("contentHashWithLength" in chunk && chunk.contentHashWithLength
- ? chunk.contentHashWithLength[ (contentHashType)]
- : undefined),
- assetInfo,
- "contenthash"
- );
- replacements.set("id", idReplacer);
- replacements.set("name", nameReplacer);
- replacements.set("chunkhash", chunkhashReplacer);
- replacements.set("contenthash", contenthashReplacer);
- }
-
-
-
-
-
-
-
-
-
-
-
- if (data.module) {
- const module = data.module;
- const idReplacer = replacer(() =>
- prepareId(
- module instanceof Module
- ? (chunkGraph).getModuleId(module)
- : module.id
- )
- );
- const moduleHashReplacer = hashLength(
- replacer(() =>
- module instanceof Module
- ? (chunkGraph).getRenderedModuleHash(
- module,
- data.runtime
- )
- : module.hash
- ),
- "hashWithLength" in module ? module.hashWithLength : undefined,
- assetInfo,
- "modulehash"
- );
- const contentHashReplacer = hashLength(
- replacer( (data.contentHash)),
- undefined,
- assetInfo,
- "contenthash"
- );
- replacements.set("id", idReplacer);
- replacements.set("modulehash", moduleHashReplacer);
- replacements.set("contenthash", contentHashReplacer);
- replacements.set(
- "hash",
- data.contentHash ? contentHashReplacer : moduleHashReplacer
- );
-
- replacements.set(
- "moduleid",
- deprecated(
- idReplacer,
- "[moduleid] is now [id]",
- "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_MODULE_ID"
- )
- );
- }
-
- if (data.url) {
- replacements.set("url", replacer(data.url));
- }
- if (typeof data.runtime === "string") {
- replacements.set(
- "runtime",
- replacer(() => prepareId( (data.runtime)))
- );
- } else {
- replacements.set("runtime", replacer("_"));
- }
- if (typeof path === "function") {
- path = path(data, assetInfo);
- }
- path = path.replace(REGEXP, (match, content) => {
- if (content.length + 2 === match.length) {
- const contentMatch = /^(\w+)(?::(\w+))?$/.exec(content);
- if (!contentMatch) return match;
- const [, kind, arg] = contentMatch;
- const replacer = replacements.get(kind);
- if (replacer !== undefined) {
- return replacer(match, arg, path);
- }
- } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
- return `[${match.slice(2, -2)}]`;
- }
- return match;
- });
- return path;
- };
- const plugin = "TemplatedPathPlugin";
- class TemplatedPathPlugin {
-
- apply(compiler) {
- compiler.hooks.compilation.tap(plugin, compilation => {
- compilation.hooks.assetPath.tap(plugin, replacePathVariables);
- });
- }
- }
- module.exports = TemplatedPathPlugin;
|