wxt 0.19.3-alpha2 → 0.19.4

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.
@@ -1,3 +1,4 @@
1
+ import type { WxtRuntime, WxtI18n } from './index';
1
2
  /**
2
3
  * EXPERIMENTAL
3
4
  *
@@ -5,10 +6,6 @@
5
6
  *
6
7
  * @module wxt/browser/chrome
7
8
  */
8
- export interface WxtRuntime {
9
- }
10
- export interface WxtI18n {
11
- }
12
9
  export type Chrome = typeof chrome;
13
10
  export type WxtBrowser = Omit<Chrome, 'runtime' | 'i18n'> & {
14
11
  runtime: WxtRuntime & Omit<Chrome['runtime'], 'getURL'>;
@@ -3,14 +3,14 @@
3
3
  *
4
4
  * @module wxt/browser
5
5
  */
6
- import { Browser, Runtime, I18n } from 'webextension-polyfill';
7
- export interface AugmentedBrowser extends Browser {
8
- runtime: WxtRuntime;
9
- i18n: WxtI18n;
6
+ import { Browser } from 'webextension-polyfill';
7
+ export type AugmentedBrowser = Omit<Browser, 'runtime' | 'i18n'> & {
8
+ runtime: WxtRuntime & Omit<Browser['runtime'], 'getURL'>;
9
+ i18n: WxtI18n & Omit<Browser['i18n'], 'getMessage'>;
10
+ };
11
+ export interface WxtRuntime {
10
12
  }
11
- export interface WxtRuntime extends Runtime.Static {
12
- }
13
- export interface WxtI18n extends I18n.Static {
13
+ export interface WxtI18n {
14
14
  }
15
15
  export declare const browser: AugmentedBrowser;
16
16
  /** @ignore */
@@ -71,7 +71,7 @@ export type IframeContentScriptUiOptions<TMounted> = ContentScriptUiOptions<TMou
71
71
  * The path to the HTML page that will be shown in the iframe. This string is passed into
72
72
  * `browser.runtime.getURL`.
73
73
  */
74
- page: PublicPath;
74
+ page: import('wxt/browser').HtmlPublicPath;
75
75
  /**
76
76
  * Callback executed when mounting the UI. Use this function to customize the iframe or wrapper
77
77
  * element's appearance. It is called every time `ui.mount()` is called.
@@ -5,3 +5,4 @@
5
5
  */
6
6
  export * from './content-scripts';
7
7
  export * from './app-config';
8
+ export * from './inject-script';
@@ -1,2 +1,3 @@
1
1
  export * from "./content-scripts/index.mjs";
2
2
  export * from "./app-config.mjs";
3
+ export * from "./inject-script.mjs";
@@ -0,0 +1,18 @@
1
+ export type ScriptPublicPath = Extract<import('wxt/browser').PublicPath, `${string}.js`>;
2
+ /**
3
+ * This function can only be called inside content scripts.
4
+ *
5
+ * Inject an unlisted script into the page. Scripts are added to the `<head>`
6
+ * element or `document.documentElement` if there is no head.
7
+ *
8
+ * Make sure to add the injected script to your manifest's
9
+ * `web_accessible_resources`.
10
+ */
11
+ export declare function injectScript(path: ScriptPublicPath, options?: InjectScriptOptions): Promise<void>;
12
+ export interface InjectScriptOptions {
13
+ /**
14
+ * By default, the injected script is removed from the DOM after being
15
+ * injected. To disable this behavior, set this flag to true.
16
+ */
17
+ keepInDom?: boolean;
18
+ }
@@ -0,0 +1,14 @@
1
+ import { browser } from "wxt/browser";
2
+ export async function injectScript(path, options) {
3
+ const url = browser.runtime.getURL(path);
4
+ const script = document.createElement("script");
5
+ if (browser.runtime.getManifest().manifest_version === 2) {
6
+ script.innerHTML = await fetch(url).then((res) => res.text());
7
+ } else {
8
+ script.src = url;
9
+ }
10
+ if (!options?.keepInDom) {
11
+ script.onload = () => script.remove();
12
+ }
13
+ (document.head ?? document.documentElement).append(script);
14
+ }
@@ -15,7 +15,17 @@ import { wxt } from "../../wxt.mjs";
15
15
  import { createExtensionEnvironment } from "../environments/index.mjs";
16
16
  export async function findEntrypoints() {
17
17
  await fs.mkdir(wxt.config.wxtDir, { recursive: true });
18
- await fs.writeJson(resolve(wxt.config.wxtDir, "tsconfig.json"), {});
18
+ try {
19
+ await fs.writeJson(
20
+ resolve(wxt.config.wxtDir, "tsconfig.json"),
21
+ {},
22
+ { flag: "wx" }
23
+ );
24
+ } catch (err) {
25
+ if (!(err instanceof Error) || !("code" in err) || err.code !== "EEXIST") {
26
+ throw err;
27
+ }
28
+ }
19
29
  const relativePaths = await glob(Object.keys(PATH_GLOB_TO_TYPE_MAP), {
20
30
  cwd: wxt.config.entrypointsDir
21
31
  });
@@ -50,9 +50,9 @@ async function getPathsDeclarationEntry(entrypoints) {
50
50
  await wxt.hooks.callHook("prepare:publicPaths", wxt, paths);
51
51
  const unions = paths.map(normalizePath).sort().map((path2) => ` | "/${path2}"`).join("\n");
52
52
  const template = `// Generated by wxt
53
- import "${wxt.config.browserModule}";
53
+ import "wxt/browser";
54
54
 
55
- declare module "${wxt.config.browserModule}" {
55
+ declare module "wxt/browser" {
56
56
  export type PublicPath =
57
57
  {{ union }}
58
58
  type HtmlPublicPath = Extract<PublicPath, \`\${string}.html\`>
@@ -71,9 +71,9 @@ declare module "${wxt.config.browserModule}" {
71
71
  async function getI18nDeclarationEntry() {
72
72
  const defaultLocale = wxt.config.manifest.default_locale;
73
73
  const template = `// Generated by wxt
74
- import "${wxt.config.browserModule}";
74
+ import "wxt/browser";
75
75
 
76
- declare module "${wxt.config.browserModule}" {
76
+ declare module "wxt/browser" {
77
77
  /**
78
78
  * See https://developer.chrome.com/docs/extensions/reference/i18n/#method-getMessage
79
79
  */
@@ -134,7 +134,6 @@ export async function resolveConfig(inlineConfig, command) {
134
134
  userConfigMetadata: userConfigMetadata ?? {},
135
135
  alias,
136
136
  extensionApi,
137
- browserModule: extensionApi === "chrome" ? "wxt/browser/chrome" : "wxt/browser",
138
137
  entrypointLoader: mergedConfig.entrypointLoader ?? "vite-node",
139
138
  experimental: defu(mergedConfig.experimental, {}),
140
139
  dev: {