wxt 0.19.1-alpha2 → 0.19.1
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/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +13 -1
- package/dist/core/utils/transform.d.ts +3 -2
- package/dist/core/utils/transform.mjs +53 -1
- package/dist/sandbox/define-background.mjs +1 -1
- package/dist/sandbox/define-unlisted-script.mjs +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +1 -1
|
@@ -8,7 +8,19 @@ export function removeEntrypointMainFunction(config, path) {
|
|
|
8
8
|
transform: {
|
|
9
9
|
order: "pre",
|
|
10
10
|
handler(code, id) {
|
|
11
|
-
if (id === absPath)
|
|
11
|
+
if (id === absPath) {
|
|
12
|
+
const newCode = removeMainFunctionCode(code);
|
|
13
|
+
config.logger.debug("vite-node transformed entrypoint", path);
|
|
14
|
+
config.logger.debug(`Original:
|
|
15
|
+
---
|
|
16
|
+
${code}
|
|
17
|
+
---`);
|
|
18
|
+
config.logger.debug(`Transformed:
|
|
19
|
+
---
|
|
20
|
+
${newCode.code}
|
|
21
|
+
---`);
|
|
22
|
+
return newCode;
|
|
23
|
+
}
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Removes any code used at runtime related to an entrypoint's main function.
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* 1. Removes or clears out `main` function from returned object
|
|
4
|
+
* 2. Removes unused imports
|
|
5
|
+
* 3. Removes value-less, side-effect only imports (like `import "./styles.css"` or `import "webextension-polyfill"`)
|
|
5
6
|
*/
|
|
6
7
|
export declare function removeMainFunctionCode(code: string): {
|
|
7
8
|
code: string;
|
|
@@ -2,12 +2,14 @@ import { parseModule } from "magicast";
|
|
|
2
2
|
export function removeMainFunctionCode(code) {
|
|
3
3
|
const mod = parseModule(code);
|
|
4
4
|
emptyMainFunction(mod);
|
|
5
|
+
removeUnusedImports(mod);
|
|
6
|
+
removeSideEffectImports(mod);
|
|
5
7
|
return mod.generate();
|
|
6
8
|
}
|
|
7
9
|
function emptyMainFunction(mod) {
|
|
8
10
|
if (mod.exports?.default?.$type === "function-call") {
|
|
9
11
|
if (mod.exports.default.$ast?.arguments?.[0]?.body) {
|
|
10
|
-
mod.exports.default.$ast.arguments[0]
|
|
12
|
+
delete mod.exports.default.$ast.arguments[0];
|
|
11
13
|
} else if (mod.exports.default.$ast?.arguments?.[0]?.properties) {
|
|
12
14
|
mod.exports.default.$ast.arguments[0].properties = mod.exports.default.$ast.arguments[0].properties.filter(
|
|
13
15
|
(prop) => prop.key.name !== "main"
|
|
@@ -15,3 +17,53 @@ function emptyMainFunction(mod) {
|
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
}
|
|
20
|
+
function removeUnusedImports(mod) {
|
|
21
|
+
const importSymbols = Object.keys(mod.imports);
|
|
22
|
+
const usedMap = new Map(importSymbols.map((sym) => [sym, false]));
|
|
23
|
+
const queue = [getSimpleAstJson(mod.$ast)];
|
|
24
|
+
for (const item of queue) {
|
|
25
|
+
if (!item) {
|
|
26
|
+
continue;
|
|
27
|
+
} else if (Array.isArray(item)) {
|
|
28
|
+
queue.push(...item);
|
|
29
|
+
} else if (item.type === "ImportDeclaration") {
|
|
30
|
+
continue;
|
|
31
|
+
} else if (item.type === "Identifier") {
|
|
32
|
+
if (usedMap.has(item.name)) usedMap.set(item.name, true);
|
|
33
|
+
} else if (typeof item === "object") {
|
|
34
|
+
queue.push(Object.values(item));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (const [name, used] of usedMap.entries()) {
|
|
38
|
+
if (!used) delete mod.imports[name];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function deleteImportAst(mod, shouldDelete) {
|
|
42
|
+
const importIndexesToDelete = [];
|
|
43
|
+
mod.$ast.body.forEach((node, index) => {
|
|
44
|
+
if (node.type === "ImportDeclaration" && shouldDelete(node)) {
|
|
45
|
+
importIndexesToDelete.push(index);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
importIndexesToDelete.reverse().forEach((i) => {
|
|
49
|
+
delete mod.$ast.body[i];
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function removeSideEffectImports(mod) {
|
|
53
|
+
deleteImportAst(mod, (node) => node.specifiers.length === 0);
|
|
54
|
+
}
|
|
55
|
+
function getSimpleAstJson(ast) {
|
|
56
|
+
if (!ast) {
|
|
57
|
+
return ast;
|
|
58
|
+
} else if (Array.isArray(ast)) {
|
|
59
|
+
return ast.map(getSimpleAstJson);
|
|
60
|
+
} else if (typeof ast === "object") {
|
|
61
|
+
return Object.fromEntries(
|
|
62
|
+
Object.entries(ast).filter(
|
|
63
|
+
([key, value]) => key !== "loc" && key !== "start" && key !== "end"
|
|
64
|
+
).map(([key, value]) => [key, getSimpleAstJson(value)])
|
|
65
|
+
);
|
|
66
|
+
} else {
|
|
67
|
+
return ast;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/dist/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.19.1
|
|
1
|
+
export const version = "0.19.1";
|