wxt 0.5.5 → 0.6.0-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/dist/index.d.cts CHANGED
@@ -3,6 +3,58 @@ import { Manifest, Scripting } from 'webextension-polyfill';
3
3
  import { UnimportOptions } from 'unimport';
4
4
  import { LogLevel } from 'consola';
5
5
 
6
+ /**
7
+ * Extends [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
8
+ * Used to detect and stop content script code when the script is invalidated.
9
+ *
10
+ * It also provides several utilities like `ctx.setTimeout` and `ctx.setInterval` that should be used in
11
+ * content scripts instead of `window.setTimeout` or `window.setInterval`.
12
+ */
13
+ declare class ContentScriptContext extends AbortController {
14
+ #private;
15
+ private readonly contentScriptName;
16
+ static SCRIPT_STARTED_MESSAGE_TYPE: string;
17
+ constructor(contentScriptName: string);
18
+ get isInvalid(): boolean;
19
+ get isValid(): boolean;
20
+ /**
21
+ * Add a listener that is called when the content script's context is invalidated.
22
+ *
23
+ * @returns A function to remove the listener.
24
+ *
25
+ * @example
26
+ * browser.runtime.onMessage.addListener(cb);
27
+ * const removeInvalidatedListener = ctx.onInvalidated(() => {
28
+ * browser.runtime.onMessage.removeListener(cb);
29
+ * })
30
+ * // ...
31
+ * removeInvalidatedListener();
32
+ */
33
+ onInvalidated(cb: () => void): () => void;
34
+ /**
35
+ * Wrapper around `window.setInterval` that automatically clears the interval when invalidated.
36
+ */
37
+ setInterval(handler: () => void, timeout?: number): number;
38
+ /**
39
+ * Wrapper around `window.setTimeout` that automatically clears the interval when invalidated.
40
+ */
41
+ setTimeout(handler: () => void, timeout?: number): number;
42
+ /**
43
+ * Wrapper around `window.requestAnimationFrame` that automatically cancels the request when
44
+ * invalidated.
45
+ */
46
+ requestAnimationFrame(callback: FrameRequestCallback): number;
47
+ /**
48
+ * Wrapper around `window.requestIdleCallback` that automatically cancels the request when
49
+ * invalidated.
50
+ */
51
+ requestIdleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): number;
52
+ /**
53
+ * Abort the abort controller and execute all `onInvalidated` listeners.
54
+ */
55
+ notifyInvalidated(): void;
56
+ }
57
+
6
58
  interface InlineConfig {
7
59
  /**
8
60
  * Your project's root directory containing the `package.json` used to fill out the
@@ -82,11 +134,13 @@ interface InlineConfig {
82
134
  */
83
135
  logger?: Logger;
84
136
  /**
85
- * Custom Vite options, see <https://vitejs.dev/config/shared-options.html>.
137
+ * Return custom Vite options from a function. See
138
+ * <https://vitejs.dev/config/shared-options.html>.
86
139
  *
87
- * [`root`](#root), [`configFile`](#configfile), and [`mode`](#mode) should be set in WXT's config.
140
+ * [`root`](#root), [`configFile`](#configfile), and [`mode`](#mode) should be set in WXT's config
141
+ * instead of Vite's.
88
142
  */
89
- vite?: WxtViteConfig | ((env: ConfigEnv) => WxtViteConfig | Promise<WxtViteConfig>);
143
+ vite?: (env: ConfigEnv) => WxtViteConfig | Promise<WxtViteConfig>;
90
144
  /**
91
145
  * Customize the `manifest.json` output. Can be an object, promise, or function that returns an
92
146
  * object or promise.
@@ -331,7 +385,7 @@ interface ContentScriptDefinition extends ExcludableEntrypoint {
331
385
  /**
332
386
  * Main function executed when the content script is loaded.
333
387
  */
334
- main(): void | Promise<void>;
388
+ main(ctx: ContentScriptContext): void | Promise<void>;
335
389
  }
336
390
  interface BackgroundScriptDefintition extends ExcludableEntrypoint {
337
391
  type?: 'module';
@@ -426,7 +480,7 @@ type EntrypointGroup = Entrypoint | Entrypoint[];
426
480
  */
427
481
  declare function clean(root?: string): Promise<void>;
428
482
 
429
- var version = "0.5.5";
483
+ var version = "0.6.0-alpha1";
430
484
 
431
485
  declare function defineConfig(config: UserConfig): UserConfig;
432
486
 
package/dist/index.d.ts CHANGED
@@ -3,6 +3,58 @@ import { Manifest, Scripting } from 'webextension-polyfill';
3
3
  import { UnimportOptions } from 'unimport';
4
4
  import { LogLevel } from 'consola';
5
5
 
6
+ /**
7
+ * Extends [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
8
+ * Used to detect and stop content script code when the script is invalidated.
9
+ *
10
+ * It also provides several utilities like `ctx.setTimeout` and `ctx.setInterval` that should be used in
11
+ * content scripts instead of `window.setTimeout` or `window.setInterval`.
12
+ */
13
+ declare class ContentScriptContext extends AbortController {
14
+ #private;
15
+ private readonly contentScriptName;
16
+ static SCRIPT_STARTED_MESSAGE_TYPE: string;
17
+ constructor(contentScriptName: string);
18
+ get isInvalid(): boolean;
19
+ get isValid(): boolean;
20
+ /**
21
+ * Add a listener that is called when the content script's context is invalidated.
22
+ *
23
+ * @returns A function to remove the listener.
24
+ *
25
+ * @example
26
+ * browser.runtime.onMessage.addListener(cb);
27
+ * const removeInvalidatedListener = ctx.onInvalidated(() => {
28
+ * browser.runtime.onMessage.removeListener(cb);
29
+ * })
30
+ * // ...
31
+ * removeInvalidatedListener();
32
+ */
33
+ onInvalidated(cb: () => void): () => void;
34
+ /**
35
+ * Wrapper around `window.setInterval` that automatically clears the interval when invalidated.
36
+ */
37
+ setInterval(handler: () => void, timeout?: number): number;
38
+ /**
39
+ * Wrapper around `window.setTimeout` that automatically clears the interval when invalidated.
40
+ */
41
+ setTimeout(handler: () => void, timeout?: number): number;
42
+ /**
43
+ * Wrapper around `window.requestAnimationFrame` that automatically cancels the request when
44
+ * invalidated.
45
+ */
46
+ requestAnimationFrame(callback: FrameRequestCallback): number;
47
+ /**
48
+ * Wrapper around `window.requestIdleCallback` that automatically cancels the request when
49
+ * invalidated.
50
+ */
51
+ requestIdleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): number;
52
+ /**
53
+ * Abort the abort controller and execute all `onInvalidated` listeners.
54
+ */
55
+ notifyInvalidated(): void;
56
+ }
57
+
6
58
  interface InlineConfig {
7
59
  /**
8
60
  * Your project's root directory containing the `package.json` used to fill out the
@@ -82,11 +134,13 @@ interface InlineConfig {
82
134
  */
83
135
  logger?: Logger;
84
136
  /**
85
- * Custom Vite options, see <https://vitejs.dev/config/shared-options.html>.
137
+ * Return custom Vite options from a function. See
138
+ * <https://vitejs.dev/config/shared-options.html>.
86
139
  *
87
- * [`root`](#root), [`configFile`](#configfile), and [`mode`](#mode) should be set in WXT's config.
140
+ * [`root`](#root), [`configFile`](#configfile), and [`mode`](#mode) should be set in WXT's config
141
+ * instead of Vite's.
88
142
  */
89
- vite?: WxtViteConfig | ((env: ConfigEnv) => WxtViteConfig | Promise<WxtViteConfig>);
143
+ vite?: (env: ConfigEnv) => WxtViteConfig | Promise<WxtViteConfig>;
90
144
  /**
91
145
  * Customize the `manifest.json` output. Can be an object, promise, or function that returns an
92
146
  * object or promise.
@@ -331,7 +385,7 @@ interface ContentScriptDefinition extends ExcludableEntrypoint {
331
385
  /**
332
386
  * Main function executed when the content script is loaded.
333
387
  */
334
- main(): void | Promise<void>;
388
+ main(ctx: ContentScriptContext): void | Promise<void>;
335
389
  }
336
390
  interface BackgroundScriptDefintition extends ExcludableEntrypoint {
337
391
  type?: 'module';
@@ -426,7 +480,7 @@ type EntrypointGroup = Entrypoint | Entrypoint[];
426
480
  */
427
481
  declare function clean(root?: string): Promise<void>;
428
482
 
429
- var version = "0.5.5";
483
+ var version = "0.6.0-alpha1";
430
484
 
431
485
  declare function defineConfig(config: UserConfig): UserConfig;
432
486
 
package/dist/index.js CHANGED
@@ -451,6 +451,15 @@ function getGlobals(config) {
451
451
  }
452
452
  ];
453
453
  }
454
+ function getEntrypointGlobals(config, entrypointName) {
455
+ return [
456
+ {
457
+ name: "__ENTRYPOINT__",
458
+ value: entrypointName,
459
+ type: `string`
460
+ }
461
+ ];
462
+ }
454
463
 
455
464
  // src/core/utils/getInternalConfig.ts
456
465
  async function getInternalConfig(inlineConfig, command) {
@@ -497,6 +506,7 @@ async function getInternalConfig(inlineConfig, command) {
497
506
  command,
498
507
  debug,
499
508
  entrypointsDir,
509
+ env,
500
510
  fsCache: createFsCache(wxtDir),
501
511
  imports: mergedConfig.imports ?? {},
502
512
  logger,
@@ -510,16 +520,12 @@ async function getInternalConfig(inlineConfig, command) {
510
520
  runnerConfig,
511
521
  srcDir,
512
522
  typesDir,
513
- vite: {},
523
+ vite: () => ({}),
514
524
  // Real value added after this object is initialized.
515
525
  wxtDir,
516
526
  zip: resolveInternalZipConfig(root, mergedConfig)
517
527
  };
518
- finalConfig.vite = await resolveInternalViteConfig(
519
- env,
520
- mergedConfig,
521
- finalConfig
522
- );
528
+ finalConfig.vite = (env2) => resolveInternalViteConfig(env2, mergedConfig, finalConfig);
523
529
  return finalConfig;
524
530
  }
525
531
  async function resolveManifestConfig(env, manifest) {
@@ -543,9 +549,9 @@ function mergeInlineConfig(inlineConfig, userConfig) {
543
549
  return vite2.mergeConfig(user, inline);
544
550
  };
545
551
  const viteConfig = async (env) => {
546
- const user = await resolveViteConfig(env, userConfig.vite);
547
- const inline = await resolveViteConfig(env, inlineConfig.vite);
548
- return vite2.mergeConfig(user, inline);
552
+ const user = await userConfig.vite?.(env);
553
+ const inline = await inlineConfig.vite?.(env);
554
+ return vite2.mergeConfig(user ?? {}, inline ?? {});
549
555
  };
550
556
  const runner = vite2.mergeConfig(
551
557
  userConfig.runner ?? {},
@@ -573,9 +579,6 @@ function mergeInlineConfig(inlineConfig, userConfig) {
573
579
  zip
574
580
  };
575
581
  }
576
- async function resolveViteConfig(env, vite6) {
577
- return await (typeof vite6 === "function" ? vite6(env) : vite6 ?? {});
578
- }
579
582
  function resolveInternalZipConfig(root, mergedConfig) {
580
583
  return {
581
584
  sourcesTemplate: "{{name}}-{{version}}-sources.zip",
@@ -597,10 +600,7 @@ function resolveInternalZipConfig(root, mergedConfig) {
597
600
  };
598
601
  }
599
602
  async function resolveInternalViteConfig(env, mergedConfig, finalConfig) {
600
- const internalVite = await resolveViteConfig(
601
- env,
602
- mergedConfig.vite
603
- );
603
+ const internalVite = await mergedConfig.vite?.(env) ?? {};
604
604
  internalVite.root = finalConfig.root;
605
605
  internalVite.configFile = false;
606
606
  internalVite.logLevel = "warn";
@@ -810,9 +810,12 @@ async function buildSingleEntrypoint(entrypoint, config) {
810
810
  "process.env.NODE_ENV": JSON.stringify(config.mode)
811
811
  }
812
812
  };
813
+ for (const global of getEntrypointGlobals(config, entrypoint.name)) {
814
+ libMode.define[global.name] = JSON.stringify(global.value);
815
+ }
813
816
  const entryConfig = vite3.mergeConfig(
814
817
  libMode,
815
- config.vite
818
+ await config.vite(config.env)
816
819
  );
817
820
  const result = await vite3.build(entryConfig);
818
821
  return {
@@ -838,11 +841,15 @@ async function buildMultipleEntrypoints(entrypoints, config) {
838
841
  assetFileNames: "assets/[name]-[hash].[ext]"
839
842
  }
840
843
  }
841
- }
844
+ },
845
+ define: {}
842
846
  };
847
+ for (const global of getEntrypointGlobals(config, "html")) {
848
+ multiPage.define[global.name] = JSON.stringify(global.value);
849
+ }
843
850
  const entryConfig = vite3.mergeConfig(
844
851
  multiPage,
845
- config.vite
852
+ await config.vite(config.env)
846
853
  );
847
854
  const result = await vite3.build(entryConfig);
848
855
  return {
@@ -1362,7 +1369,7 @@ declare module "wxt/browser" {
1362
1369
  }
1363
1370
  async function writeGlobalsDeclarationFile(config) {
1364
1371
  const filePath = resolve9(config.typesDir, "globals.d.ts");
1365
- const globals = getGlobals(config);
1372
+ const globals = [...getGlobals(config), ...getEntrypointGlobals(config, "")];
1366
1373
  await writeFileIfDifferent(
1367
1374
  filePath,
1368
1375
  [
@@ -2145,7 +2152,7 @@ async function getServerInfo() {
2145
2152
  async function setupServer(serverInfo, config) {
2146
2153
  const runner = createWebExtRunner();
2147
2154
  const viteServer = await vite5.createServer(
2148
- vite5.mergeConfig(serverInfo, config.vite)
2155
+ vite5.mergeConfig(serverInfo, await config.vite(config.env))
2149
2156
  );
2150
2157
  const start = async () => {
2151
2158
  await viteServer.listen(server.port);
@@ -2246,7 +2253,7 @@ async function clean(root = process.cwd()) {
2246
2253
  }
2247
2254
 
2248
2255
  // package.json
2249
- var version2 = "0.5.5";
2256
+ var version2 = "0.6.0-alpha1";
2250
2257
 
2251
2258
  // src/core/utils/defineConfig.ts
2252
2259
  function defineConfig(config) {
@@ -2269,7 +2276,7 @@ async function createServer2(config) {
2269
2276
  return getInternalConfig(
2270
2277
  {
2271
2278
  ...config,
2272
- vite: serverInfo.viteServerConfig
2279
+ vite: () => serverInfo.viteServerConfig
2273
2280
  },
2274
2281
  "serve"
2275
2282
  );