Skip to content

Commit a6969b0

Browse files
committed
fix: avoid leaking @types/node into client code
1 parent ad63b59 commit a6969b0

File tree

9 files changed

+231
-209
lines changed

9 files changed

+231
-209
lines changed

packages/wxt/src/client-types.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type {
2+
BackgroundEntrypointOptions,
3+
BaseEntrypointOptions,
4+
IsolatedWorldContentScriptEntrypointOptions,
5+
MainWorldContentScriptEntrypointOptions,
6+
} from './option-types';
7+
import type { ContentScriptContext } from './utils/content-script-context';
8+
9+
export type WxtPlugin = () => void;
10+
11+
export interface IsolatedWorldContentScriptDefinition
12+
extends IsolatedWorldContentScriptEntrypointOptions {
13+
/**
14+
* Main function executed when the content script is loaded.
15+
*
16+
* When running a content script with `browser.scripting.executeScript`,
17+
* values returned from this function will be returned in the `executeScript`
18+
* result as well. Otherwise returning a value does nothing.
19+
*/
20+
main(ctx: ContentScriptContext): any | Promise<any>;
21+
}
22+
23+
export interface MainWorldContentScriptDefinition
24+
extends MainWorldContentScriptEntrypointOptions {
25+
/**
26+
* Main function executed when the content script is loaded.
27+
*
28+
* When running a content script with `browser.scripting.executeScript`,
29+
* values returned from this function will be returned in the `executeScript`
30+
* result as well. Otherwise returning a value does nothing.
31+
*/
32+
main(): any | Promise<any>;
33+
}
34+
35+
export type ContentScriptDefinition =
36+
| IsolatedWorldContentScriptDefinition
37+
| MainWorldContentScriptDefinition;
38+
39+
export interface BackgroundDefinition extends BackgroundEntrypointOptions {
40+
/**
41+
* Main function executed when the background script is started. Cannot be async.
42+
*/
43+
main(): void;
44+
}
45+
46+
export interface UnlistedScriptDefinition extends BaseEntrypointOptions {
47+
/**
48+
* Main function executed when the unlisted script is ran.
49+
*
50+
* When running a content script with `browser.scripting.executeScript`,
51+
* values returned from this function will be returned in the `executeScript`
52+
* result as well. Otherwise returning a value does nothing.
53+
*/
54+
main(): any | Promise<any>;
55+
}

packages/wxt/src/core/resolve-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ async function getUnimportOptions(
361361
];
362362

363363
const defaultOptions: WxtResolvedUnimportOptions = {
364-
imports: [{ name: 'fakeBrowser', from: 'wxt/testing' }],
365364
presets: [
366365
{
367366
from: 'wxt/browser',

packages/wxt/src/option-types.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import type { ManifestContentScript } from "./core/utils/types";
2+
import type { Browser } from '@wxt-dev/browser';
3+
4+
export type TargetBrowser = string;
5+
6+
/**
7+
* Either a single value or a map of different browsers to the value for that browser.
8+
*/
9+
export type PerBrowserOption<T> = T | PerBrowserMap<T>;
10+
export type PerBrowserMap<T> = { [browser: TargetBrowser]: T };
11+
12+
export interface BaseEntrypointOptions {
13+
/**
14+
* List of target browsers to include this entrypoint in. Defaults to being included in all
15+
* builds. Cannot be used with `exclude`. You must choose one of the two options.
16+
*
17+
* @default undefined
18+
*/
19+
include?: TargetBrowser[];
20+
/**
21+
* List of target browsers to exclude this entrypoint from. Cannot be used with `include`. You
22+
* must choose one of the two options.
23+
*
24+
* @default undefined
25+
*/
26+
exclude?: TargetBrowser[];
27+
}
28+
29+
export interface BackgroundEntrypointOptions extends BaseEntrypointOptions {
30+
persistent?: PerBrowserOption<boolean>;
31+
/**
32+
* Set to `"module"` to output the background entrypoint as ESM. ESM outputs can share chunks and
33+
* reduce the overall size of the bundled extension.
34+
*
35+
* When `undefined`, the background is bundled individually into an IIFE format.
36+
*
37+
* @default undefined
38+
*/
39+
type?: PerBrowserOption<'module'>;
40+
}
41+
42+
export interface BaseContentScriptEntrypointOptions
43+
extends BaseEntrypointOptions {
44+
matches?: PerBrowserOption<NonNullable<ManifestContentScript['matches']>>;
45+
/**
46+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
47+
* @default "documentIdle"
48+
*/
49+
runAt?: PerBrowserOption<Browser.scripting.RegisteredContentScript['runAt']>;
50+
/**
51+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
52+
* @default false
53+
*/
54+
matchAboutBlank?: PerBrowserOption<
55+
ManifestContentScript['match_about_blank']
56+
>;
57+
/**
58+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
59+
* @default []
60+
*/
61+
excludeMatches?: PerBrowserOption<ManifestContentScript['exclude_matches']>;
62+
/**
63+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
64+
* @default []
65+
*/
66+
includeGlobs?: PerBrowserOption<ManifestContentScript['include_globs']>;
67+
/**
68+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
69+
* @default []
70+
*/
71+
excludeGlobs?: PerBrowserOption<ManifestContentScript['exclude_globs']>;
72+
/**
73+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
74+
* @default false
75+
*/
76+
allFrames?: PerBrowserOption<ManifestContentScript['all_frames']>;
77+
/**
78+
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
79+
* @default false
80+
*/
81+
matchOriginAsFallback?: PerBrowserOption<boolean>;
82+
/**
83+
* Customize how imported/generated styles are injected with the content script. Regardless of the
84+
* mode selected, CSS will always be built and included in the output directory.
85+
*
86+
* - `"manifest"` - Include the CSS in the manifest, under the content script's `css` array.
87+
* - `"manual"` - Exclude the CSS from the manifest. You are responsible for manually loading it
88+
* onto the page. Use `browser.runtime.getURL("content-scripts/<name>.css")` to get the file's
89+
* URL
90+
* - `"ui"` - Exclude the CSS from the manifest. CSS will be automatically added to your UI when
91+
* calling `createShadowRootUi`
92+
*
93+
* @default "manifest"
94+
*/
95+
cssInjectionMode?: PerBrowserOption<'manifest' | 'manual' | 'ui'>;
96+
/**
97+
* Specify how the content script is registered.
98+
*
99+
* - `"manifest"`: The content script will be added to the `content_scripts` entry in the
100+
* manifest. This is the normal and most well known way of registering a content script.
101+
* - `"runtime"`: The content script's `matches` is added to `host_permissions` and you are
102+
* responsible for using the scripting API to register/execute the content script
103+
* dynamically at runtime.
104+
*
105+
* @default "manifest"
106+
*/
107+
registration?: PerBrowserOption<'manifest' | 'runtime'>;
108+
}
109+
110+
export interface MainWorldContentScriptEntrypointOptions
111+
extends BaseContentScriptEntrypointOptions {
112+
/**
113+
* See https://developer.chrome.com/docs/extensions/develop/concepts/content-scripts#isolated_world
114+
*/
115+
world: 'MAIN';
116+
}
117+
118+
export interface IsolatedWorldContentScriptEntrypointOptions
119+
extends BaseContentScriptEntrypointOptions {
120+
/**
121+
* See https://developer.chrome.com/docs/extensions/develop/concepts/content-scripts#isolated_world
122+
* @default "ISOLATED"
123+
*/
124+
world?: 'ISOLATED';
125+
}
126+
127+
export interface PopupEntrypointOptions extends BaseEntrypointOptions {
128+
/**
129+
* Defaults to "browser_action" to be equivalent to MV3's "action" key
130+
*/
131+
mv2Key?: PerBrowserOption<'browser_action' | 'page_action'>;
132+
defaultIcon?: Record<string, string>;
133+
defaultTitle?: PerBrowserOption<string>;
134+
browserStyle?: PerBrowserOption<boolean>;
135+
}
136+
137+
export interface OptionsEntrypointOptions extends BaseEntrypointOptions {
138+
openInTab?: PerBrowserOption<boolean>;
139+
browserStyle?: PerBrowserOption<boolean>;
140+
chromeStyle?: PerBrowserOption<boolean>;
141+
}
142+
143+
export interface SidepanelEntrypointOptions extends BaseEntrypointOptions {
144+
/**
145+
* Firefox only. See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/sidebar_action#syntax
146+
* @default false
147+
*/
148+
openAtInstall?: PerBrowserOption<boolean>;
149+
/**
150+
* @deprecated See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/sidebar_action#syntax
151+
*/
152+
browserStyle?: PerBrowserOption<boolean>;
153+
defaultIcon?: string | Record<string, string>;
154+
defaultTitle?: PerBrowserOption<string>;
155+
}

0 commit comments

Comments
 (0)