webpack/lib/TemplatedPathPlugin.js

196 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-08-22 19:51:24 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Jason Anderson @diurnalist
*/
2018-07-30 23:08:51 +08:00
"use strict";
2014-08-22 19:51:24 +08:00
const Module = require("./Module");
/** @typedef {import("./Compilation").PathData} PathData */
const REGEXP_HASH = /\[hash(?::(\d+))?\]/gi,
2014-08-22 19:51:24 +08:00
REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/gi,
2017-10-30 20:56:57 +08:00
REGEXP_MODULEHASH = /\[modulehash(?::(\d+))?\]/gi,
2018-03-23 02:52:11 +08:00
REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/gi,
2014-08-22 19:51:24 +08:00
REGEXP_NAME = /\[name\]/gi,
REGEXP_ID = /\[id\]/gi,
2017-10-30 20:56:57 +08:00
REGEXP_MODULEID = /\[moduleid\]/gi,
2014-08-22 19:51:24 +08:00
REGEXP_FILE = /\[file\]/gi,
REGEXP_QUERY = /\[query\]/gi,
REGEXP_FILEBASE = /\[filebase\]/gi;
2014-09-24 05:24:49 +08:00
// Using global RegExp for .test is dangerous
// We use a normal RegExp instead of .test
const REGEXP_HASH_FOR_TEST = new RegExp(REGEXP_HASH.source, "i"),
2014-09-24 05:24:49 +08:00
REGEXP_CHUNKHASH_FOR_TEST = new RegExp(REGEXP_CHUNKHASH.source, "i"),
2018-03-23 02:52:11 +08:00
REGEXP_CONTENTHASH_FOR_TEST = new RegExp(REGEXP_CONTENTHASH.source, "i"),
2014-09-24 05:24:49 +08:00
REGEXP_NAME_FOR_TEST = new RegExp(REGEXP_NAME.source, "i");
const withHashLength = (replacer, handlerFn) => {
const fn = (match, hashLength, ...args) => {
const length = hashLength && parseInt(hashLength, 10);
2018-02-25 09:00:20 +08:00
if (length && handlerFn) {
return handlerFn(length);
}
2017-11-08 18:32:05 +08:00
const hash = replacer(match, hashLength, ...args);
return length ? hash.slice(0, length) : hash;
2014-08-22 19:51:24 +08:00
};
return fn;
2017-01-11 17:51:58 +08:00
};
2014-08-22 19:51:24 +08:00
const getReplacer = (value, allowEmpty) => {
const fn = (match, ...args) => {
2014-08-22 19:51:24 +08:00
// last argument in replacer is the entire input string
2017-11-08 18:32:05 +08:00
const input = args[args.length - 1];
2018-02-25 09:00:20 +08:00
if (value === null || value === undefined) {
if (!allowEmpty) {
2018-02-25 09:00:20 +08:00
throw new Error(
`Path variable ${match} not implemented in this context: ${input}`
);
}
2014-08-22 19:51:24 +08:00
return "";
} else {
return `${value}`;
2014-08-22 19:51:24 +08:00
}
};
return fn;
2017-01-11 17:51:58 +08:00
};
2014-08-22 19:51:24 +08:00
/**
* @param {string | function(PathData): string} path the raw path
* @param {PathData} data context data
* @returns {string} the interpolated path
*/
const replacePathVariables = (path, data) => {
const chunkGraph = data.chunkGraph;
const chunk = data.chunk;
const chunkId = chunk && chunk.id;
const chunkName = chunk && (chunk.name || chunk.id);
const chunkHash = chunk && (chunk.renderedHash || chunk.hash);
const chunkHashWithLength =
chunk && "hashWithLength" in chunk && chunk.hashWithLength;
2018-03-23 02:52:11 +08:00
const contentHashType = data.contentHashType;
const contentHash =
data.contentHash ||
2018-03-23 02:52:11 +08:00
(chunk &&
contentHashType &&
chunk.contentHash &&
chunk.contentHash[contentHashType]);
const contentHashWithLength =
data.contentHashWithLength ||
(chunk && "contentHashWithLength" in chunk
? chunk.contentHashWithLength[contentHashType]
: undefined);
2017-10-30 20:56:57 +08:00
const module = data.module;
const moduleId =
module &&
(module instanceof Module ? chunkGraph.getModuleId(module) : module.id);
const moduleHash =
module &&
(module instanceof Module
? chunkGraph.getRenderedModuleHash(module)
: module.renderedHash || module.hash);
const moduleHashWithLength =
module && "hashWithLength" in module && module.hashWithLength;
2014-08-22 19:51:24 +08:00
2018-02-25 09:00:20 +08:00
if (typeof path === "function") {
path = path(data);
}
2018-03-23 02:52:11 +08:00
if (
data.noChunkHash &&
(REGEXP_CHUNKHASH_FOR_TEST.test(path) ||
REGEXP_CONTENTHASH_FOR_TEST.test(path))
) {
2018-02-25 09:00:20 +08:00
throw new Error(
2018-03-26 22:56:10 +08:00
`Cannot use [chunkhash] or [contenthash] for chunk in '${path}' (use [hash] instead)`
2018-02-25 09:00:20 +08:00
);
}
2018-02-25 09:00:20 +08:00
return (
path
.replace(
REGEXP_HASH,
withHashLength(getReplacer(data.hash), data.hashWithLength)
)
.replace(
REGEXP_CHUNKHASH,
withHashLength(getReplacer(chunkHash), chunkHashWithLength)
)
2018-03-23 02:52:11 +08:00
.replace(
REGEXP_CONTENTHASH,
withHashLength(getReplacer(contentHash), contentHashWithLength)
)
2018-02-25 09:00:20 +08:00
.replace(
REGEXP_MODULEHASH,
withHashLength(getReplacer(moduleHash), moduleHashWithLength)
)
.replace(REGEXP_ID, getReplacer(chunkId))
.replace(REGEXP_MODULEID, getReplacer(moduleId))
.replace(REGEXP_NAME, getReplacer(chunkName))
.replace(REGEXP_FILE, getReplacer(data.filename))
.replace(REGEXP_FILEBASE, getReplacer(data.basename))
// query is optional, it's OK if it's in a path but there's nothing to replace it with
.replace(REGEXP_QUERY, getReplacer(data.query, true))
);
2017-01-11 17:51:58 +08:00
};
2014-08-22 19:51:24 +08:00
class TemplatedPathPlugin {
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap("TemplatedPathPlugin", compilation => {
const mainTemplate = compilation.mainTemplate;
2014-08-22 19:51:24 +08:00
2018-02-25 09:00:20 +08:00
mainTemplate.hooks.assetPath.tap(
"TemplatedPathPlugin",
replacePathVariables
);
2014-08-22 19:51:24 +08:00
2018-02-25 09:00:20 +08:00
mainTemplate.hooks.globalHash.tap(
"TemplatedPathPlugin",
(chunk, paths) => {
const outputOptions = mainTemplate.outputOptions;
const publicPath = outputOptions.publicPath || "";
const filename = outputOptions.filename || "";
const chunkFilename =
outputOptions.chunkFilename || outputOptions.filename;
if (
REGEXP_HASH_FOR_TEST.test(publicPath) ||
REGEXP_CHUNKHASH_FOR_TEST.test(publicPath) ||
2018-03-23 02:52:11 +08:00
REGEXP_CONTENTHASH_FOR_TEST.test(publicPath) ||
2018-02-25 09:00:20 +08:00
REGEXP_NAME_FOR_TEST.test(publicPath)
)
return true;
if (REGEXP_HASH_FOR_TEST.test(filename)) return true;
if (REGEXP_HASH_FOR_TEST.test(chunkFilename)) return true;
if (REGEXP_HASH_FOR_TEST.test(paths.join("|"))) return true;
}
);
2018-02-25 09:00:20 +08:00
mainTemplate.hooks.hashForChunk.tap(
"TemplatedPathPlugin",
(hash, chunk) => {
const outputOptions = mainTemplate.outputOptions;
const chunkFilename =
outputOptions.chunkFilename || outputOptions.filename;
if (REGEXP_CHUNKHASH_FOR_TEST.test(chunkFilename)) {
2018-02-25 09:00:20 +08:00
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
}
2018-03-23 02:52:11 +08:00
if (REGEXP_CONTENTHASH_FOR_TEST.test(chunkFilename)) {
hash.update(
JSON.stringify(
chunk.getChunkMaps(true).contentHash.javascript || {}
)
2018-03-23 02:52:11 +08:00
);
}
if (REGEXP_NAME_FOR_TEST.test(chunkFilename)) {
2018-02-25 09:00:20 +08:00
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
}
2018-02-25 09:00:20 +08:00
}
);
});
}
}
module.exports = TemplatedPathPlugin;