extension-develop 3.15.1 → 3.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/0~branding.mjs +26 -0
- package/dist/0~dev-server.mjs +488 -0
- package/dist/0~rslib-runtime.mjs +40 -0
- package/dist/0~rspack-config.mjs +8648 -0
- package/dist/0~stats-handler.mjs +25 -0
- package/dist/0~zip.mjs +34 -0
- package/dist/314.mjs +35 -0
- package/dist/526.mjs +23 -0
- package/dist/946.mjs +1670 -0
- package/dist/962.mjs +1389 -0
- package/dist/ensure-hmr-for-scripts.js +12 -56
- package/dist/{ensure-hmr-for-scripts.cjs → ensure-hmr-for-scripts.mjs} +12 -56
- package/dist/feature-scripts-content-script-wrapper.js +23 -98
- package/dist/{feature-scripts-content-script-wrapper.cjs → feature-scripts-content-script-wrapper.mjs} +23 -98
- package/dist/main-world-bridge.js +0 -18
- package/dist/{main-world-bridge.cjs → main-world-bridge.mjs} +0 -18
- package/dist/minimum-chromium-file.js +0 -5
- package/dist/minimum-chromium-file.mjs +5 -0
- package/dist/minimum-firefox-file.js +0 -5
- package/dist/minimum-firefox-file.mjs +5 -0
- package/dist/minimum-script-file.js +0 -18
- package/dist/{minimum-script-file.cjs → minimum-script-file.mjs} +0 -18
- package/dist/module.mjs +3 -0
- package/dist/package.json +1 -1
- package/dist/preact-refresh-shim.mjs +7 -0
- package/dist/preview.mjs +2 -0
- package/dist/resolve-paths-loader.js +1043 -1093
- package/dist/resolve-paths-loader.mjs +1300 -0
- package/package.json +8 -8
- package/dist/221.cjs +0 -513
- package/dist/442.cjs +0 -9226
- package/dist/504.cjs +0 -65
- package/dist/787.cjs +0 -44
- package/dist/minimum-chromium-file.cjs +0 -10
- package/dist/minimum-firefox-file.cjs +0 -10
- package/dist/module.cjs +0 -3479
- package/dist/preact-refresh-shim.cjs +0 -25
- package/dist/preview.cjs +0 -1464
- package/dist/resolve-paths-loader.cjs +0 -1350
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
import { createRequire as __extjsCreateRequire } from "node:module"; const require = __extjsCreateRequire(import.meta.url);
|
|
2
|
+
import magic_string from "magic-string";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import { __webpack_require__ } from "./0~rslib-runtime.mjs";
|
|
5
|
+
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
6
|
+
import * as __rspack_external__swc_core_81ba23c0 from "@swc/core";
|
|
7
|
+
import * as __rspack_external_path from "path";
|
|
8
|
+
import * as __rspack_external_fs from "fs";
|
|
9
|
+
__webpack_require__.add({
|
|
10
|
+
"@swc/core?1153" (module) {
|
|
11
|
+
module.exports = __rspack_external__swc_core_81ba23c0;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
function getFilename(feature, filePath) {
|
|
15
|
+
const entryExt = __rspack_external_path.extname(filePath);
|
|
16
|
+
let fileOutputpath = feature;
|
|
17
|
+
if ([
|
|
18
|
+
'.js',
|
|
19
|
+
'.jsx',
|
|
20
|
+
'.tsx',
|
|
21
|
+
'.ts'
|
|
22
|
+
].includes(entryExt)) fileOutputpath = fileOutputpath.replace(entryExt, '.js');
|
|
23
|
+
if ([
|
|
24
|
+
'.html',
|
|
25
|
+
'.njk',
|
|
26
|
+
'.nunjucks'
|
|
27
|
+
].includes(entryExt)) fileOutputpath = fileOutputpath.replace(entryExt, '.html');
|
|
28
|
+
if ([
|
|
29
|
+
'.css',
|
|
30
|
+
'.scss',
|
|
31
|
+
'.sass',
|
|
32
|
+
'.less'
|
|
33
|
+
].includes(entryExt)) fileOutputpath = fileOutputpath.replace(entryExt, '.css');
|
|
34
|
+
return unixify(fileOutputpath || '');
|
|
35
|
+
}
|
|
36
|
+
function unixify(filePath) {
|
|
37
|
+
return filePath.replace(/\\/g, '/');
|
|
38
|
+
}
|
|
39
|
+
function memberChainFromCallee(callee) {
|
|
40
|
+
const memberParts = [];
|
|
41
|
+
let currentExpression = callee;
|
|
42
|
+
while(currentExpression && 'ParenExpression' === currentExpression.type)currentExpression = currentExpression.expression;
|
|
43
|
+
while(currentExpression){
|
|
44
|
+
if ('Identifier' === currentExpression.type) {
|
|
45
|
+
memberParts.unshift(currentExpression.value);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
if ('MemberExpression' === currentExpression.type) {
|
|
49
|
+
const propertyNode = currentExpression.property;
|
|
50
|
+
if (propertyNode?.type === 'Identifier') memberParts.unshift(propertyNode.value);
|
|
51
|
+
currentExpression = currentExpression.object;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return memberParts;
|
|
57
|
+
}
|
|
58
|
+
function getPropName(propertyNode) {
|
|
59
|
+
if (!propertyNode || 'KeyValueProperty' !== propertyNode.type) return;
|
|
60
|
+
const keyNode = propertyNode.key;
|
|
61
|
+
if (keyNode?.type === 'Identifier') return keyNode.value;
|
|
62
|
+
if (keyNode?.type === 'StringLiteral') return keyNode.value;
|
|
63
|
+
}
|
|
64
|
+
function isHttpLike(input) {
|
|
65
|
+
return /^https?:\/\//i.test(input) || /^data:/i.test(input) || /^chrome:\/\//i.test(input) || /^moz-extension:\/\//i.test(input);
|
|
66
|
+
}
|
|
67
|
+
function hasGlob(input) {
|
|
68
|
+
return /[*?\[\]{}]/.test(input);
|
|
69
|
+
}
|
|
70
|
+
function resolveLiteralToOutput(literal, opts) {
|
|
71
|
+
const { manifestPath, packageJsonDir, authorFilePath } = opts;
|
|
72
|
+
const root = packageJsonDir || __rspack_external_path.dirname(manifestPath);
|
|
73
|
+
const canonicalUnixPath = unixify(literal || '');
|
|
74
|
+
if (!literal || isHttpLike(literal) || hasGlob(literal)) return;
|
|
75
|
+
if (/^\/public\//i.test(canonicalUnixPath)) {
|
|
76
|
+
const out = canonicalUnixPath.replace(/^\/public\//i, '');
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
if (/^(?:\.\/)?public\//i.test(canonicalUnixPath)) {
|
|
80
|
+
const out = canonicalUnixPath.replace(/^(?:\.\/)?public\//i, '');
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
if (/^\//.test(canonicalUnixPath)) {
|
|
84
|
+
const trimmed = canonicalUnixPath.replace(/^\//, '');
|
|
85
|
+
if (/^(?:pages|scripts)\//i.test(trimmed)) {
|
|
86
|
+
const abs = __rspack_external_path.join(root, trimmed);
|
|
87
|
+
return unixify(getFilename(trimmed, abs));
|
|
88
|
+
}
|
|
89
|
+
return trimmed;
|
|
90
|
+
}
|
|
91
|
+
const baseDir = /^(\.\/|\.\.\/)/.test(canonicalUnixPath) && authorFilePath ? __rspack_external_path.dirname(authorFilePath) : root;
|
|
92
|
+
const candidateAbsolutePath = __rspack_external_path.isAbsolute(literal) ? literal : __rspack_external_path.resolve(baseDir, literal);
|
|
93
|
+
const pathWithoutDotPrefix = canonicalUnixPath.replace(/^\.\//, '');
|
|
94
|
+
if (/^pages\//i.test(pathWithoutDotPrefix)) return unixify(getFilename(pathWithoutDotPrefix, candidateAbsolutePath));
|
|
95
|
+
if (/^scripts\//i.test(pathWithoutDotPrefix)) return unixify(getFilename(pathWithoutDotPrefix, candidateAbsolutePath));
|
|
96
|
+
try {
|
|
97
|
+
const normalizedRel = unixify(__rspack_external_path.relative(root, candidateAbsolutePath));
|
|
98
|
+
const relNoDotPrefix = normalizedRel.replace(/^\.\//, '');
|
|
99
|
+
if (/^public\//i.test(relNoDotPrefix)) return relNoDotPrefix.replace(/^public\//i, '');
|
|
100
|
+
if (/^pages\//i.test(relNoDotPrefix)) return unixify(getFilename(relNoDotPrefix, candidateAbsolutePath));
|
|
101
|
+
if (/^scripts\//i.test(relNoDotPrefix)) return unixify(getFilename(relNoDotPrefix, candidateAbsolutePath));
|
|
102
|
+
} catch {}
|
|
103
|
+
}
|
|
104
|
+
function isStringLiteral(node) {
|
|
105
|
+
return node && 'StringLiteral' === node.type && 'string' == typeof node.value;
|
|
106
|
+
}
|
|
107
|
+
function isStaticTemplate(node) {
|
|
108
|
+
return node && 'TemplateLiteral' === node.type && Array.isArray(node.expressions) && 0 === node.expressions.length;
|
|
109
|
+
}
|
|
110
|
+
function evalStaticString(node) {
|
|
111
|
+
if (!node) return;
|
|
112
|
+
if (node && 'ParenExpression' === node.type) return evalStaticString(node.expression);
|
|
113
|
+
if (isStringLiteral(node)) return node.value;
|
|
114
|
+
if (isStaticTemplate(node)) return node.quasis?.map((quasi)=>quasi.raw ?? '').join('');
|
|
115
|
+
if (node && 'BinaryExpression' === node.type && '+' === node.operator) {
|
|
116
|
+
const left = evalStaticString(node.left);
|
|
117
|
+
const right = evalStaticString(node.right);
|
|
118
|
+
if ('string' == typeof left && 'string' == typeof right) return left + right;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function handleRuntimeCalls(node, source, rewriteValue, manifestPath) {
|
|
122
|
+
if ('CallExpression' !== node.type) return false;
|
|
123
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
124
|
+
const apiRoot = memberChain[0];
|
|
125
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
126
|
+
const method = memberChain.join('.');
|
|
127
|
+
if ((method.endsWith('runtime.getURL') || method.endsWith('extension.getURL')) && node.arguments?.[0]) {
|
|
128
|
+
const firstArgument = node.arguments[0].expression;
|
|
129
|
+
if (isStringLiteral(firstArgument)) {
|
|
130
|
+
const resolved = resolveLiteralToOutput(firstArgument.value, {
|
|
131
|
+
manifestPath
|
|
132
|
+
});
|
|
133
|
+
rewriteValue(firstArgument, resolved, firstArgument.value);
|
|
134
|
+
} else if (isStaticTemplate(firstArgument)) {
|
|
135
|
+
const rawTemplate = source.slice(firstArgument.span.start + 1, firstArgument.span.end - 1);
|
|
136
|
+
const resolved = resolveLiteralToOutput(rawTemplate, {
|
|
137
|
+
manifestPath
|
|
138
|
+
});
|
|
139
|
+
rewriteValue(firstArgument, resolved, rawTemplate);
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
function handleActionAndPopup(node, source, rewriteValue, manifestPath) {
|
|
146
|
+
if ('CallExpression' !== node.type) return false;
|
|
147
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
148
|
+
const apiRoot = memberChain[0];
|
|
149
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
150
|
+
const method = memberChain.join('.');
|
|
151
|
+
if (method.endsWith('action.setIcon') || method.endsWith('pageAction.setIcon') || method.endsWith('browserAction.setIcon')) {
|
|
152
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
153
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
154
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
155
|
+
const keyName = getPropName(propertyNode);
|
|
156
|
+
if ('path' !== keyName) continue;
|
|
157
|
+
const valueNode = propertyNode.value;
|
|
158
|
+
if (isStringLiteral(valueNode)) {
|
|
159
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
160
|
+
manifestPath
|
|
161
|
+
});
|
|
162
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
163
|
+
} else if (valueNode?.type === 'ObjectExpression') {
|
|
164
|
+
for (const innerProperty of valueNode.properties || [])if ('KeyValueProperty' === innerProperty.type && isStringLiteral(innerProperty.value)) {
|
|
165
|
+
const resolved = resolveLiteralToOutput(innerProperty.value.value, {
|
|
166
|
+
manifestPath
|
|
167
|
+
});
|
|
168
|
+
rewriteValue(innerProperty.value, resolved, innerProperty.value.value);
|
|
169
|
+
} else if ('KeyValueProperty' === innerProperty.type && isStaticTemplate(innerProperty.value)) {
|
|
170
|
+
const raw = source.slice(innerProperty.value.span.start + 1, innerProperty.value.span.end - 1);
|
|
171
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
172
|
+
manifestPath
|
|
173
|
+
});
|
|
174
|
+
rewriteValue(innerProperty.value, resolved, raw);
|
|
175
|
+
} else if ('KeyValueProperty' === innerProperty.type) {
|
|
176
|
+
const raw = evalStaticString(innerProperty.value);
|
|
177
|
+
if ('string' == typeof raw) {
|
|
178
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
179
|
+
manifestPath
|
|
180
|
+
});
|
|
181
|
+
const originalExpr = source.slice(innerProperty.value.span.start, innerProperty.value.span.end);
|
|
182
|
+
rewriteValue(innerProperty.value, resolved, originalExpr);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
186
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
187
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
188
|
+
manifestPath
|
|
189
|
+
});
|
|
190
|
+
rewriteValue(valueNode, resolved, raw);
|
|
191
|
+
} else {
|
|
192
|
+
const raw = evalStaticString(valueNode);
|
|
193
|
+
if ('string' == typeof raw) {
|
|
194
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
195
|
+
manifestPath
|
|
196
|
+
});
|
|
197
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
198
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (method.endsWith('action.setPopup') || method.endsWith('pageAction.setPopup') || method.endsWith('browserAction.setPopup')) {
|
|
205
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
206
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
207
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
208
|
+
const keyName = getPropName(propertyNode);
|
|
209
|
+
if ('popup' !== keyName) continue;
|
|
210
|
+
const valueNode = propertyNode.value;
|
|
211
|
+
if (isStringLiteral(valueNode)) {
|
|
212
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
213
|
+
manifestPath
|
|
214
|
+
});
|
|
215
|
+
rewriteValue(valueNode, resolved);
|
|
216
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
217
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
218
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
219
|
+
manifestPath
|
|
220
|
+
});
|
|
221
|
+
rewriteValue(valueNode, resolved);
|
|
222
|
+
} else {
|
|
223
|
+
const raw = evalStaticString(valueNode);
|
|
224
|
+
if ('string' == typeof raw) {
|
|
225
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
226
|
+
manifestPath
|
|
227
|
+
});
|
|
228
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
229
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
function handleDevtools(node, source, rewriteValue, manifestPath) {
|
|
238
|
+
if ('CallExpression' !== node.type) return false;
|
|
239
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
240
|
+
const apiRoot = memberChain[0];
|
|
241
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
242
|
+
const method = memberChain.join('.');
|
|
243
|
+
if (method.endsWith('devtools.panels.create')) {
|
|
244
|
+
const iconArg = node.arguments?.[1]?.expression;
|
|
245
|
+
const pageArg = node.arguments?.[2]?.expression;
|
|
246
|
+
let touched = false;
|
|
247
|
+
if (isStringLiteral(iconArg)) {
|
|
248
|
+
const resolved = resolveLiteralToOutput(iconArg.value, {
|
|
249
|
+
manifestPath
|
|
250
|
+
});
|
|
251
|
+
rewriteValue(iconArg, resolved, iconArg.value);
|
|
252
|
+
touched = true;
|
|
253
|
+
} else if (isStaticTemplate(iconArg)) {
|
|
254
|
+
const rawTemplate = source.slice(iconArg.span.start + 1, iconArg.span.end - 1);
|
|
255
|
+
const resolved = resolveLiteralToOutput(rawTemplate, {
|
|
256
|
+
manifestPath
|
|
257
|
+
});
|
|
258
|
+
rewriteValue(iconArg, resolved, rawTemplate);
|
|
259
|
+
} else {
|
|
260
|
+
const staticallyEvaluated = evalStaticString(iconArg);
|
|
261
|
+
if ('string' == typeof staticallyEvaluated) {
|
|
262
|
+
const resolved = resolveLiteralToOutput(staticallyEvaluated, {
|
|
263
|
+
manifestPath
|
|
264
|
+
});
|
|
265
|
+
const originalExpr = source.slice(iconArg.span.start, iconArg.span.end);
|
|
266
|
+
rewriteValue(iconArg, resolved, originalExpr);
|
|
267
|
+
touched = true;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (isStringLiteral(pageArg)) {
|
|
271
|
+
const resolved = resolveLiteralToOutput(pageArg.value, {
|
|
272
|
+
manifestPath
|
|
273
|
+
});
|
|
274
|
+
rewriteValue(pageArg, resolved, pageArg.value);
|
|
275
|
+
touched = true;
|
|
276
|
+
} else if (isStaticTemplate(pageArg)) {
|
|
277
|
+
const rawTemplate = source.slice(pageArg.span.start + 1, pageArg.span.end - 1);
|
|
278
|
+
const resolved = resolveLiteralToOutput(rawTemplate, {
|
|
279
|
+
manifestPath
|
|
280
|
+
});
|
|
281
|
+
rewriteValue(pageArg, resolved, rawTemplate);
|
|
282
|
+
} else {
|
|
283
|
+
const staticallyEvaluated = evalStaticString(pageArg);
|
|
284
|
+
if ('string' == typeof staticallyEvaluated) {
|
|
285
|
+
const resolved = resolveLiteralToOutput(staticallyEvaluated, {
|
|
286
|
+
manifestPath
|
|
287
|
+
});
|
|
288
|
+
const originalExpr = source.slice(pageArg.span.start, pageArg.span.end);
|
|
289
|
+
rewriteValue(pageArg, resolved, originalExpr);
|
|
290
|
+
touched = true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return touched;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
function handleTabsAndWindows(node, source, rewriteValue, manifestPath) {
|
|
298
|
+
if ('CallExpression' !== node.type) return false;
|
|
299
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
300
|
+
const apiRoot = memberChain[0];
|
|
301
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
302
|
+
const method = memberChain.join('.');
|
|
303
|
+
if (method.endsWith('tabs.create') || method.endsWith('windows.create')) {
|
|
304
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
305
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
306
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
307
|
+
const keyName = getPropName(propertyNode);
|
|
308
|
+
if ('url' !== keyName) continue;
|
|
309
|
+
const valueNode = propertyNode.value;
|
|
310
|
+
if (isStringLiteral(valueNode)) {
|
|
311
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
312
|
+
manifestPath
|
|
313
|
+
});
|
|
314
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
315
|
+
} else if (valueNode?.type === 'ArrayExpression') for (const arrayElement of valueNode.elements || []){
|
|
316
|
+
const literalNode = arrayElement?.expression;
|
|
317
|
+
if (isStringLiteral(literalNode)) {
|
|
318
|
+
const resolved = resolveLiteralToOutput(literalNode.value, {
|
|
319
|
+
manifestPath
|
|
320
|
+
});
|
|
321
|
+
rewriteValue(literalNode, resolved, literalNode.value);
|
|
322
|
+
} else if (isStaticTemplate(literalNode)) {
|
|
323
|
+
const raw = source.slice(literalNode.span.start + 1, literalNode.span.end - 1);
|
|
324
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
325
|
+
manifestPath
|
|
326
|
+
});
|
|
327
|
+
rewriteValue(literalNode, resolved, raw);
|
|
328
|
+
} else {
|
|
329
|
+
const raw = evalStaticString(literalNode);
|
|
330
|
+
if ('string' == typeof raw) {
|
|
331
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
332
|
+
manifestPath
|
|
333
|
+
});
|
|
334
|
+
const originalExpr = source.slice(literalNode.span.start, literalNode.span.end);
|
|
335
|
+
rewriteValue(literalNode, resolved, originalExpr);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else if (isStaticTemplate(valueNode)) {
|
|
340
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
341
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
342
|
+
manifestPath
|
|
343
|
+
});
|
|
344
|
+
rewriteValue(valueNode, resolved, raw);
|
|
345
|
+
} else {
|
|
346
|
+
const raw = evalStaticString(valueNode);
|
|
347
|
+
if ('string' == typeof raw) {
|
|
348
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
349
|
+
manifestPath
|
|
350
|
+
});
|
|
351
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
352
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
if (method.endsWith('tabs.update')) {
|
|
359
|
+
const optionsArgument = node.arguments?.[1]?.expression || node.arguments?.[0]?.expression;
|
|
360
|
+
if (optionsArgument?.type === 'ObjectExpression') for (const propertyNode of optionsArgument.properties || []){
|
|
361
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
362
|
+
const keyName = getPropName(propertyNode);
|
|
363
|
+
if ('url' !== keyName) continue;
|
|
364
|
+
const valueNode = propertyNode.value;
|
|
365
|
+
if (isStringLiteral(valueNode)) {
|
|
366
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
367
|
+
manifestPath
|
|
368
|
+
});
|
|
369
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
370
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
371
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
372
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
373
|
+
manifestPath
|
|
374
|
+
});
|
|
375
|
+
rewriteValue(valueNode, resolved, raw);
|
|
376
|
+
} else {
|
|
377
|
+
const raw = evalStaticString(valueNode);
|
|
378
|
+
if ('string' == typeof raw) {
|
|
379
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
380
|
+
manifestPath
|
|
381
|
+
});
|
|
382
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
383
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
function handleScripting(node, source, rewriteValue, manifestPath) {
|
|
392
|
+
if ('CallExpression' !== node.type) return false;
|
|
393
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
394
|
+
const apiRoot = memberChain[0];
|
|
395
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
396
|
+
const method = memberChain.join('.');
|
|
397
|
+
if (method.endsWith("scripting.registerContentScripts")) {
|
|
398
|
+
for (const argumentNode of node.arguments || []){
|
|
399
|
+
const arrayExpressionNode = argumentNode?.expression;
|
|
400
|
+
if (arrayExpressionNode && 'ArrayExpression' === arrayExpressionNode.type) for (const arrayElement of arrayExpressionNode.elements || []){
|
|
401
|
+
const objectExpressionNode = arrayElement?.expression;
|
|
402
|
+
if (objectExpressionNode && 'ObjectExpression' === objectExpressionNode.type) for (const propertyNode of objectExpressionNode.properties || []){
|
|
403
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
404
|
+
const keyName = getPropName(propertyNode);
|
|
405
|
+
if ('js' !== keyName && 'css' !== keyName) continue;
|
|
406
|
+
const valueNode = propertyNode.value;
|
|
407
|
+
if ('ArrayExpression' === valueNode.type) for (const innerItem of valueNode.elements || []){
|
|
408
|
+
const literalNode = innerItem?.expression;
|
|
409
|
+
if (isStringLiteral(literalNode)) {
|
|
410
|
+
const resolved = resolveLiteralToOutput(literalNode.value, {
|
|
411
|
+
manifestPath
|
|
412
|
+
});
|
|
413
|
+
rewriteValue(literalNode, resolved, literalNode.value);
|
|
414
|
+
} else if (isStaticTemplate(literalNode)) {
|
|
415
|
+
const raw = source.slice(literalNode.span.start + 1, literalNode.span.end - 1);
|
|
416
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
417
|
+
manifestPath
|
|
418
|
+
});
|
|
419
|
+
rewriteValue(literalNode, resolved, raw);
|
|
420
|
+
} else {
|
|
421
|
+
const raw = evalStaticString(literalNode);
|
|
422
|
+
if ('string' == typeof raw) {
|
|
423
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
424
|
+
manifestPath
|
|
425
|
+
});
|
|
426
|
+
const originalExpr = source.slice(literalNode.span.start, literalNode.span.end);
|
|
427
|
+
rewriteValue(literalNode, resolved, originalExpr);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
else if (isStringLiteral(valueNode)) {
|
|
432
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
433
|
+
manifestPath
|
|
434
|
+
});
|
|
435
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
436
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
437
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
438
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
439
|
+
manifestPath
|
|
440
|
+
});
|
|
441
|
+
rewriteValue(valueNode, resolved, raw);
|
|
442
|
+
} else {
|
|
443
|
+
const raw = evalStaticString(valueNode);
|
|
444
|
+
if ('string' == typeof raw) {
|
|
445
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
446
|
+
manifestPath
|
|
447
|
+
});
|
|
448
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
449
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
if (method.endsWith("scripting.executeScript") || method.endsWith("scripting.insertCSS")) {
|
|
458
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
459
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
460
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
461
|
+
const keyName = getPropName(propertyNode);
|
|
462
|
+
if ('files' !== keyName) continue;
|
|
463
|
+
const valueNode = propertyNode.value;
|
|
464
|
+
if (valueNode?.type === 'ArrayExpression') for (const arrayElement of valueNode.elements || []){
|
|
465
|
+
const literalNode = arrayElement?.expression;
|
|
466
|
+
if (isStringLiteral(literalNode)) {
|
|
467
|
+
const resolved = resolveLiteralToOutput(literalNode.value, {
|
|
468
|
+
manifestPath
|
|
469
|
+
});
|
|
470
|
+
rewriteValue(literalNode, resolved, literalNode.value);
|
|
471
|
+
} else if (isStaticTemplate(literalNode)) {
|
|
472
|
+
const raw = source.slice(literalNode.span.start + 1, literalNode.span.end - 1);
|
|
473
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
474
|
+
manifestPath
|
|
475
|
+
});
|
|
476
|
+
rewriteValue(literalNode, resolved, raw);
|
|
477
|
+
} else {
|
|
478
|
+
const raw = evalStaticString(literalNode);
|
|
479
|
+
if ('string' == typeof raw) {
|
|
480
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
481
|
+
manifestPath
|
|
482
|
+
});
|
|
483
|
+
const originalExpr = source.slice(literalNode.span.start, literalNode.span.end);
|
|
484
|
+
rewriteValue(literalNode, resolved, originalExpr);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
function handleMv2Tabs(node, source, rewriteValue, manifestPath) {
|
|
494
|
+
if ('CallExpression' !== node.type) return false;
|
|
495
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
496
|
+
const apiRoot = memberChain[0];
|
|
497
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
498
|
+
const method = memberChain.join('.');
|
|
499
|
+
if (method.endsWith('tabs.executeScript') || method.endsWith('tabs.insertCSS')) {
|
|
500
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
501
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
502
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
503
|
+
const keyName = getPropName(propertyNode);
|
|
504
|
+
if ('file' !== keyName) continue;
|
|
505
|
+
const valueNode = propertyNode.value;
|
|
506
|
+
if (isStringLiteral(valueNode)) {
|
|
507
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
508
|
+
manifestPath
|
|
509
|
+
});
|
|
510
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
511
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
512
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
513
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
514
|
+
manifestPath
|
|
515
|
+
});
|
|
516
|
+
rewriteValue(valueNode, resolved, raw);
|
|
517
|
+
} else {
|
|
518
|
+
const raw = evalStaticString(valueNode);
|
|
519
|
+
if ('string' == typeof raw) {
|
|
520
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
521
|
+
manifestPath
|
|
522
|
+
});
|
|
523
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
524
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
return false;
|
|
531
|
+
}
|
|
532
|
+
function handlePanels(node, source, rewriteValue, manifestPath) {
|
|
533
|
+
if ('CallExpression' !== node.type) return false;
|
|
534
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
535
|
+
const apiRoot = memberChain[0];
|
|
536
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
537
|
+
const method = memberChain.join('.');
|
|
538
|
+
if (method.endsWith('sidePanel.setOptions') || method.endsWith('sidebarAction.setPanel')) {
|
|
539
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
540
|
+
if (method.endsWith('sidebarAction.setPanel')) {
|
|
541
|
+
const arg = firstArgument;
|
|
542
|
+
if (isStringLiteral(arg)) {
|
|
543
|
+
const resolved = resolveLiteralToOutput(arg.value, {
|
|
544
|
+
manifestPath
|
|
545
|
+
});
|
|
546
|
+
rewriteValue(arg, resolved, arg.value);
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
if (isStaticTemplate(arg)) {
|
|
550
|
+
const raw = source.slice(arg.span.start + 1, arg.span.end - 1);
|
|
551
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
552
|
+
manifestPath
|
|
553
|
+
});
|
|
554
|
+
rewriteValue(arg, resolved, raw);
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
{
|
|
558
|
+
const raw = evalStaticString(arg);
|
|
559
|
+
if ('string' == typeof raw) {
|
|
560
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
561
|
+
manifestPath
|
|
562
|
+
});
|
|
563
|
+
const originalExpr = source.slice(arg.span.start, arg.span.end);
|
|
564
|
+
rewriteValue(arg, resolved, originalExpr);
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
570
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
571
|
+
const keyName = getPropName(propertyNode);
|
|
572
|
+
if ('path' === keyName || 'panel' === keyName || 'page' === keyName) {
|
|
573
|
+
const valueNode = propertyNode.value;
|
|
574
|
+
if (isStringLiteral(valueNode)) {
|
|
575
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
576
|
+
manifestPath
|
|
577
|
+
});
|
|
578
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
579
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
580
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
581
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
582
|
+
manifestPath
|
|
583
|
+
});
|
|
584
|
+
rewriteValue(valueNode, resolved, raw);
|
|
585
|
+
} else {
|
|
586
|
+
const raw = evalStaticString(valueNode);
|
|
587
|
+
if ('string' == typeof raw) {
|
|
588
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
589
|
+
manifestPath
|
|
590
|
+
});
|
|
591
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
592
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
function handleNotifications(node, source, rewriteValue, manifestPath) {
|
|
602
|
+
if ('CallExpression' !== node.type) return false;
|
|
603
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
604
|
+
const apiRoot = memberChain[0];
|
|
605
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
606
|
+
const method = memberChain.join('.');
|
|
607
|
+
if (method.endsWith('notifications.create') || method.endsWith('notifications.update')) {
|
|
608
|
+
const optionsArgument = node.arguments?.[1]?.expression || node.arguments?.[0]?.expression;
|
|
609
|
+
if (optionsArgument?.type === 'ObjectExpression') {
|
|
610
|
+
let touched = false;
|
|
611
|
+
for (const propertyNode of optionsArgument.properties || []){
|
|
612
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
613
|
+
const keyName = getPropName(propertyNode);
|
|
614
|
+
if ('iconUrl' === keyName || 'imageUrl' === keyName) {
|
|
615
|
+
const valueNode = propertyNode.value;
|
|
616
|
+
if (isStringLiteral(valueNode)) {
|
|
617
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
618
|
+
manifestPath
|
|
619
|
+
});
|
|
620
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
621
|
+
touched = true;
|
|
622
|
+
} else if (isStaticTemplate(valueNode)) {
|
|
623
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
624
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
625
|
+
manifestPath
|
|
626
|
+
});
|
|
627
|
+
rewriteValue(valueNode, resolved, raw);
|
|
628
|
+
touched = true;
|
|
629
|
+
} else {
|
|
630
|
+
const raw = evalStaticString(valueNode);
|
|
631
|
+
if ('string' == typeof raw) {
|
|
632
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
633
|
+
manifestPath
|
|
634
|
+
});
|
|
635
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
636
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
637
|
+
touched = true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
if (touched) return true;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
function handleMenus(node, source, rewriteValue, manifestPath) {
|
|
648
|
+
if ('CallExpression' !== node.type) return false;
|
|
649
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
650
|
+
const apiRoot = memberChain[0];
|
|
651
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
652
|
+
const method = memberChain.join('.');
|
|
653
|
+
if (method.endsWith('contextMenus.create') || method.endsWith('contextMenus.update') || method.endsWith('menus.create') || method.endsWith('menus.update')) {
|
|
654
|
+
const firstArgument = node.arguments?.[0]?.expression;
|
|
655
|
+
if (firstArgument?.type === 'ObjectExpression') for (const propertyNode of firstArgument.properties || []){
|
|
656
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
657
|
+
const keyNode = propertyNode.key;
|
|
658
|
+
const keyName = keyNode?.type === 'Identifier' ? keyNode.value : keyNode?.value;
|
|
659
|
+
if ('icons' !== keyName) continue;
|
|
660
|
+
const valueNode = propertyNode.value;
|
|
661
|
+
if (valueNode?.type === 'ObjectExpression') for (const innerProperty of valueNode.properties || []){
|
|
662
|
+
if ('KeyValueProperty' !== innerProperty.type) continue;
|
|
663
|
+
const innerValue = innerProperty.value;
|
|
664
|
+
if (isStringLiteral(innerValue)) {
|
|
665
|
+
const resolved = resolveLiteralToOutput(innerValue.value, {
|
|
666
|
+
manifestPath
|
|
667
|
+
});
|
|
668
|
+
rewriteValue(innerValue, resolved, innerValue.value);
|
|
669
|
+
} else if (isStaticTemplate(innerValue)) {
|
|
670
|
+
const raw = source.slice(innerValue.span.start + 1, innerValue.span.end - 1);
|
|
671
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
672
|
+
manifestPath
|
|
673
|
+
});
|
|
674
|
+
rewriteValue(innerValue, resolved, raw);
|
|
675
|
+
} else {
|
|
676
|
+
const raw = evalStaticString(innerValue);
|
|
677
|
+
if ('string' == typeof raw) {
|
|
678
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
679
|
+
manifestPath
|
|
680
|
+
});
|
|
681
|
+
const originalExpr = source.slice(innerValue.span.start, innerValue.span.end);
|
|
682
|
+
rewriteValue(innerValue, resolved, originalExpr);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
return false;
|
|
690
|
+
}
|
|
691
|
+
function handleDeclarativeContent(node, source, rewriteValue, manifestPath) {
|
|
692
|
+
if ('NewExpression' !== node.type) return false;
|
|
693
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
694
|
+
const apiRoot = memberChain[0];
|
|
695
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
696
|
+
const method = memberChain.join('.');
|
|
697
|
+
if (method.endsWith('declarativeContent.SetIcon')) {
|
|
698
|
+
const arg0 = node.arguments?.[0];
|
|
699
|
+
const optionsArgument = arg0 && arg0.expression || arg0;
|
|
700
|
+
if (optionsArgument?.type === 'ObjectExpression') for (const propertyNode of optionsArgument.properties || []){
|
|
701
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
702
|
+
const keyNode = propertyNode.key;
|
|
703
|
+
const keyName = keyNode?.type === 'Identifier' ? keyNode.value : keyNode?.value;
|
|
704
|
+
if ('path' !== keyName) continue;
|
|
705
|
+
const valueNode = propertyNode.value;
|
|
706
|
+
if (isStringLiteral(valueNode)) {
|
|
707
|
+
const resolved = resolveLiteralToOutput(valueNode.value, {
|
|
708
|
+
manifestPath
|
|
709
|
+
});
|
|
710
|
+
rewriteValue(valueNode, resolved, valueNode.value);
|
|
711
|
+
} else if (valueNode?.type === 'ObjectExpression') for (const innerProperty of valueNode.properties || []){
|
|
712
|
+
if ('KeyValueProperty' !== innerProperty.type) continue;
|
|
713
|
+
const innerValue = innerProperty.value;
|
|
714
|
+
if (isStringLiteral(innerValue)) {
|
|
715
|
+
const resolved = resolveLiteralToOutput(innerValue.value, {
|
|
716
|
+
manifestPath
|
|
717
|
+
});
|
|
718
|
+
rewriteValue(innerValue, resolved, innerValue.value);
|
|
719
|
+
} else if (isStaticTemplate(innerValue)) {
|
|
720
|
+
const raw = source.slice(innerValue.span.start + 1, innerValue.span.end - 1);
|
|
721
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
722
|
+
manifestPath
|
|
723
|
+
});
|
|
724
|
+
rewriteValue(innerValue, resolved, raw);
|
|
725
|
+
} else {
|
|
726
|
+
const raw = evalStaticString(innerValue);
|
|
727
|
+
if ('string' == typeof raw) {
|
|
728
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
729
|
+
manifestPath
|
|
730
|
+
});
|
|
731
|
+
const originalExpr = source.slice(innerValue.span.start, innerValue.span.end);
|
|
732
|
+
rewriteValue(innerValue, resolved, originalExpr);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
else if (isStaticTemplate(valueNode)) {
|
|
737
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
738
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
739
|
+
manifestPath
|
|
740
|
+
});
|
|
741
|
+
rewriteValue(valueNode, resolved, raw);
|
|
742
|
+
} else {
|
|
743
|
+
const raw = evalStaticString(valueNode);
|
|
744
|
+
if ('string' == typeof raw) {
|
|
745
|
+
const resolved = resolveLiteralToOutput(raw, {
|
|
746
|
+
manifestPath
|
|
747
|
+
});
|
|
748
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
749
|
+
rewriteValue(valueNode, resolved, originalExpr);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
return true;
|
|
754
|
+
}
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
const ALLOWED_KEYS = new Set([
|
|
758
|
+
'url',
|
|
759
|
+
'file',
|
|
760
|
+
'files',
|
|
761
|
+
'path',
|
|
762
|
+
'popup',
|
|
763
|
+
'panel',
|
|
764
|
+
'page',
|
|
765
|
+
'iconUrl',
|
|
766
|
+
'imageUrl',
|
|
767
|
+
'default_icon',
|
|
768
|
+
'default_popup',
|
|
769
|
+
'default_panel'
|
|
770
|
+
]);
|
|
771
|
+
function processValue(valueNode, source, rewriteValue, manifestPath) {
|
|
772
|
+
if (!valueNode) return;
|
|
773
|
+
if (isStringLiteral(valueNode)) {
|
|
774
|
+
const out = resolveLiteralToOutput(valueNode.value, {
|
|
775
|
+
manifestPath
|
|
776
|
+
});
|
|
777
|
+
rewriteValue(valueNode, out, valueNode.value);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
if (isStaticTemplate(valueNode)) {
|
|
781
|
+
const raw = source.slice(valueNode.span.start + 1, valueNode.span.end - 1);
|
|
782
|
+
const out = resolveLiteralToOutput(raw, {
|
|
783
|
+
manifestPath
|
|
784
|
+
});
|
|
785
|
+
rewriteValue(valueNode, out, raw);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const raw = evalStaticString(valueNode);
|
|
789
|
+
if ('string' == typeof raw) {
|
|
790
|
+
const out = resolveLiteralToOutput(raw, {
|
|
791
|
+
manifestPath
|
|
792
|
+
});
|
|
793
|
+
const originalExpr = source.slice(valueNode.span.start, valueNode.span.end);
|
|
794
|
+
rewriteValue(valueNode, out, originalExpr);
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function walkCandidate(containerNode, onCandidateValue) {
|
|
799
|
+
if (!containerNode) return;
|
|
800
|
+
if ('ArrayExpression' === containerNode.type) {
|
|
801
|
+
for (const elementNode of containerNode.elements || [])onCandidateValue(elementNode?.expression);
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
if ('ObjectExpression' === containerNode.type) for (const propertyNode of containerNode.properties || []){
|
|
805
|
+
if ('KeyValueProperty' !== propertyNode.type) continue;
|
|
806
|
+
const key = getPropName(propertyNode);
|
|
807
|
+
if (key) {
|
|
808
|
+
if (ALLOWED_KEYS.has(key)) onCandidateValue(propertyNode.value);
|
|
809
|
+
if (propertyNode.value?.type === 'ArrayExpression' || propertyNode.value?.type === 'ObjectExpression') if (('default_icon' === key || 'path' === key) && propertyNode.value?.type === 'ObjectExpression') {
|
|
810
|
+
for (const sizeProp of propertyNode.value.properties || [])if ('KeyValueProperty' === sizeProp.type) onCandidateValue(sizeProp.value);
|
|
811
|
+
} else walkCandidate(propertyNode.value, onCandidateValue);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
function handleGenericApiPaths(node, source, rewriteValue, manifestPath) {
|
|
816
|
+
if ('CallExpression' !== node.type) return false;
|
|
817
|
+
const memberChain = memberChainFromCallee(node.callee);
|
|
818
|
+
const apiRoot = memberChain[0];
|
|
819
|
+
if ('chrome' !== apiRoot && 'browser' !== apiRoot) return false;
|
|
820
|
+
let touched = false;
|
|
821
|
+
for (const argumentNode of node.arguments || []){
|
|
822
|
+
const expressionNode = argumentNode?.expression;
|
|
823
|
+
if (expressionNode) {
|
|
824
|
+
if ('ObjectExpression' === expressionNode.type || 'ArrayExpression' === expressionNode.type) walkCandidate(expressionNode, (candidateValue)=>{
|
|
825
|
+
const touchedBefore = touched;
|
|
826
|
+
processValue(candidateValue, source, rewriteValue, manifestPath);
|
|
827
|
+
if (!touchedBefore) touched = true;
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return touched;
|
|
832
|
+
}
|
|
833
|
+
function handleCallExpression(node, source, rewriteValue, manifestPath) {
|
|
834
|
+
if (handleRuntimeCalls(node, source, rewriteValue, manifestPath)) return;
|
|
835
|
+
if (handleActionAndPopup(node, source, rewriteValue, manifestPath)) return;
|
|
836
|
+
if (handleDevtools(node, source, rewriteValue, manifestPath)) return;
|
|
837
|
+
if (handleTabsAndWindows(node, source, rewriteValue, manifestPath)) return;
|
|
838
|
+
if (handleScripting(node, source, rewriteValue, manifestPath)) return;
|
|
839
|
+
if (handleMv2Tabs(node, source, rewriteValue, manifestPath)) return;
|
|
840
|
+
if (handlePanels(node, source, rewriteValue, manifestPath)) return;
|
|
841
|
+
if (handleNotifications(node, source, rewriteValue, manifestPath)) return;
|
|
842
|
+
if (handleMenus(node, source, rewriteValue, manifestPath)) return;
|
|
843
|
+
if (handleDeclarativeContent(node, source, rewriteValue, manifestPath)) return;
|
|
844
|
+
try {
|
|
845
|
+
handleGenericApiPaths(node, source, rewriteValue, manifestPath);
|
|
846
|
+
} catch {}
|
|
847
|
+
}
|
|
848
|
+
function isJsxLikePath(filePath) {
|
|
849
|
+
return 'string' == typeof filePath && /\.[cm]?[jt]sx$/.test(String(filePath));
|
|
850
|
+
}
|
|
851
|
+
function stripNestedQuotes(value) {
|
|
852
|
+
const match = /^(['"])(.*)\1$/.exec(value);
|
|
853
|
+
return match ? match[2] : value;
|
|
854
|
+
}
|
|
855
|
+
function normalizeLiteralPayload(value) {
|
|
856
|
+
const withForwardSlashes = value.replace(/\\/g, '/');
|
|
857
|
+
const withCollapsed = withForwardSlashes.replace(/\/{2,}/g, '/');
|
|
858
|
+
return stripNestedQuotes(withCollapsed);
|
|
859
|
+
}
|
|
860
|
+
function isLikelyApiContextAt(source, atIndex) {
|
|
861
|
+
if ('number' != typeof atIndex) return false;
|
|
862
|
+
const start = Math.max(0, atIndex - 200);
|
|
863
|
+
const neighborhood = source.slice(start, atIndex);
|
|
864
|
+
return /(chrome|browser)\s*\.(?:tabs|windows|scripting|action|sidePanel|sidebarAction|devtools|runtime|extension)\b/.test(neighborhood);
|
|
865
|
+
}
|
|
866
|
+
function resolveAndNormalizeLiteral(literal, ctx) {
|
|
867
|
+
const normalizedInput = normalizeLiteralPayload(literal);
|
|
868
|
+
const resolved = resolveLiteralToOutput(normalizedInput, {
|
|
869
|
+
manifestPath: ctx.manifestPath,
|
|
870
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
871
|
+
authorFilePath: ctx.authorFilePath
|
|
872
|
+
});
|
|
873
|
+
return normalizeLiteralPayload(resolved ?? normalizedInput);
|
|
874
|
+
}
|
|
875
|
+
function replaceRuntimeGetURL(source, input, ctx) {
|
|
876
|
+
const rx = /(\.(?:runtime|extension)\.getURL\()\s*(['"])(.*?)\2(\))/g;
|
|
877
|
+
function onMatch(full, pre, q, p, post, offset) {
|
|
878
|
+
try {
|
|
879
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(p), {
|
|
880
|
+
manifestPath: ctx.manifestPath,
|
|
881
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
882
|
+
authorFilePath: ctx.authorFilePath
|
|
883
|
+
});
|
|
884
|
+
const computed = normalizeLiteralPayload(resolved ?? p);
|
|
885
|
+
try {
|
|
886
|
+
ctx.onResolvedLiteral?.(p, resolved ?? void 0, offset);
|
|
887
|
+
} catch {}
|
|
888
|
+
return `${pre}${q}${computed}${q}${post}`;
|
|
889
|
+
} catch {
|
|
890
|
+
return full;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
return input.replace(rx, onMatch);
|
|
894
|
+
}
|
|
895
|
+
function replaceObjectKeyLiterals(source, input, keys, ctx) {
|
|
896
|
+
for (const key of keys){
|
|
897
|
+
const rx = new RegExp(`(${key}\\s*:\\s*)(['"])(.*?)\\2`, 'g');
|
|
898
|
+
function onMatch(full, pre, q, p, offset) {
|
|
899
|
+
try {
|
|
900
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(p), {
|
|
901
|
+
manifestPath: ctx.manifestPath,
|
|
902
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
903
|
+
authorFilePath: ctx.authorFilePath
|
|
904
|
+
});
|
|
905
|
+
const computed = normalizeLiteralPayload(resolved ?? p);
|
|
906
|
+
try {
|
|
907
|
+
if (isLikelyApiContextAt(source, offset)) ctx.onResolvedLiteral?.(p, resolved ?? void 0, offset);
|
|
908
|
+
} catch {}
|
|
909
|
+
return `${pre}${q}${computed}${q}`;
|
|
910
|
+
} catch {
|
|
911
|
+
return full;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
input = input.replace(rx, onMatch);
|
|
915
|
+
}
|
|
916
|
+
return input;
|
|
917
|
+
}
|
|
918
|
+
function replaceStaticTemplateForKeys(source, input, keys, ctx) {
|
|
919
|
+
const rx = new RegExp(`((?:${keys.join('|')})\\s*:\\s*)\`([^\\\`$]*)\``, 'g');
|
|
920
|
+
function onMatch(full, pre, inner, offset) {
|
|
921
|
+
try {
|
|
922
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(inner), {
|
|
923
|
+
manifestPath: ctx.manifestPath,
|
|
924
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
925
|
+
authorFilePath: ctx.authorFilePath
|
|
926
|
+
});
|
|
927
|
+
const computed = normalizeLiteralPayload(resolved ?? inner);
|
|
928
|
+
try {
|
|
929
|
+
if (isLikelyApiContextAt(source, offset)) ctx.onResolvedLiteral?.(inner, resolved ?? void 0, offset);
|
|
930
|
+
} catch {}
|
|
931
|
+
return `${pre}'${computed}'`;
|
|
932
|
+
} catch {
|
|
933
|
+
return full;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
return input.replace(rx, onMatch);
|
|
937
|
+
}
|
|
938
|
+
function replaceConcatForKeys(source, input, keys, ctx) {
|
|
939
|
+
const rx = new RegExp(`((?:${keys.join('|')})\\s*:\\s*)((?:['"][^'"]*['"]\\s*\\+\\s*)+['"][^'"]*['"])`, 'g');
|
|
940
|
+
function onMatch(full, pre, expr, offset) {
|
|
941
|
+
try {
|
|
942
|
+
const partRe = /(['"])([^'"]*?)\1/g;
|
|
943
|
+
let m;
|
|
944
|
+
let concatenated = '';
|
|
945
|
+
while(m = partRe.exec(expr))concatenated += m[2];
|
|
946
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(concatenated), {
|
|
947
|
+
manifestPath: ctx.manifestPath,
|
|
948
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
949
|
+
authorFilePath: ctx.authorFilePath
|
|
950
|
+
});
|
|
951
|
+
const computed = normalizeLiteralPayload(resolved ?? concatenated);
|
|
952
|
+
try {
|
|
953
|
+
if (isLikelyApiContextAt(source, offset)) ctx.onResolvedLiteral?.(concatenated, resolved ?? void 0, offset);
|
|
954
|
+
} catch {}
|
|
955
|
+
return `${pre}'${computed}'`;
|
|
956
|
+
} catch {
|
|
957
|
+
return full;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
return input.replace(rx, onMatch);
|
|
961
|
+
}
|
|
962
|
+
function replaceFilesArray(source, input, ctx) {
|
|
963
|
+
const rx = /(files\s*:\s*\[)([^\]]*)(\])/g;
|
|
964
|
+
function onMatch(full, pre, inner, post, offset) {
|
|
965
|
+
try {
|
|
966
|
+
const replacedInner = inner.replace(/(['"])(.*?)(\1)/g, function(_m, q, p) {
|
|
967
|
+
try {
|
|
968
|
+
const computed = resolveAndNormalizeLiteral(p, ctx);
|
|
969
|
+
return `${q}${computed ?? p}${q}`;
|
|
970
|
+
} catch {
|
|
971
|
+
return `${q}${p}${q}`;
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
try {
|
|
975
|
+
if (isLikelyApiContextAt(source, offset)) {
|
|
976
|
+
const re = /(['"])(.*?)(\1)/g;
|
|
977
|
+
let match;
|
|
978
|
+
while(match = re.exec(inner)){
|
|
979
|
+
const raw = match[2];
|
|
980
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(raw), {
|
|
981
|
+
manifestPath: ctx.manifestPath,
|
|
982
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
983
|
+
authorFilePath: ctx.authorFilePath
|
|
984
|
+
});
|
|
985
|
+
ctx.onResolvedLiteral?.(raw, resolved ?? void 0, offset + match.index);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
} catch {}
|
|
989
|
+
return `${pre}${replacedInner}${post}`;
|
|
990
|
+
} catch {
|
|
991
|
+
return full;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return input.replace(rx, onMatch);
|
|
995
|
+
}
|
|
996
|
+
function replaceJsCssArrays(source, input, ctx) {
|
|
997
|
+
for (const key of [
|
|
998
|
+
'js',
|
|
999
|
+
'css'
|
|
1000
|
+
]){
|
|
1001
|
+
const rxArr = new RegExp(`(${key}\\s*:\\s*\\[)([^\\]]*)(\\])`, 'g');
|
|
1002
|
+
function onMatch(full, pre, inner, post, offset) {
|
|
1003
|
+
try {
|
|
1004
|
+
const replacedInner = String(inner).replace(/(['"])(.*?)(\1)/g, function(_m, q, p) {
|
|
1005
|
+
try {
|
|
1006
|
+
const computed = resolveAndNormalizeLiteral(p, ctx);
|
|
1007
|
+
return `${q}${computed ?? p}${q}`;
|
|
1008
|
+
} catch {
|
|
1009
|
+
return `${q}${p}${q}`;
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
1012
|
+
try {
|
|
1013
|
+
if (isLikelyApiContextAt(source, offset)) {
|
|
1014
|
+
const re = /(['"])(.*?)(\1)/g;
|
|
1015
|
+
let match;
|
|
1016
|
+
while(match = re.exec(inner)){
|
|
1017
|
+
const raw = match[2];
|
|
1018
|
+
const resolved = resolveLiteralToOutput(normalizeLiteralPayload(raw), {
|
|
1019
|
+
manifestPath: ctx.manifestPath,
|
|
1020
|
+
packageJsonDir: ctx.packageJsonDir,
|
|
1021
|
+
authorFilePath: ctx.authorFilePath
|
|
1022
|
+
});
|
|
1023
|
+
ctx.onResolvedLiteral?.(raw, resolved ?? void 0, offset + match.index);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
} catch {}
|
|
1027
|
+
return `${pre}${replacedInner}${post}`;
|
|
1028
|
+
} catch {
|
|
1029
|
+
return full;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
input = input.replace(rxArr, onMatch);
|
|
1033
|
+
}
|
|
1034
|
+
return input;
|
|
1035
|
+
}
|
|
1036
|
+
function cleanupPublicRootLiterals(input) {
|
|
1037
|
+
input = input.replace(/(['"])\/public\/([^'"]+?)\1/g, function(_m, q, p) {
|
|
1038
|
+
return `${q}${normalizeLiteralPayload(p)}${q}`;
|
|
1039
|
+
});
|
|
1040
|
+
input = input.replace(/(['"])public\/(.*?)\1/g, function(_m, q, p) {
|
|
1041
|
+
return `${q}${normalizeLiteralPayload(p)}${q}`;
|
|
1042
|
+
});
|
|
1043
|
+
return input;
|
|
1044
|
+
}
|
|
1045
|
+
function normalizeSpecialFolderExtensions(input) {
|
|
1046
|
+
input = input.replace(/(['"])((?:pages|scripts)\/[^'"]+?)\.(ts|tsx)\1/g, function(_m, q, p) {
|
|
1047
|
+
return `${q}${p}.js${q}`;
|
|
1048
|
+
});
|
|
1049
|
+
input = input.replace(/(['"])((?:pages|scripts)\/[^'"]+?)\.(njk|nunjucks)\1/g, function(_m, q, p) {
|
|
1050
|
+
return `${q}${p}.html${q}`;
|
|
1051
|
+
});
|
|
1052
|
+
input = input.replace(/(['"])((?:pages|scripts)\/[^'"]+?)\.(scss|sass|less)\1/g, function(_m, q, p) {
|
|
1053
|
+
return `${q}${p}.css${q}`;
|
|
1054
|
+
});
|
|
1055
|
+
return input;
|
|
1056
|
+
}
|
|
1057
|
+
function collapseAccidentalDoubleQuotes(input, keys) {
|
|
1058
|
+
const keyUnion = `(?:${keys.join('|')})`;
|
|
1059
|
+
input = input.replace(new RegExp(`(${keyUnion}\\s*:\\s*)''`, 'g'), "$1'");
|
|
1060
|
+
input = input.replace(new RegExp(`(${keyUnion}\\s*:\\s*)""`, 'g'), '$1"');
|
|
1061
|
+
input = input.replace(/:\s*''/g, ": '");
|
|
1062
|
+
input = input.replace(/:\s*""/g, ': "');
|
|
1063
|
+
input = input.replace(/:\s*''([^']+)'/g, ": '$1'");
|
|
1064
|
+
input = input.replace(/:\s*\"\"([^\"]+)\"/g, ': "$1"');
|
|
1065
|
+
input = input.replace(/''([^']*?)''/g, "'$1'");
|
|
1066
|
+
input = input.replace(/""([^"]*?)""/g, '"$1"');
|
|
1067
|
+
return input;
|
|
1068
|
+
}
|
|
1069
|
+
function textFallbackTransform(source, opts) {
|
|
1070
|
+
let output = String(source);
|
|
1071
|
+
const ctx = {
|
|
1072
|
+
manifestPath: opts.manifestPath,
|
|
1073
|
+
packageJsonDir: opts.packageJsonDir,
|
|
1074
|
+
authorFilePath: opts.authorFilePath,
|
|
1075
|
+
onResolvedLiteral: opts.onResolvedLiteral
|
|
1076
|
+
};
|
|
1077
|
+
output = replaceRuntimeGetURL(source, output, ctx);
|
|
1078
|
+
const supportedKeys = [
|
|
1079
|
+
'url',
|
|
1080
|
+
'file',
|
|
1081
|
+
'path',
|
|
1082
|
+
'iconUrl',
|
|
1083
|
+
'imageUrl',
|
|
1084
|
+
'default_icon'
|
|
1085
|
+
];
|
|
1086
|
+
output = replaceObjectKeyLiterals(source, output, supportedKeys, ctx);
|
|
1087
|
+
output = replaceStaticTemplateForKeys(source, output, supportedKeys, ctx);
|
|
1088
|
+
output = replaceConcatForKeys(source, output, supportedKeys, ctx);
|
|
1089
|
+
output = replaceFilesArray(source, output, ctx);
|
|
1090
|
+
output = replaceJsCssArrays(source, output, ctx);
|
|
1091
|
+
if (!isJsxLikePath(opts.authorFilePath)) output = cleanupPublicRootLiterals(output);
|
|
1092
|
+
output = normalizeSpecialFolderExtensions(output);
|
|
1093
|
+
output = collapseAccidentalDoubleQuotes(output, supportedKeys);
|
|
1094
|
+
return output;
|
|
1095
|
+
}
|
|
1096
|
+
var swc_filename = __rspack_fileURLToPath(import.meta.url);
|
|
1097
|
+
let swcRuntimeModule;
|
|
1098
|
+
async function loadSwc() {
|
|
1099
|
+
if (swcRuntimeModule) return swcRuntimeModule;
|
|
1100
|
+
try {
|
|
1101
|
+
const mod = await import("@swc/core");
|
|
1102
|
+
swcRuntimeModule = mod?.default && mod.default.parse ? mod.default : mod;
|
|
1103
|
+
} catch {
|
|
1104
|
+
try {
|
|
1105
|
+
const mod = __webpack_require__("@swc/core?1153") || createRequire(swc_filename)('@swc/core');
|
|
1106
|
+
swcRuntimeModule = mod?.default && mod.default.parse ? mod.default : mod;
|
|
1107
|
+
} catch {}
|
|
1108
|
+
}
|
|
1109
|
+
return swcRuntimeModule || null;
|
|
1110
|
+
}
|
|
1111
|
+
async function parseWithSwc(swc, source, flags) {
|
|
1112
|
+
return swc.parse(source, {
|
|
1113
|
+
syntax: flags.isTS ? "typescript" : "ecmascript",
|
|
1114
|
+
tsx: flags.isTS && flags.isJSX,
|
|
1115
|
+
jsx: !flags.isTS && flags.isJSX,
|
|
1116
|
+
target: 'es2022',
|
|
1117
|
+
isModule: true
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
async function resolvePathsLoader(source) {
|
|
1121
|
+
const callback = this.async();
|
|
1122
|
+
const options = this.getOptions?.() || {};
|
|
1123
|
+
const manifestPath = options.manifestPath;
|
|
1124
|
+
const packageJsonDir = options.packageJsonDir;
|
|
1125
|
+
const outputPath = String(options.outputPath || '');
|
|
1126
|
+
const sourceMapsOpt = 'boolean' == typeof options.sourceMaps || 'auto' === options.sourceMaps ? options.sourceMaps : 'auto';
|
|
1127
|
+
this.cacheable && this.cacheable();
|
|
1128
|
+
const emittedBodies = new Set();
|
|
1129
|
+
const emitStandardWarning = (headerFilePath, bodyLines)=>{
|
|
1130
|
+
const bodyMessage = bodyLines.join('\n');
|
|
1131
|
+
if (emittedBodies.has(bodyMessage)) return;
|
|
1132
|
+
emittedBodies.add(bodyMessage);
|
|
1133
|
+
const warning = new Error(bodyMessage);
|
|
1134
|
+
warning.file = headerFilePath;
|
|
1135
|
+
this.emitWarning?.(warning);
|
|
1136
|
+
};
|
|
1137
|
+
const warnIfMissingPublic = (original, computed)=>{
|
|
1138
|
+
try {
|
|
1139
|
+
const canonical = unixify(original || '');
|
|
1140
|
+
const authorUsedRoot = /^\//.test(canonical);
|
|
1141
|
+
if (computed) {
|
|
1142
|
+
if (/^(?:pages|scripts)\//i.test(computed)) return;
|
|
1143
|
+
const root = packageJsonDir || __rspack_external_path.dirname(manifestPath);
|
|
1144
|
+
const sourcePublicAbs = __rspack_external_path.join(root, 'public', computed);
|
|
1145
|
+
if (!__rspack_external_fs.existsSync(sourcePublicAbs)) {
|
|
1146
|
+
const displayPath = authorUsedRoot && outputPath ? __rspack_external_path.join(outputPath, computed) : sourcePublicAbs;
|
|
1147
|
+
const lines = [
|
|
1148
|
+
'Check the path used in your extension API call.',
|
|
1149
|
+
'The path must point to an existing file that will be packaged with the extension.',
|
|
1150
|
+
`Found value: ${original ?? ''}`,
|
|
1151
|
+
`Resolved path: ${authorUsedRoot && outputPath ? __rspack_external_path.join(outputPath, computed) : sourcePublicAbs}`
|
|
1152
|
+
];
|
|
1153
|
+
if (authorUsedRoot) lines.push("Paths starting with '/' are resolved from the extension output root (served from public/), not your source directory.");
|
|
1154
|
+
lines.push('', `NOT FOUND ${displayPath}`);
|
|
1155
|
+
emitStandardWarning(String(this.resourcePath), lines);
|
|
1156
|
+
}
|
|
1157
|
+
} else if (original) {
|
|
1158
|
+
const looksNested = /(^|\/)src\/pages\//i.test(canonical) || /(^|\/)src\/scripts\//i.test(canonical);
|
|
1159
|
+
if (looksNested) {
|
|
1160
|
+
const root = packageJsonDir || __rspack_external_path.dirname(manifestPath);
|
|
1161
|
+
const abs = __rspack_external_path.join(root, canonical);
|
|
1162
|
+
const lines = [
|
|
1163
|
+
'Check the path used in your extension API call.',
|
|
1164
|
+
'The path must point to an existing file that will be packaged with the extension.',
|
|
1165
|
+
`Found value: ${original}`
|
|
1166
|
+
];
|
|
1167
|
+
lines.push('', `NOT FOUND ${abs}`);
|
|
1168
|
+
emitStandardWarning(String(this.resourcePath), lines);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
} catch {}
|
|
1172
|
+
};
|
|
1173
|
+
try {
|
|
1174
|
+
const resourcePathUnix = unixify(String(this.resourcePath || ''));
|
|
1175
|
+
if (resourcePathUnix.split('/').includes('public')) return callback(null, source);
|
|
1176
|
+
} catch {}
|
|
1177
|
+
try {
|
|
1178
|
+
const sourceString = String(source);
|
|
1179
|
+
const containsEligiblePatterns = /(?:^|[^A-Za-z0-9_$])(chrome|browser)\s*\./.test(sourceString) || /(?:^|[^A-Za-z0-9_$])(?:runtime|extension)\s*\.?\s*getURL\s*\(/.test(sourceString) || /(?:^|[^A-Za-z0-9_$])\/public\//.test(sourceString) || /(?:^|[^A-Za-z0-9_$])public\//.test(sourceString) || /(?:^|[^A-Za-z0-9_$])\/pages\//.test(sourceString) || /(?:^|[^A-Za-z0-9_$])pages\//.test(sourceString) || /(?:^|[^A-Za-z0-9_$])\/scripts\//.test(sourceString) || /(?:^|[^A-Za-z0-9_$])scripts\//.test(sourceString);
|
|
1180
|
+
if (!containsEligiblePatterns) return callback(null, source);
|
|
1181
|
+
} catch {}
|
|
1182
|
+
let postTextSource;
|
|
1183
|
+
try {
|
|
1184
|
+
const out = textFallbackTransform(String(source), {
|
|
1185
|
+
manifestPath,
|
|
1186
|
+
packageJsonDir,
|
|
1187
|
+
authorFilePath: String(this.resourcePath || ''),
|
|
1188
|
+
onResolvedLiteral: (original, computed)=>warnIfMissingPublic(original, computed)
|
|
1189
|
+
});
|
|
1190
|
+
postTextSource = out;
|
|
1191
|
+
const hasStaticTemplates = /`[^$`]*`/.test(out);
|
|
1192
|
+
const hasBinaryConcats = /(['"`][^'"`]*['"`]\s*\+\s*['"`][^'"`]*['"`])/.test(out);
|
|
1193
|
+
const hasApiRoots = /(?:^|[^A-Za-z0-9_$])(chrome|browser)\s*\./.test(out);
|
|
1194
|
+
const needsStaticEval = (hasStaticTemplates || hasBinaryConcats) && hasApiRoots;
|
|
1195
|
+
const enableMaps = 'auto' === sourceMapsOpt ? Boolean(this.sourceMap) : Boolean(sourceMapsOpt);
|
|
1196
|
+
if (!needsStaticEval) {
|
|
1197
|
+
if (out !== String(source) || enableMaps && /\.[cm]?tsx?$/.test(String(this.resourcePath || ''))) {
|
|
1198
|
+
const msAll = new magic_string(String(source));
|
|
1199
|
+
msAll.overwrite(0, String(source).length, out);
|
|
1200
|
+
const map = enableMaps ? msAll.generateMap({
|
|
1201
|
+
hires: true,
|
|
1202
|
+
source: String(this.resourcePath),
|
|
1203
|
+
includeContent: true
|
|
1204
|
+
}) : void 0;
|
|
1205
|
+
return callback(null, out, map);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
} catch {}
|
|
1209
|
+
const swc = await loadSwc();
|
|
1210
|
+
if (!swc) return callback(null, source);
|
|
1211
|
+
let programAst;
|
|
1212
|
+
try {
|
|
1213
|
+
const isTS = /\.[cm]?tsx?$/.test(this.resourcePath);
|
|
1214
|
+
const isJSX = /\.[cm]?[jt]sx$/.test(this.resourcePath);
|
|
1215
|
+
const parseSource = 'string' == typeof postTextSource ? postTextSource : source;
|
|
1216
|
+
programAst = await parseWithSwc(swc, parseSource, {
|
|
1217
|
+
isTS,
|
|
1218
|
+
isJSX
|
|
1219
|
+
});
|
|
1220
|
+
} catch {
|
|
1221
|
+
return callback(null, source);
|
|
1222
|
+
}
|
|
1223
|
+
const inputSource = 'string' == typeof postTextSource ? postTextSource : String(source);
|
|
1224
|
+
const ms = new magic_string(inputSource);
|
|
1225
|
+
let edited = false;
|
|
1226
|
+
const rewriteValue = (node, computed, rawInput)=>{
|
|
1227
|
+
if (!computed && 'string' == typeof rawInput) try {
|
|
1228
|
+
const recomputed = resolveLiteralToOutput(rawInput, {
|
|
1229
|
+
manifestPath,
|
|
1230
|
+
packageJsonDir,
|
|
1231
|
+
authorFilePath: String(this.resourcePath || '')
|
|
1232
|
+
});
|
|
1233
|
+
if (recomputed) computed = recomputed;
|
|
1234
|
+
} catch {}
|
|
1235
|
+
if (!computed) return;
|
|
1236
|
+
try {
|
|
1237
|
+
const span = node?.span;
|
|
1238
|
+
if (span && 'number' == typeof span.start && 'number' == typeof span.end) {
|
|
1239
|
+
const src = String(inputSource);
|
|
1240
|
+
const before = src[span.start - 1];
|
|
1241
|
+
const after = src[span.end];
|
|
1242
|
+
const isQuote = (c)=>"'" === c || '"' === c;
|
|
1243
|
+
if (isQuote(before) && before === after) {
|
|
1244
|
+
let start = span.start - 1;
|
|
1245
|
+
let end = span.end + 1;
|
|
1246
|
+
const q = before;
|
|
1247
|
+
const json = JSON.stringify(String(computed));
|
|
1248
|
+
if ('"' === q) ms.overwrite(start, end, json);
|
|
1249
|
+
else {
|
|
1250
|
+
const inner = json.slice(1, -1).replace(/'/g, "\\'");
|
|
1251
|
+
ms.overwrite(start, end, `'${inner}'`);
|
|
1252
|
+
}
|
|
1253
|
+
} else ms.overwrite(span.start, span.end, JSON.stringify(computed));
|
|
1254
|
+
edited = true;
|
|
1255
|
+
}
|
|
1256
|
+
} catch {}
|
|
1257
|
+
};
|
|
1258
|
+
const walk = (currentNode)=>{
|
|
1259
|
+
if (!currentNode || 'object' != typeof currentNode) return;
|
|
1260
|
+
if ('CallExpression' === currentNode.type) try {
|
|
1261
|
+
handleCallExpression(currentNode, String(inputSource), rewriteValue, manifestPath);
|
|
1262
|
+
} catch {}
|
|
1263
|
+
for(const key in currentNode){
|
|
1264
|
+
if (!Object.prototype.hasOwnProperty.call(currentNode, key)) continue;
|
|
1265
|
+
const childNode = currentNode[key];
|
|
1266
|
+
if (childNode) {
|
|
1267
|
+
if (Array.isArray(childNode)) childNode.forEach(walk);
|
|
1268
|
+
else if (childNode && 'string' == typeof childNode.type) walk(childNode);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
walk(programAst);
|
|
1273
|
+
try {
|
|
1274
|
+
if (!edited) {
|
|
1275
|
+
if ('string' == typeof postTextSource && postTextSource !== String(source)) {
|
|
1276
|
+
const enableMaps = 'auto' === sourceMapsOpt ? Boolean(this.sourceMap) : Boolean(sourceMapsOpt);
|
|
1277
|
+
const msAll = new magic_string(String(source));
|
|
1278
|
+
msAll.overwrite(0, String(source).length, postTextSource);
|
|
1279
|
+
const map = enableMaps ? msAll.generateMap({
|
|
1280
|
+
hires: true,
|
|
1281
|
+
source: String(this.resourcePath),
|
|
1282
|
+
includeContent: true
|
|
1283
|
+
}) : void 0;
|
|
1284
|
+
return callback(null, postTextSource, map);
|
|
1285
|
+
}
|
|
1286
|
+
return callback(null, source);
|
|
1287
|
+
}
|
|
1288
|
+
const enableMaps = 'auto' === sourceMapsOpt ? Boolean(this.sourceMap) : Boolean(sourceMapsOpt);
|
|
1289
|
+
const code = ms.toString();
|
|
1290
|
+
const map = enableMaps ? ms.generateMap({
|
|
1291
|
+
hires: true,
|
|
1292
|
+
source: String(this.resourcePath),
|
|
1293
|
+
includeContent: true
|
|
1294
|
+
}) : void 0;
|
|
1295
|
+
return callback(null, code, map);
|
|
1296
|
+
} catch {
|
|
1297
|
+
return callback(null, source);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
export default resolvePathsLoader;
|