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
|
|
|
|
2017-01-06 23:11:30 +08:00
|
|
|
"use strict";
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
const { basename, extname } = require("path");
|
2018-10-30 18:50:54 +08:00
|
|
|
const util = require("util");
|
2018-08-23 01:23:48 +08:00
|
|
|
const Module = require("./Module");
|
|
|
|
|
|
|
|
/** @typedef {import("./Compilation").PathData} PathData */
|
2018-11-03 04:05:46 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2018-08-23 01:23:48 +08:00
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
const REGEXP = /\[([a-z]+)(?::(\d+))?\]/gi;
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-09-06 03:20:22 +08:00
|
|
|
const prepareId = id => {
|
|
|
|
if (typeof id !== "string") return id;
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2018-09-06 03:20:22 +08:00
|
|
|
if (/^"\s\+*.*\+\s*"$/.test(id)) {
|
|
|
|
const match = /^"\s\+*\s*(.*)\s*\+\s*"$/.exec(id);
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2018-09-06 03:20:22 +08:00
|
|
|
return `" + (${
|
|
|
|
match[1]
|
|
|
|
} + "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_") + "`;
|
|
|
|
}
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2018-09-06 03:20:22 +08:00
|
|
|
return id.replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
|
|
|
|
};
|
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
const hashLength = (replacer, handler) => {
|
2017-11-09 03:49:41 +08:00
|
|
|
const fn = (match, hashLength, ...args) => {
|
2017-01-06 23:11:30 +08:00
|
|
|
const length = hashLength && parseInt(hashLength, 10);
|
2018-09-28 19:10:37 +08:00
|
|
|
|
|
|
|
if (length && handler) {
|
|
|
|
return handler(length);
|
2014-09-12 01:25:18 +08:00
|
|
|
}
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const hash = replacer(match, hashLength, ...args);
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2014-09-12 01:25:18 +08:00
|
|
|
return length ? hash.slice(0, length) : hash;
|
2014-08-22 19:51:24 +08:00
|
|
|
};
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2017-11-09 03:49:41 +08:00
|
|
|
return fn;
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
const replacer = (value, allowEmpty) => {
|
2017-11-09 03:49:41 +08:00
|
|
|
const fn = (match, ...args) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (value === null || value === undefined) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!allowEmpty) {
|
2018-10-30 18:50:54 +08:00
|
|
|
// last argument in replacer is the entire input string
|
|
|
|
const input = args[args.length - 1];
|
2018-02-25 09:00:20 +08:00
|
|
|
throw new Error(
|
|
|
|
`Path variable ${match} not implemented in this context: ${input}`
|
|
|
|
);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2014-08-22 19:51:24 +08:00
|
|
|
return "";
|
|
|
|
} else {
|
2017-01-06 23:11:30 +08:00
|
|
|
return `${value}`;
|
2014-08-22 19:51:24 +08:00
|
|
|
}
|
|
|
|
};
|
2018-09-28 19:10:37 +08:00
|
|
|
|
2017-11-09 03:49:41 +08:00
|
|
|
return fn;
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
const deprecationCache = new Map();
|
|
|
|
const deprecated = (fn, message) => {
|
|
|
|
let d = deprecationCache.get(message);
|
|
|
|
if (d === undefined) {
|
|
|
|
d = util.deprecate(() => {}, message);
|
|
|
|
deprecationCache.set(message, d);
|
|
|
|
}
|
|
|
|
return (...args) => {
|
|
|
|
d();
|
|
|
|
return fn(...args);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-23 01:23:48 +08:00
|
|
|
/**
|
|
|
|
* @param {string | function(PathData): string} path the raw path
|
|
|
|
* @param {PathData} data context data
|
|
|
|
* @returns {string} the interpolated path
|
|
|
|
*/
|
2017-01-06 23:11:30 +08:00
|
|
|
const replacePathVariables = (path, data) => {
|
2018-08-23 01:23:48 +08:00
|
|
|
const chunkGraph = data.chunkGraph;
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
/** @type {Map<string, Function>} */
|
2018-10-30 17:49:49 +08:00
|
|
|
const replacements = new Map();
|
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
// Filename context
|
|
|
|
//
|
|
|
|
// Placeholders
|
|
|
|
//
|
|
|
|
// for /some/path/file.js?query:
|
|
|
|
// [file] - /some/path/file.js
|
|
|
|
// [query] - ?query
|
|
|
|
// [base] - file.js
|
|
|
|
// [path] - /some/path/
|
|
|
|
// [name] - file
|
|
|
|
// [ext] - .js
|
2018-09-28 19:10:37 +08:00
|
|
|
if (data.filename) {
|
|
|
|
if (typeof data.filename === "string") {
|
|
|
|
const idx = data.filename.indexOf("?");
|
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
let file, query;
|
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
if (idx >= 0) {
|
2018-10-30 18:50:54 +08:00
|
|
|
file = data.filename.substr(0, idx);
|
|
|
|
query = data.filename.substr(idx);
|
2018-09-28 19:10:37 +08:00
|
|
|
} else {
|
2018-10-30 18:50:54 +08:00
|
|
|
file = data.filename;
|
|
|
|
query = "";
|
2018-09-28 19:10:37 +08:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
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);
|
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
replacements.set("file", replacer(file));
|
|
|
|
replacements.set("query", replacer(query, true));
|
2018-11-05 16:13:40 +08:00
|
|
|
replacements.set("path", replacer(path, true));
|
2018-10-30 19:13:09 +08:00
|
|
|
replacements.set("base", replacer(base));
|
|
|
|
replacements.set("name", replacer(name));
|
|
|
|
replacements.set("ext", replacer(ext, true));
|
2018-10-30 18:50:54 +08:00
|
|
|
// Legacy
|
|
|
|
replacements.set(
|
2018-10-30 19:13:09 +08:00
|
|
|
"filebase",
|
2018-10-30 18:50:54 +08:00
|
|
|
deprecated(replacer(base), "[filebase] is now [base]")
|
|
|
|
);
|
2018-09-28 19:10:37 +08:00
|
|
|
}
|
2017-12-20 10:03:15 +08:00
|
|
|
}
|
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
// Compilation context
|
|
|
|
//
|
|
|
|
// Placeholders
|
|
|
|
//
|
2018-11-05 16:13:40 +08:00
|
|
|
// [fullhash] - data.hash (3a4b5c6e7f)
|
|
|
|
//
|
|
|
|
// Legacy Placeholders
|
|
|
|
//
|
2018-10-30 18:50:54 +08:00
|
|
|
// [hash] - data.hash (3a4b5c6e7f)
|
|
|
|
if (data.hash) {
|
|
|
|
const hashReplacer = hashLength(replacer(data.hash), data.hashWithLength);
|
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
replacements.set("fullhash", hashReplacer);
|
2018-10-30 18:50:54 +08:00
|
|
|
|
|
|
|
// Legacy
|
|
|
|
replacements.set(
|
2018-10-30 19:13:09 +08:00
|
|
|
"hash",
|
2018-10-30 18:50:54 +08:00
|
|
|
deprecated(
|
|
|
|
hashReplacer,
|
|
|
|
"[hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)"
|
2018-03-23 02:52:11 +08:00
|
|
|
)
|
2018-10-30 18:50:54 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
// Chunk Context
|
|
|
|
//
|
|
|
|
// Placeholders
|
|
|
|
//
|
|
|
|
// [id] - chunk.id (0.js)
|
|
|
|
// [name] - chunk.name (app.js)
|
|
|
|
// [chunkhash] - chunk.hash (7823t4t4.js)
|
|
|
|
// [contenthash] - chunk.contentHash[type] (3256urzg.js)
|
|
|
|
if (data.chunk) {
|
|
|
|
const chunk = data.chunk;
|
|
|
|
|
|
|
|
const contentHashType = data.contentHashType;
|
2018-10-30 18:50:54 +08:00
|
|
|
|
|
|
|
const idReplacer = replacer(chunk.id);
|
|
|
|
const nameReplacer = replacer(chunk.name || chunk.id);
|
|
|
|
const chunkhashReplacer = hashLength(
|
|
|
|
replacer(chunk.renderedHash || chunk.hash),
|
|
|
|
"hashWithLength" in chunk ? chunk.hashWithLength : undefined
|
2018-10-30 17:49:49 +08:00
|
|
|
);
|
2018-10-30 18:50:54 +08:00
|
|
|
const contenthashReplacer = hashLength(
|
|
|
|
replacer(
|
|
|
|
data.contentHash ||
|
|
|
|
(contentHashType &&
|
|
|
|
chunk.contentHash &&
|
|
|
|
chunk.contentHash[contentHashType])
|
|
|
|
),
|
|
|
|
data.contentHashWithLength ||
|
|
|
|
("contentHashWithLength" in chunk && chunk.contentHashWithLength
|
|
|
|
? chunk.contentHashWithLength[contentHashType]
|
|
|
|
: undefined)
|
2018-10-30 17:49:49 +08:00
|
|
|
);
|
2018-10-30 18:50:54 +08:00
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
replacements.set("id", idReplacer);
|
|
|
|
replacements.set("name", nameReplacer);
|
|
|
|
replacements.set("chunkhash", chunkhashReplacer);
|
|
|
|
replacements.set("contenthash", contenthashReplacer);
|
2018-09-28 19:10:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Module Context
|
|
|
|
//
|
|
|
|
// Placeholders
|
|
|
|
//
|
|
|
|
// [id] - module.id (2.png)
|
|
|
|
// [hash] - module.hash (6237543873.png)
|
|
|
|
//
|
|
|
|
// Legacy Placeholders
|
|
|
|
//
|
|
|
|
// [moduleid] - module.id (2.png)
|
|
|
|
// [modulehash] - module.hash (6237543873.png)
|
|
|
|
if (data.module) {
|
|
|
|
const module = data.module;
|
|
|
|
|
2018-10-30 18:50:54 +08:00
|
|
|
const idReplacer = replacer(
|
|
|
|
prepareId(
|
|
|
|
module instanceof Module ? chunkGraph.getModuleId(module) : module.id
|
2018-02-25 09:00:20 +08:00
|
|
|
)
|
2018-10-30 18:50:54 +08:00
|
|
|
);
|
|
|
|
const hashReplacer = hashLength(
|
|
|
|
replacer(
|
|
|
|
module instanceof Module
|
|
|
|
? chunkGraph.getRenderedModuleHash(module)
|
|
|
|
: module.renderedHash || module.hash
|
|
|
|
),
|
|
|
|
"hashWithLength" in module ? module.hashWithLength : undefined
|
|
|
|
);
|
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
replacements.set("id", idReplacer);
|
|
|
|
replacements.set("hash", hashReplacer);
|
2018-10-30 17:49:49 +08:00
|
|
|
// Legacy
|
2018-10-30 18:50:54 +08:00
|
|
|
replacements.set(
|
2018-10-30 19:13:09 +08:00
|
|
|
"moduleid",
|
2018-10-30 18:50:54 +08:00
|
|
|
deprecated(idReplacer, "[moduleid] is now [id]")
|
|
|
|
);
|
2018-10-30 17:49:49 +08:00
|
|
|
replacements.set(
|
2018-10-30 19:13:09 +08:00
|
|
|
"modulehash",
|
2018-10-30 18:50:54 +08:00
|
|
|
deprecated(hashReplacer, "[modulehash] is now [hash]")
|
2018-09-28 19:10:37 +08:00
|
|
|
);
|
|
|
|
}
|
2018-10-30 17:49:49 +08:00
|
|
|
|
|
|
|
if (typeof path === "function") {
|
|
|
|
path = path(data);
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:13:09 +08:00
|
|
|
path = path.replace(REGEXP, (match, kind, ...args) => {
|
|
|
|
const replacer = replacements.get(kind);
|
|
|
|
if (replacer !== undefined) {
|
|
|
|
return replacer(match, ...args);
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
2018-10-30 17:49:49 +08:00
|
|
|
|
|
|
|
return path;
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
const plugin = "TemplatedPathPlugin";
|
|
|
|
|
2017-01-06 23:11:30 +08:00
|
|
|
class TemplatedPathPlugin {
|
2018-11-03 04:05:46 +08:00
|
|
|
/**
|
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-06 23:11:30 +08:00
|
|
|
apply(compiler) {
|
2018-09-28 19:10:37 +08:00
|
|
|
compiler.hooks.compilation.tap(plugin, compilation => {
|
2017-01-06 23:11:30 +08:00
|
|
|
const mainTemplate = compilation.mainTemplate;
|
2014-08-22 19:51:24 +08:00
|
|
|
|
2018-09-28 19:10:37 +08:00
|
|
|
mainTemplate.hooks.assetPath.tap(plugin, replacePathVariables);
|
2015-06-25 05:17:12 +08:00
|
|
|
});
|
2017-01-06 23:11:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TemplatedPathPlugin;
|