wxt 0.5.5 → 0.5.6

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/client.d.ts CHANGED
@@ -1,5 +1,57 @@
1
1
  import { Manifest } from 'webextension-polyfill';
2
2
 
3
+ /**
4
+ * Extends [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
5
+ * Used to detect and stop content script code when the script is invalidated.
6
+ *
7
+ * It also provides several utilities like `ctx.setTimeout` and `ctx.setInterval` that should be used in
8
+ * content scripts instead of `window.setTimeout` or `window.setInterval`.
9
+ */
10
+ declare class ContentScriptContext extends AbortController {
11
+ #private;
12
+ private readonly contentScriptName;
13
+ static SCRIPT_STARTED_MESSAGE_TYPE: string;
14
+ constructor(contentScriptName: string);
15
+ get isInvalid(): boolean;
16
+ get isValid(): boolean;
17
+ /**
18
+ * Add a listener that is called when the content script's context is invalidated.
19
+ *
20
+ * @returns A function to remove the listener.
21
+ *
22
+ * @example
23
+ * browser.runtime.onMessage.addListener(cb);
24
+ * const removeInvalidatedListener = ctx.onInvalidated(() => {
25
+ * browser.runtime.onMessage.removeListener(cb);
26
+ * })
27
+ * // ...
28
+ * removeInvalidatedListener();
29
+ */
30
+ onInvalidated(cb: () => void): () => void;
31
+ /**
32
+ * Wrapper around `window.setInterval` that automatically clears the interval when invalidated.
33
+ */
34
+ setInterval(handler: () => void, timeout?: number): number;
35
+ /**
36
+ * Wrapper around `window.setTimeout` that automatically clears the interval when invalidated.
37
+ */
38
+ setTimeout(handler: () => void, timeout?: number): number;
39
+ /**
40
+ * Wrapper around `window.requestAnimationFrame` that automatically cancels the request when
41
+ * invalidated.
42
+ */
43
+ requestAnimationFrame(callback: FrameRequestCallback): number;
44
+ /**
45
+ * Wrapper around `window.requestIdleCallback` that automatically cancels the request when
46
+ * invalidated.
47
+ */
48
+ requestIdleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): number;
49
+ /**
50
+ * Abort the abort controller and execute all `onInvalidated` listeners.
51
+ */
52
+ notifyInvalidated(): void;
53
+ }
54
+
3
55
  type TargetBrowser = string;
4
56
  interface ContentScriptDefinition extends ExcludableEntrypoint {
5
57
  matches: Manifest.ContentScript['matches'];
@@ -46,7 +98,7 @@ interface ContentScriptDefinition extends ExcludableEntrypoint {
46
98
  /**
47
99
  * Main function executed when the content script is loaded.
48
100
  */
49
- main(): void | Promise<void>;
101
+ main(ctx: ContentScriptContext): void | Promise<void>;
50
102
  }
51
103
  interface BackgroundScriptDefintition extends ExcludableEntrypoint {
52
104
  type?: 'module';
package/dist/index.cjs CHANGED
@@ -492,6 +492,15 @@ function getGlobals(config) {
492
492
  }
493
493
  ];
494
494
  }
495
+ function getEntrypointGlobals(config, entrypointName) {
496
+ return [
497
+ {
498
+ name: "__ENTRYPOINT__",
499
+ value: entrypointName,
500
+ type: `string`
501
+ }
502
+ ];
503
+ }
495
504
 
496
505
  // src/core/utils/getInternalConfig.ts
497
506
  async function getInternalConfig(inlineConfig, command) {
@@ -851,6 +860,9 @@ async function buildSingleEntrypoint(entrypoint, config) {
851
860
  "process.env.NODE_ENV": JSON.stringify(config.mode)
852
861
  }
853
862
  };
863
+ for (const global of getEntrypointGlobals(config, entrypoint.name)) {
864
+ libMode.define[global.name] = JSON.stringify(global.value);
865
+ }
854
866
  const entryConfig = vite3.mergeConfig(
855
867
  libMode,
856
868
  config.vite
@@ -879,8 +891,12 @@ async function buildMultipleEntrypoints(entrypoints, config) {
879
891
  assetFileNames: "assets/[name]-[hash].[ext]"
880
892
  }
881
893
  }
882
- }
894
+ },
895
+ define: {}
883
896
  };
897
+ for (const global of getEntrypointGlobals(config, "html")) {
898
+ multiPage.define[global.name] = JSON.stringify(global.value);
899
+ }
884
900
  const entryConfig = vite3.mergeConfig(
885
901
  multiPage,
886
902
  config.vite
@@ -1403,7 +1419,7 @@ declare module "wxt/browser" {
1403
1419
  }
1404
1420
  async function writeGlobalsDeclarationFile(config) {
1405
1421
  const filePath = (0, import_path9.resolve)(config.typesDir, "globals.d.ts");
1406
- const globals = getGlobals(config);
1422
+ const globals = [...getGlobals(config), ...getEntrypointGlobals(config, "")];
1407
1423
  await writeFileIfDifferent(
1408
1424
  filePath,
1409
1425
  [
@@ -2287,7 +2303,7 @@ async function clean(root = process.cwd()) {
2287
2303
  }
2288
2304
 
2289
2305
  // package.json
2290
- var version2 = "0.5.5";
2306
+ var version2 = "0.5.6";
2291
2307
 
2292
2308
  // src/core/utils/defineConfig.ts
2293
2309
  function defineConfig(config) {