ContextDependencyHelpers.js
6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { parseResource } = require("../util/identifier");
/** @typedef {import("estree").Node} EsTreeNode */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
/** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("./ContextDependency")} ContextDependency */
/** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
/**
* Escapes regular expression metacharacters
* @param {string} str String to quote
* @returns {string} Escaped string
*/
const quoteMeta = str => {
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
};
const splitContextFromPrefix = prefix => {
const idx = prefix.lastIndexOf("/");
let context = ".";
if (idx >= 0) {
context = prefix.slice(0, idx);
prefix = `.${prefix.slice(idx)}`;
}
return {
context,
prefix
};
};
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
/** @typedef {{ new(options: ContextDependencyOptions, range: [number, number], valueRange: [number, number], ...args: any[]): ContextDependency }} ContextDependencyConstructor */
/**
* @param {ContextDependencyConstructor} Dep the Dependency class
* @param {[number, number]} range source range
* @param {BasicEvaluatedExpression} param context param
* @param {EsTreeNode} 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
* @param {...any} depArgs depArgs
* @returns {ContextDependency} the created Dependency
*/
exports.create = (
Dep,
range,
param,
expr,
options,
contextOptions,
parser,
...depArgs
) => {
if (param.isTemplateString()) {
let prefixRaw = param.quasis[0].string;
let postfixRaw =
param.quasis.length > 1
? param.quasis[param.quasis.length - 1].string
: "";
const valueRange = param.range;
const { context, prefix } = splitContextFromPrefix(prefixRaw);
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
const innerQuasis = param.quasis.slice(1, param.quasis.length - 1);
const innerRegExp =
options.wrappedContextRegExp.source +
innerQuasis
.map(q => quoteMeta(q.string) + options.wrappedContextRegExp.source)
.join("");
// 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"
// fragment: "#frag"
// regExp: /^\.\/pre.*inner.*inner2.*post$/
const regExp = new RegExp(
`^${quoteMeta(prefix)}${innerRegExp}${quoteMeta(postfix)}$`
);
const dep = new Dep(
{
request: context + query + fragment,
recursive: options.wrappedContextRecursive,
regExp,
mode: "sync",
...contextOptions
},
range,
valueRange,
...depArgs
);
dep.loc = expr.loc;
const replaces = [];
param.parts.forEach((part, i) => {
if (i % 2 === 0) {
// Quasis or merged quasi
let range = part.range;
let value = part.string;
if (param.templateStringKind === "cooked") {
value = JSON.stringify(value);
value = value.slice(1, value.length - 1);
}
if (i === 0) {
// prefix
value = prefix;
range = [param.range[0], part.range[1]];
value =
(param.templateStringKind === "cooked" ? "`" : "String.raw`") +
value;
} else if (i === param.parts.length - 1) {
// postfix
value = postfix;
range = [part.range[0], param.range[1]];
value = value + "`";
} else if (
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
return;
}
replaces.push({
range,
value
});
} else {
// Expression
parser.walkExpression(part.expression);
}
});
dep.replaces = replaces;
dep.critical =
options.wrappedContextCritical &&
"a part of the request of a dependency is an expression";
return dep;
} else if (
param.isWrapped() &&
((param.prefix && param.prefix.isString()) ||
(param.postfix && param.postfix.isString()))
) {
let prefixRaw =
param.prefix && param.prefix.isString() ? param.prefix.string : "";
let postfixRaw =
param.postfix && param.postfix.isString() ? param.postfix.string : "";
const prefixRange =
param.prefix && param.prefix.isString() ? param.prefix.range : null;
const postfixRange =
param.postfix && param.postfix.isString() ? param.postfix.range : null;
const valueRange = param.range;
const { context, prefix } = splitContextFromPrefix(prefixRaw);
const {
path: postfix,
query,
fragment
} = parseResource(postfixRaw, parser);
const regExp = new RegExp(
`^${quoteMeta(prefix)}${options.wrappedContextRegExp.source}${quoteMeta(
postfix
)}$`
);
const dep = new Dep(
{
request: context + query + fragment,
recursive: options.wrappedContextRecursive,
regExp,
mode: "sync",
...contextOptions
},
range,
valueRange,
...depArgs
);
dep.loc = 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;
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) parser.walkExpression(part.expression);
}
}
return dep;
} else {
const dep = new Dep(
{
request: options.exprContextRequest,
recursive: options.exprContextRecursive,
regExp: /** @type {RegExp} */ (options.exprContextRegExp),
mode: "sync",
...contextOptions
},
range,
param.range,
...depArgs
);
dep.loc = expr.loc;
dep.critical =
options.exprContextCritical &&
"the request of a dependency is an expression";
parser.walkExpression(param.expression);
return dep;
}
};