Skip to content

Commit 06c09e7

Browse files
feat(hooks): Move entrypoints:resolved before debug logs and add entrypoints:found (#1292)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
1 parent 30b96ac commit 06c09e7

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

docs/guide/essentials/wxt-modules.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,31 @@ This file could then be loaded at runtime:
167167
const res = await fetch(browser.runtime.getURL('/some-text.txt'));
168168
```
169169

170+
#### Add custom entrypoints
171+
172+
Once the existing files under the `entrypoints/` directory have been discovered, the `entrypoints:found` hook can be used to add custom entrypoints.
173+
174+
:::info
175+
The `entrypoints:found` hook is triggered before validation is carried out on the list of entrypoints. Thus, any custom entrypoints will still be checked for duplicate names and logged during debugging.
176+
:::
177+
178+
```ts
179+
import { defineWxtModule } from 'wxt/modules';
180+
181+
export default defineWxtModule({
182+
setup(wxt) {
183+
wxt.hook('entrypoints:found', (_, entrypointInfos) => {
184+
// Add your new entrypoint
185+
entrypointInfos.push({
186+
name: 'my-custom-script',
187+
inputPath: 'path/to/custom-script.js',
188+
type: 'content-script',
189+
});
190+
});
191+
},
192+
});
193+
```
194+
170195
#### Generate runtime module
171196

172197
Create a file in `.wxt`, add an alias to import it, and add auto-imports for exported variables.

packages/wxt/e2e/tests/hooks.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const hooks: WxtHooks = {
1111
'build:done': vi.fn(),
1212
'build:manifestGenerated': vi.fn(),
1313
'build:publicAssets': vi.fn(),
14+
'entrypoints:found': vi.fn(),
1415
'entrypoints:resolved': vi.fn(),
1516
'entrypoints:grouped': vi.fn(),
1617
'vite:build:extendConfig': vi.fn(),
@@ -60,6 +61,7 @@ describe('Hooks', () => {
6061
'build:done': false,
6162
'build:publicAssets': false,
6263
'build:manifestGenerated': false,
64+
'entrypoints:found': true,
6365
'entrypoints:grouped': false,
6466
'entrypoints:resolved': true,
6567
'vite:build:extendConfig': false,
@@ -91,6 +93,7 @@ describe('Hooks', () => {
9193
'build:done': true,
9294
'build:publicAssets': true,
9395
'build:manifestGenerated': true,
96+
'entrypoints:found': true,
9497
'entrypoints:grouped': true,
9598
'entrypoints:resolved': true,
9699
'vite:build:extendConfig': 1,
@@ -122,6 +125,7 @@ describe('Hooks', () => {
122125
'build:done': true,
123126
'build:publicAssets': true,
124127
'build:manifestGenerated': true,
128+
'entrypoints:found': true,
125129
'entrypoints:grouped': true,
126130
'entrypoints:resolved': true,
127131
'vite:build:extendConfig': 1,
@@ -153,6 +157,7 @@ describe('Hooks', () => {
153157
'build:done': true,
154158
'build:publicAssets': true,
155159
'build:manifestGenerated': true,
160+
'entrypoints:found': 2,
156161
'entrypoints:grouped': true,
157162
'entrypoints:resolved': 2,
158163
'vite:build:extendConfig': 1,
@@ -191,6 +196,7 @@ describe('Hooks', () => {
191196
'build:done': true,
192197
'build:publicAssets': true,
193198
'build:manifestGenerated': true,
199+
'entrypoints:found': true,
194200
'entrypoints:grouped': true,
195201
'entrypoints:resolved': true,
196202
'vite:build:extendConfig': 2,

packages/wxt/src/core/utils/building/find-entrypoints.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
BackgroundEntrypoint,
44
ContentScriptEntrypoint,
55
Entrypoint,
6+
EntrypointInfo,
67
GenericEntrypoint,
78
OptionsEntrypoint,
89
PopupEntrypoint,
@@ -67,6 +68,8 @@ export async function findEntrypoints(): Promise<Entrypoint[]> {
6768
return results;
6869
}, []);
6970

71+
await wxt.hooks.callHook('entrypoints:found', wxt, entrypointInfos);
72+
7073
// Validation
7174
preventNoEntrypoints(entrypointInfos);
7275
preventDuplicateEntrypointNames(entrypointInfos);
@@ -137,6 +140,8 @@ export async function findEntrypoints(): Promise<Entrypoint[]> {
137140
skipped: isEntrypointSkipped(entry),
138141
}));
139142

143+
await wxt.hooks.callHook('entrypoints:resolved', wxt, entrypoints);
144+
140145
wxt.logger.debug('All entrypoints:', entrypoints);
141146
const skippedEntrypointNames = entrypoints
142147
.filter((item) => item.skipped)
@@ -151,17 +156,10 @@ export async function findEntrypoints(): Promise<Entrypoint[]> {
151156
].join('\n'),
152157
);
153158
}
154-
await wxt.hooks.callHook('entrypoints:resolved', wxt, entrypoints);
155159

156160
return entrypoints;
157161
}
158162

159-
interface EntrypointInfo {
160-
name: string;
161-
inputPath: string;
162-
type: Entrypoint['type'];
163-
}
164-
165163
/** Returns a map of input paths to the file's options. */
166164
async function importEntrypoints(infos: EntrypointInfo[]) {
167165
const resMap: Record<string, Record<string, any> | undefined> = {};

packages/wxt/src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,13 @@ export type Entrypoint =
789789
| OptionsEntrypoint
790790
| SidepanelEntrypoint;
791791

792+
export interface EntrypointInfo {
793+
name: string;
794+
/** Absolute path to the entrypoint file. */
795+
inputPath: string;
796+
type: Entrypoint['type'];
797+
}
798+
792799
export type EntrypointGroup = Entrypoint | Entrypoint[];
793800

794801
export type OnContentScriptStopped = (cb: () => void) => void;
@@ -1182,6 +1189,12 @@ export interface WxtHooks {
11821189
wxt: Wxt,
11831190
manifest: Manifest.WebExtensionManifest,
11841191
) => HookResult;
1192+
/**
1193+
* Called once the names and paths of all entrypoints have been resolved.
1194+
* @param wxt The configured WXT object
1195+
* @param infos List of entrypoints found in the project's `entrypoints` directory
1196+
*/
1197+
'entrypoints:found': (wxt: Wxt, infos: EntrypointInfo[]) => HookResult;
11851198
/**
11861199
* Called once all entrypoints have been loaded from the `entrypointsDir`.
11871200
* Use `wxt.builder.importEntrypoint` to load entrypoint options from the

0 commit comments

Comments
 (0)