wxt 0.19.1 → 0.19.3-alpha1

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/README.md CHANGED
@@ -59,6 +59,7 @@ Or see the [installation guide](https://wxt.dev/guide/installation.html) to get
59
59
  - 🦾 Auto-imports
60
60
  - 🤖 Automated publishing
61
61
  - 🎨 Frontend framework agnostic: works with Vue, React, Svelte, etc
62
+ - 📦 Modular architecture with [WXT modules](https://wxt.dev/guide/go-further/reusable-modules.html#overview)
62
63
  - 🖍️ Quickly bootstrap a new project
63
64
  - 📏 Bundle analysis
64
65
  - ⬇️ Download and bundle remote URL imports
@@ -8,11 +8,11 @@ export class ContentScriptContext {
8
8
  this.options = options;
9
9
  this.#abortController = new AbortController();
10
10
  if (this.#isTopFrame) {
11
+ this.#listenForNewerScripts({ ignoreFirstEvent: true });
11
12
  this.#stopOldScripts();
12
- }
13
- this.setTimeout(() => {
13
+ } else {
14
14
  this.#listenForNewerScripts();
15
- });
15
+ }
16
16
  }
17
17
  static SCRIPT_STARTED_MESSAGE_TYPE = "wxt:content-script-started";
18
18
  #isTopFrame = window.self === window.top;
@@ -157,9 +157,12 @@ export class ContentScriptContext {
157
157
  "*"
158
158
  );
159
159
  }
160
- #listenForNewerScripts() {
160
+ #listenForNewerScripts(options) {
161
+ let isFirst = true;
161
162
  const cb = (event) => {
162
163
  if (event.data?.type === ContentScriptContext.SCRIPT_STARTED_MESSAGE_TYPE && event.data?.contentScriptName === this.contentScriptName) {
164
+ if (isFirst && options?.ignoreFirstEvent) return;
165
+ isFirst = false;
163
166
  this.notifyInvalidated();
164
167
  }
165
168
  };
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Removes any code used at runtime related to an entrypoint's main function.
3
3
  * 1. Removes or clears out `main` function from returned object
4
- * 2. Removes unused imports
4
+ * 2. Removes any unused functions/variables outside the definition that aren't being called/used
5
+ * 3. Removes unused imports
5
6
  * 3. Removes value-less, side-effect only imports (like `import "./styles.css"` or `import "webextension-polyfill"`)
6
7
  */
7
8
  export declare function removeMainFunctionCode(code: string): {
@@ -2,7 +2,15 @@ import { parseModule } from "magicast";
2
2
  export function removeMainFunctionCode(code) {
3
3
  const mod = parseModule(code);
4
4
  emptyMainFunction(mod);
5
- removeUnusedImports(mod);
5
+ let removedCount = 0;
6
+ let depth = 0;
7
+ const maxDepth = 10;
8
+ do {
9
+ removedCount = 0;
10
+ removedCount += removeUnusedTopLevelVariables(mod);
11
+ removedCount += removeUnusedTopLevelFunctions(mod);
12
+ removedCount += removeUnusedImports(mod);
13
+ } while (removedCount > 0 && depth++ <= maxDepth);
6
14
  removeSideEffectImports(mod);
7
15
  return mod.generate();
8
16
  }
@@ -17,26 +25,77 @@ function emptyMainFunction(mod) {
17
25
  }
18
26
  }
19
27
  }
28
+ function removeUnusedTopLevelVariables(mod) {
29
+ const simpleAst = getSimpleAstJson(mod.$ast);
30
+ const usedMap = findUsedIdentifiers(simpleAst);
31
+ let deletedCount = 0;
32
+ const ast = mod.$ast;
33
+ for (let i = ast.body.length - 1; i >= 0; i--) {
34
+ if (ast.body[i].type === "VariableDeclaration") {
35
+ for (let j = ast.body[i].declarations.length - 1; j >= 0; j--) {
36
+ if (!usedMap.get(ast.body[i].declarations[j].id.name)) {
37
+ ast.body[i].declarations.splice(j, 1);
38
+ deletedCount++;
39
+ }
40
+ }
41
+ if (ast.body[i].declarations.length === 0) {
42
+ ast.body.splice(i, 1);
43
+ }
44
+ }
45
+ }
46
+ return deletedCount;
47
+ }
48
+ function removeUnusedTopLevelFunctions(mod) {
49
+ const simpleAst = getSimpleAstJson(mod.$ast);
50
+ const usedMap = findUsedIdentifiers(simpleAst);
51
+ let deletedCount = 0;
52
+ const ast = mod.$ast;
53
+ for (let i = ast.body.length - 1; i >= 0; i--) {
54
+ if (ast.body[i].type === "FunctionDeclaration" && !usedMap.get(ast.body[i].id.name)) {
55
+ ast.body.splice(i, 1);
56
+ deletedCount++;
57
+ }
58
+ }
59
+ return deletedCount;
60
+ }
20
61
  function removeUnusedImports(mod) {
62
+ const simpleAst = getSimpleAstJson(mod.$ast);
63
+ const usedMap = findUsedIdentifiers(simpleAst);
21
64
  const importSymbols = Object.keys(mod.imports);
22
- const usedMap = new Map(importSymbols.map((sym) => [sym, false]));
23
- const queue = [getSimpleAstJson(mod.$ast)];
65
+ let deletedCount = 0;
66
+ importSymbols.forEach((name) => {
67
+ if (usedMap.get(name)) return;
68
+ delete mod.imports[name];
69
+ deletedCount++;
70
+ });
71
+ return deletedCount;
72
+ }
73
+ function findUsedIdentifiers(simpleAst) {
74
+ const usedMap = /* @__PURE__ */ new Map();
75
+ const queue = [simpleAst];
24
76
  for (const item of queue) {
25
77
  if (!item) {
26
- continue;
27
78
  } else if (Array.isArray(item)) {
28
79
  queue.push(...item);
29
80
  } else if (item.type === "ImportDeclaration") {
30
81
  continue;
31
82
  } else if (item.type === "Identifier") {
32
- if (usedMap.has(item.name)) usedMap.set(item.name, true);
83
+ usedMap.set(item.name, true);
33
84
  } else if (typeof item === "object") {
34
- queue.push(Object.values(item));
85
+ const filterFns = {
86
+ // Ignore the function declaration's name
87
+ FunctionDeclaration: ([key]) => key !== "id",
88
+ // Ignore object property names
89
+ ObjectProperty: ([key]) => key !== "key",
90
+ // Ignore variable declaration's name
91
+ VariableDeclarator: ([key]) => key !== "id"
92
+ };
93
+ queue.push(
94
+ Object.entries(item).filter(filterFns[item.type] ?? (() => true)).map(([_, value]) => value)
95
+ );
35
96
  }
36
97
  }
37
- for (const [name, used] of usedMap.entries()) {
38
- if (!used) delete mod.imports[name];
39
- }
98
+ return usedMap;
40
99
  }
41
100
  function deleteImportAst(mod, shouldDelete) {
42
101
  const importIndexesToDelete = [];
package/dist/core/wxt.mjs CHANGED
@@ -5,6 +5,7 @@ import { createViteBuilder } from "./builders/vite/index.mjs";
5
5
  import { builtinModules } from "../builtin-modules/index.mjs";
6
6
  export let wxt;
7
7
  export async function registerWxt(command, inlineConfig = {}, getServer) {
8
+ process.env.NODE_ENV ??= command === "serve" ? "development" : "production";
8
9
  const hooks = createHooks();
9
10
  const config = await resolveConfig(inlineConfig, command);
10
11
  const server = await getServer?.(config);
package/dist/types.d.ts CHANGED
@@ -937,9 +937,14 @@ export interface WxtHooks {
937
937
  * // variable called "example" in the TS project.
938
938
  * entries.push({
939
939
  * path: "types/example.d.ts",
940
- * textContent: "declare const a: string;",
940
+ * text: "declare const a: string;",
941
941
  * tsReference: true,
942
942
  * });
943
+ * // use module to add Triple-Slash Directive in .wxt/wxt.d.ts
944
+ * // eg: /// <reference types="@types/example" />
945
+ * entries.push({
946
+ * module: '@types/example'
947
+ * });
943
948
  * })
944
949
  */
945
950
  'prepare:types': (wxt: Wxt, entries: WxtDirEntry[]) => HookResult;
@@ -1266,6 +1271,10 @@ export type WxtDirEntry = WxtDirTypeReferenceEntry | WxtDirFileEntry;
1266
1271
  * Represents type reference to a node module to be added to `.wxt/wxt.d.ts` file
1267
1272
  */
1268
1273
  export interface WxtDirTypeReferenceEntry {
1274
+ /**
1275
+ * Specifies the module name that will be used in the `/// <reference types="..." />` directive.
1276
+ * This value will be added to the `.wxt/wxt.d.ts` file to include type definitions from the specified module.
1277
+ */
1269
1278
  module: string;
1270
1279
  }
1271
1280
  /**
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export const version = "0.19.1";
1
+ export const version = "0.19.3-alpha1";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wxt",
3
3
  "type": "module",
4
- "version": "0.19.1",
4
+ "version": "0.19.3-alpha1",
5
5
  "description": "Next gen framework for developing web extensions",
6
6
  "repository": {
7
7
  "type": "git",
@@ -80,6 +80,7 @@
80
80
  "cac": "^6.7.14",
81
81
  "chokidar": "^3.6.0",
82
82
  "ci-info": "^4.0.0",
83
+ "consola": "^3.2.3",
83
84
  "defu": "^6.1.4",
84
85
  "dequal": "^2.0.3",
85
86
  "esbuild": "^0.23.0",
@@ -105,20 +106,20 @@
105
106
  "picocolors": "^1.0.1",
106
107
  "prompts": "^2.4.2",
107
108
  "publish-browser-extension": "^2.1.3",
108
- "unimport": "^3.9.0",
109
- "vite": "^5.3.4",
110
- "vite-node": "^2.0.0",
109
+ "unimport": "^3.9.1",
110
+ "vite": "^5.3.5",
111
+ "vite-node": "^2.0.4",
111
112
  "web-ext-run": "^0.2.1",
112
113
  "webextension-polyfill": "^0.12.0"
113
114
  },
114
115
  "devDependencies": {
115
116
  "@aklinker1/check": "^1.3.1",
116
117
  "@faker-js/faker": "^8.4.1",
117
- "@types/chrome": "^0.0.268",
118
+ "@types/chrome": "^0.0.269",
118
119
  "@types/fs-extra": "^11.0.4",
119
120
  "@types/lodash.merge": "^4.6.9",
120
121
  "@types/natural-compare": "^1.4.3",
121
- "@types/node": "^20.14.11",
122
+ "@types/node": "^20.14.12",
122
123
  "@types/normalize-path": "^3.0.2",
123
124
  "@types/prompts": "^2.4.9",
124
125
  "execa": "^9.3.0",
@@ -128,9 +129,9 @@
128
129
  "p-map": "^7.0.2",
129
130
  "publint": "^0.2.9",
130
131
  "tsx": "4.15.7",
131
- "typescript": "^5.5.3",
132
+ "typescript": "^5.5.4",
132
133
  "unbuild": "^2.0.0",
133
- "vitest": "^2.0.3",
134
+ "vitest": "^2.0.4",
134
135
  "vitest-plugin-random-seed": "^1.1.0"
135
136
  },
136
137
  "peerDependencies": {
@@ -144,7 +145,7 @@
144
145
  "scripts": {
145
146
  "wxt": "tsx src/cli/index.ts",
146
147
  "build": "buildc -- unbuild",
147
- "check": "buildc --deps-only -- run-s -c check:*",
148
+ "check": "pnpm build && run-s -c check:*",
148
149
  "check:default": "check",
149
150
  "check:tsc-virtual": "tsc --noEmit -p src/virtual",
150
151
  "test": "buildc --deps-only -- vitest",