2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-04-05 21:00:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-06 23:39:52 +08:00
|
|
|
const { parseResource } = require("../util/identifier");
|
2020-07-03 23:03:15 +08:00
|
|
|
|
2025-04-07 21:09:05 +08:00
|
|
|
/** @typedef {import("estree").Expression} Expression */
|
2021-01-20 22:08:58 +08:00
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
2019-10-22 15:27:52 +08:00
|
|
|
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
|
2019-10-11 21:46:57 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {import("./ContextDependency")} ContextDependency */
|
|
|
|
/** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @typedef {import("./ContextDependency").Replaces} Replaces */
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2016-11-17 20:43:14 +08:00
|
|
|
/**
|
|
|
|
* Escapes regular expression metacharacters
|
2016-11-21 22:38:56 +08:00
|
|
|
* @param {string} str String to quote
|
2018-05-08 20:31:51 +08:00
|
|
|
* @returns {string} Escaped string
|
2016-11-17 20:43:14 +08:00
|
|
|
*/
|
2025-07-17 00:13:14 +08:00
|
|
|
const quoteMeta = (str) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
|
2016-11-17 20:43:14 +08:00
|
|
|
|
2024-03-18 23:28:40 +08:00
|
|
|
/**
|
|
|
|
* @param {string} prefix prefix
|
|
|
|
* @returns {{prefix: string, context: string}} result
|
|
|
|
*/
|
2025-07-17 00:13:14 +08:00
|
|
|
const splitContextFromPrefix = (prefix) => {
|
2018-08-03 15:20:23 +08:00
|
|
|
const idx = prefix.lastIndexOf("/");
|
|
|
|
let context = ".";
|
|
|
|
if (idx >= 0) {
|
2022-03-14 05:54:18 +08:00
|
|
|
context = prefix.slice(0, idx);
|
|
|
|
prefix = `.${prefix.slice(idx)}`;
|
2018-08-03 15:20:23 +08:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
prefix
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-28 20:09:16 +08:00
|
|
|
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
|
2025-04-17 06:54:48 +08:00
|
|
|
/** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: Range, ...args: any[]): ContextDependency }} ContextDependencyConstructor */
|
2019-06-14 17:44:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ContextDependencyConstructor} Dep the Dependency class
|
2023-05-22 04:31:30 +08:00
|
|
|
* @param {Range} range source range
|
2019-06-14 17:44:54 +08:00
|
|
|
* @param {BasicEvaluatedExpression} param context param
|
2025-04-07 21:09:05 +08:00
|
|
|
* @param {Expression} expr expr
|
2021-01-20 22:08:58 +08:00
|
|
|
* @param {Pick<JavascriptParserOptions, `${"expr"|"wrapped"}Context${"Critical"|"Recursive"|"RegExp"}` | "exprContextRequest">} options options for context creation
|
2019-06-14 17:44:54 +08:00
|
|
|
* @param {PartialContextDependencyOptions} contextOptions options for the ContextModule
|
|
|
|
* @param {JavascriptParser} parser the parser
|
2025-04-07 21:09:05 +08:00
|
|
|
* @param {...EXPECTED_ANY} depArgs depArgs
|
2019-06-14 17:44:54 +08:00
|
|
|
* @returns {ContextDependency} the created Dependency
|
|
|
|
*/
|
2024-07-31 04:54:55 +08:00
|
|
|
module.exports.create = (
|
2022-03-28 23:51:48 +08:00
|
|
|
Dep,
|
|
|
|
range,
|
|
|
|
param,
|
|
|
|
expr,
|
|
|
|
options,
|
|
|
|
contextOptions,
|
|
|
|
parser,
|
|
|
|
...depArgs
|
|
|
|
) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (param.isTemplateString()) {
|
2024-03-18 23:28:40 +08:00
|
|
|
const quasis = /** @type {BasicEvaluatedExpression[]} */ (param.quasis);
|
2024-07-31 04:09:42 +08:00
|
|
|
const prefixRaw = /** @type {string} */ (quasis[0].string);
|
|
|
|
const postfixRaw =
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @type {string} */
|
|
|
|
(quasis.length > 1 ? quasis[quasis.length - 1].string : "");
|
2018-11-03 18:14:15 +08:00
|
|
|
|
2024-03-18 23:28:40 +08:00
|
|
|
const valueRange = /** @type {Range} */ (param.range);
|
2018-08-03 15:20:23 +08:00
|
|
|
const { context, prefix } = splitContextFromPrefix(prefixRaw);
|
2021-05-11 15:31:46 +08:00
|
|
|
const {
|
|
|
|
path: postfix,
|
|
|
|
query,
|
|
|
|
fragment
|
|
|
|
} = parseResource(postfixRaw, parser);
|
2018-11-03 18:14:15 +08:00
|
|
|
|
|
|
|
// When there are more than two quasis, the generated RegExp can be more precise
|
|
|
|
// We join the quasis with the expression regexp
|
2024-08-02 02:36:27 +08:00
|
|
|
const innerQuasis = quasis.slice(1, -1);
|
2018-11-03 18:14:15 +08:00
|
|
|
const innerRegExp =
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @type {RegExp} */ (options.wrappedContextRegExp).source +
|
2018-11-03 18:14:15 +08:00
|
|
|
innerQuasis
|
2024-03-18 23:28:40 +08:00
|
|
|
.map(
|
2025-07-17 00:13:14 +08:00
|
|
|
(q) =>
|
2024-03-18 23:28:40 +08:00
|
|
|
quoteMeta(/** @type {string} */ (q.string)) +
|
|
|
|
/** @type {RegExp} */ (options.wrappedContextRegExp).source
|
|
|
|
)
|
2018-11-03 18:14:15 +08:00
|
|
|
.join("");
|
|
|
|
|
2020-07-03 23:03:15 +08:00
|
|
|
// Example: `./context/pre${e}inner${e}inner2${e}post?query#frag`
|
2018-11-03 18:14:15 +08:00
|
|
|
// context: "./context"
|
|
|
|
// prefix: "./pre"
|
|
|
|
// innerQuasis: [BEE("inner"), BEE("inner2")]
|
|
|
|
// (BEE = BasicEvaluatedExpression)
|
|
|
|
// postfix: "post"
|
|
|
|
// query: "?query"
|
2020-07-03 23:03:15 +08:00
|
|
|
// fragment: "#frag"
|
2018-11-03 18:14:15 +08:00
|
|
|
// regExp: /^\.\/pre.*inner.*inner2.*post$/
|
2018-08-03 15:20:23 +08:00
|
|
|
const regExp = new RegExp(
|
2020-03-13 17:25:20 +08:00
|
|
|
`^${quoteMeta(prefix)}${innerRegExp}${quoteMeta(postfix)}$`
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-08-03 15:20:23 +08:00
|
|
|
const dep = new Dep(
|
2019-06-19 19:16:05 +08:00
|
|
|
{
|
2020-07-03 23:03:15 +08:00
|
|
|
request: context + query + fragment,
|
2024-03-18 23:28:40 +08:00
|
|
|
recursive: /** @type {boolean} */ (options.wrappedContextRecursive),
|
2019-06-19 19:16:05 +08:00
|
|
|
regExp,
|
|
|
|
mode: "sync",
|
|
|
|
...contextOptions
|
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
range,
|
2022-03-28 23:51:48 +08:00
|
|
|
valueRange,
|
|
|
|
...depArgs
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2024-03-18 23:28:40 +08:00
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
|
|
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @type {Replaces} */
|
2018-08-03 15:20:23 +08:00
|
|
|
const replaces = [];
|
2024-03-18 23:28:40 +08:00
|
|
|
const parts = /** @type {BasicEvaluatedExpression[]} */ (param.parts);
|
2018-10-30 19:39:43 +08:00
|
|
|
|
2024-08-02 02:36:27 +08:00
|
|
|
for (const [i, part] of parts.entries()) {
|
2018-11-03 18:19:37 +08:00
|
|
|
if (i % 2 === 0) {
|
|
|
|
// Quasis or merged quasi
|
2024-03-18 23:28:40 +08:00
|
|
|
let range = /** @type {Range} */ (part.range);
|
|
|
|
let value = /** @type {string} */ (part.string);
|
2018-11-03 18:19:37 +08:00
|
|
|
if (param.templateStringKind === "cooked") {
|
|
|
|
value = JSON.stringify(value);
|
2024-08-02 02:36:27 +08:00
|
|
|
value = value.slice(1, -1);
|
2018-11-03 18:19:37 +08:00
|
|
|
}
|
|
|
|
if (i === 0) {
|
|
|
|
// prefix
|
|
|
|
value = prefix;
|
2024-03-18 23:28:40 +08:00
|
|
|
range = [
|
|
|
|
/** @type {Range} */ (param.range)[0],
|
|
|
|
/** @type {Range} */ (part.range)[1]
|
|
|
|
];
|
2018-11-03 18:19:37 +08:00
|
|
|
value =
|
|
|
|
(param.templateStringKind === "cooked" ? "`" : "String.raw`") +
|
|
|
|
value;
|
2024-03-18 23:28:40 +08:00
|
|
|
} else if (i === parts.length - 1) {
|
2018-11-03 18:19:37 +08:00
|
|
|
// postfix
|
|
|
|
value = postfix;
|
2024-03-18 23:28:40 +08:00
|
|
|
range = [
|
|
|
|
/** @type {Range} */ (part.range)[0],
|
|
|
|
/** @type {Range} */ (param.range)[1]
|
|
|
|
];
|
2024-07-31 10:39:30 +08:00
|
|
|
value = `${value}\``;
|
2018-11-03 18:19:37 +08:00
|
|
|
} else if (
|
2018-11-04 04:59:27 +08:00
|
|
|
part.expression &&
|
|
|
|
part.expression.type === "TemplateElement" &&
|
|
|
|
part.expression.value.raw === value
|
2018-11-03 18:19:37 +08:00
|
|
|
) {
|
|
|
|
// Shortcut when it's a single quasi and doesn't need to be replaced
|
2024-08-02 02:36:27 +08:00
|
|
|
continue;
|
2018-11-03 18:19:37 +08:00
|
|
|
}
|
|
|
|
replaces.push({
|
|
|
|
range,
|
|
|
|
value
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Expression
|
2025-04-07 21:09:05 +08:00
|
|
|
parser.walkExpression(
|
|
|
|
/** @type {Expression} */
|
|
|
|
(part.expression)
|
|
|
|
);
|
2018-11-03 18:19:37 +08:00
|
|
|
}
|
2024-08-02 02:36:27 +08:00
|
|
|
}
|
2018-10-30 19:39:43 +08:00
|
|
|
|
2018-08-03 15:20:23 +08:00
|
|
|
dep.replaces = replaces;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.critical =
|
|
|
|
options.wrappedContextCritical &&
|
|
|
|
"a part of the request of a dependency is an expression";
|
2016-11-15 21:03:53 +08:00
|
|
|
return dep;
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (
|
|
|
|
param.isWrapped() &&
|
|
|
|
((param.prefix && param.prefix.isString()) ||
|
|
|
|
(param.postfix && param.postfix.isString()))
|
|
|
|
) {
|
2024-07-31 04:09:42 +08:00
|
|
|
const prefixRaw =
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @type {string} */
|
|
|
|
(param.prefix && param.prefix.isString() ? param.prefix.string : "");
|
2024-07-31 04:09:42 +08:00
|
|
|
const postfixRaw =
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @type {string} */
|
|
|
|
(param.postfix && param.postfix.isString() ? param.postfix.string : "");
|
2018-08-03 15:20:23 +08:00
|
|
|
const prefixRange =
|
2018-02-25 09:00:20 +08:00
|
|
|
param.prefix && param.prefix.isString() ? param.prefix.range : null;
|
2018-08-03 15:20:23 +08:00
|
|
|
const postfixRange =
|
|
|
|
param.postfix && param.postfix.isString() ? param.postfix.range : null;
|
2024-03-18 23:28:40 +08:00
|
|
|
const valueRange = /** @type {Range} */ (param.range);
|
2018-08-03 15:20:23 +08:00
|
|
|
const { context, prefix } = splitContextFromPrefix(prefixRaw);
|
2021-05-11 15:31:46 +08:00
|
|
|
const {
|
|
|
|
path: postfix,
|
|
|
|
query,
|
|
|
|
fragment
|
|
|
|
} = parseResource(postfixRaw, parser);
|
2018-08-03 15:20:23 +08:00
|
|
|
const regExp = new RegExp(
|
2024-03-18 23:28:40 +08:00
|
|
|
`^${quoteMeta(prefix)}${
|
|
|
|
/** @type {RegExp} */ (options.wrappedContextRegExp).source
|
|
|
|
}${quoteMeta(postfix)}$`
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-08-03 15:20:23 +08:00
|
|
|
const dep = new Dep(
|
2019-06-19 19:16:05 +08:00
|
|
|
{
|
2020-07-03 23:03:15 +08:00
|
|
|
request: context + query + fragment,
|
2024-03-18 23:28:40 +08:00
|
|
|
recursive: /** @type {boolean} */ (options.wrappedContextRecursive),
|
2019-06-19 19:16:05 +08:00
|
|
|
regExp,
|
|
|
|
mode: "sync",
|
|
|
|
...contextOptions
|
|
|
|
},
|
2018-02-25 09:00:20 +08:00
|
|
|
range,
|
2022-03-28 23:51:48 +08:00
|
|
|
valueRange,
|
|
|
|
...depArgs
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2024-03-18 23:28:40 +08:00
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
2018-08-03 15:20:23 +08:00
|
|
|
const replaces = [];
|
2018-11-05 22:17:10 +08:00
|
|
|
if (prefixRange) {
|
2018-08-03 15:20:23 +08:00
|
|
|
replaces.push({
|
|
|
|
range: prefixRange,
|
|
|
|
value: JSON.stringify(prefix)
|
|
|
|
});
|
|
|
|
}
|
2018-11-05 22:17:10 +08:00
|
|
|
if (postfixRange) {
|
2018-08-03 15:20:23 +08:00
|
|
|
replaces.push({
|
|
|
|
range: postfixRange,
|
|
|
|
value: JSON.stringify(postfix)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dep.replaces = replaces;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.critical =
|
|
|
|
options.wrappedContextCritical &&
|
|
|
|
"a part of the request of a dependency is an expression";
|
2018-11-05 22:17:10 +08:00
|
|
|
|
|
|
|
if (parser && param.wrappedInnerExpressions) {
|
|
|
|
for (const part of param.wrappedInnerExpressions) {
|
2025-07-02 20:10:54 +08:00
|
|
|
if (part.expression) {
|
2025-04-07 21:09:05 +08:00
|
|
|
parser.walkExpression(
|
|
|
|
/** @type {Expression} */
|
|
|
|
(part.expression)
|
|
|
|
);
|
2025-07-02 20:10:54 +08:00
|
|
|
}
|
2018-11-05 22:17:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
return dep;
|
2024-07-31 04:21:27 +08:00
|
|
|
}
|
|
|
|
const dep = new Dep(
|
|
|
|
{
|
|
|
|
request: /** @type {string} */ (options.exprContextRequest),
|
|
|
|
recursive: /** @type {boolean} */ (options.exprContextRecursive),
|
|
|
|
regExp: /** @type {RegExp} */ (options.exprContextRegExp),
|
|
|
|
mode: "sync",
|
|
|
|
...contextOptions
|
|
|
|
},
|
|
|
|
range,
|
|
|
|
/** @type {Range} */ (param.range),
|
|
|
|
...depArgs
|
|
|
|
);
|
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
|
|
|
dep.critical =
|
|
|
|
options.exprContextCritical &&
|
|
|
|
"the request of a dependency is an expression";
|
2018-10-30 19:39:43 +08:00
|
|
|
|
2025-04-07 21:09:05 +08:00
|
|
|
parser.walkExpression(/** @type {Expression} */ (param.expression));
|
2018-10-30 19:39:43 +08:00
|
|
|
|
2024-07-31 04:21:27 +08:00
|
|
|
return dep;
|
2013-01-31 01:49:25 +08:00
|
|
|
};
|