webpack/lib/dependencies/ContextDependencyHelpers.js

271 lines
7.8 KiB
JavaScript
Raw Permalink Normal View History

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";
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 */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2024-03-18 23:28:40 +08:00
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @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 */
/** @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
* @returns {string} Escaped string
2016-11-17 20:43: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
*/
const splitContextFromPrefix = (prefix) => {
const idx = prefix.lastIndexOf("/");
let context = ".";
if (idx >= 0) {
context = prefix.slice(0, idx);
prefix = `.${prefix.slice(idx)}`;
}
return {
context,
prefix
};
};
2022-02-28 20:09:16 +08:00
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
/** @typedef {{ new(options: ContextDependencyOptions, range: Range, valueRange: Range, ...args: any[]): ContextDependency }} ContextDependencyConstructor */
/**
* @param {ContextDependencyConstructor} Dep the Dependency class
2023-05-22 04:31:30 +08:00
* @param {Range} range source range
* @param {BasicEvaluatedExpression} param context param
2025-04-07 21:09:05 +08:00
* @param {Expression} expr expr
* @param {Pick<JavascriptParserOptions, `${"expr"|"wrapped"}Context${"Critical"|"Recursive"|"RegExp"}` | "exprContextRequest">} options options for context creation
* @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
* @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 : "");
2024-03-18 23:28:40 +08:00
const valueRange = /** @type {Range} */ (param.range);
const { context, prefix } = splitContextFromPrefix(prefixRaw);
2021-05-11 15:31:46 +08:00
const {
path: postfix,
query,
fragment
} = parseResource(postfixRaw, parser);
// 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);
const innerRegExp =
2024-03-18 23:28:40 +08:00
/** @type {RegExp} */ (options.wrappedContextRegExp).source +
innerQuasis
2024-03-18 23:28:40 +08:00
.map(
(q) =>
2024-03-18 23:28:40 +08:00
quoteMeta(/** @type {string} */ (q.string)) +
/** @type {RegExp} */ (options.wrappedContextRegExp).source
)
.join("");
2020-07-03 23:03:15 +08:00
// Example: `./context/pre${e}inner${e}inner2${e}post?query#frag`
// 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"
// regExp: /^\.\/pre.*inner.*inner2.*post$/
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
);
const dep = new Dep(
{
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),
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} */
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()) {
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);
if (param.templateStringKind === "cooked") {
value = JSON.stringify(value);
2024-08-02 02:36:27 +08:00
value = value.slice(1, -1);
}
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]
];
value =
(param.templateStringKind === "cooked" ? "`" : "String.raw`") +
value;
2024-03-18 23:28:40 +08:00
} else if (i === parts.length - 1) {
// 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}\``;
} else if (
2018-11-04 04:59:27 +08:00
part.expression &&
part.expression.type === "TemplateElement" &&
part.expression.value.raw === value
) {
// Shortcut when it's a single quasi and doesn't need to be replaced
2024-08-02 02:36:27 +08:00
continue;
}
replaces.push({
range,
value
});
} else {
// Expression
2025-04-07 21:09:05 +08:00
parser.walkExpression(
/** @type {Expression} */
(part.expression)
);
}
2024-08-02 02:36:27 +08:00
}
2018-10-30 19:39:43 +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 : "");
const prefixRange =
2018-02-25 09:00:20 +08:00
param.prefix && param.prefix.isString() ? param.prefix.range : null;
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);
const { context, prefix } = splitContextFromPrefix(prefixRaw);
2021-05-11 15:31:46 +08:00
const {
path: postfix,
query,
fragment
} = parseResource(postfixRaw, parser);
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
);
const dep = new Dep(
{
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),
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);
const replaces = [];
if (prefixRange) {
replaces.push({
range: prefixRange,
value: JSON.stringify(prefix)
});
}
if (postfixRange) {
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";
if (parser && param.wrappedInnerExpressions) {
for (const part of param.wrappedInnerExpressions) {
if (part.expression) {
2025-04-07 21:09:05 +08:00
parser.walkExpression(
/** @type {Expression} */
(part.expression)
);
}
}
}
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
};