Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hooks): Move entrypoints:resolved before debug logs and add entrypoints:found #1292

Merged
merged 6 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/guide/essentials/wxt-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ This file could then be loaded at runtime:
const res = await fetch(browser.runtime.getURL('/some-text.txt'));
```

#### Add custom entrypoints

Once the existing files under the `entrypoints/` directory have been discovered, the `entrypoints:found` hook can be used to add custom entrypoints.

:::info
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.
:::

```ts
import { defineWxtModule } from 'wxt/modules';

export default defineWxtModule({
setup(wxt) {
wxt.hook('entrypoints:found', (_, entrypointInfos) => {
// Add your new entrypoint
entrypointInfos.push({
name: 'my-custom-script',
inputPath: 'path/to/custom-script.js',
type: 'content-script',
});
});
},
});
```

#### Generate runtime module

Create a file in `.wxt`, add an alias to import it, and add auto-imports for exported variables.
Expand Down
6 changes: 6 additions & 0 deletions packages/wxt/e2e/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const hooks: WxtHooks = {
'build:done': vi.fn(),
'build:manifestGenerated': vi.fn(),
'build:publicAssets': vi.fn(),
'entrypoints:found': vi.fn(),
'entrypoints:resolved': vi.fn(),
'entrypoints:grouped': vi.fn(),
'vite:build:extendConfig': vi.fn(),
Expand Down Expand Up @@ -60,6 +61,7 @@ describe('Hooks', () => {
'build:done': false,
'build:publicAssets': false,
'build:manifestGenerated': false,
'entrypoints:found': true,
'entrypoints:grouped': false,
'entrypoints:resolved': true,
'vite:build:extendConfig': false,
Expand Down Expand Up @@ -91,6 +93,7 @@ describe('Hooks', () => {
'build:done': true,
'build:publicAssets': true,
'build:manifestGenerated': true,
'entrypoints:found': true,
'entrypoints:grouped': true,
'entrypoints:resolved': true,
'vite:build:extendConfig': 1,
Expand Down Expand Up @@ -122,6 +125,7 @@ describe('Hooks', () => {
'build:done': true,
'build:publicAssets': true,
'build:manifestGenerated': true,
'entrypoints:found': true,
'entrypoints:grouped': true,
'entrypoints:resolved': true,
'vite:build:extendConfig': 1,
Expand Down Expand Up @@ -153,6 +157,7 @@ describe('Hooks', () => {
'build:done': true,
'build:publicAssets': true,
'build:manifestGenerated': true,
'entrypoints:found': 2,
'entrypoints:grouped': true,
'entrypoints:resolved': 2,
'vite:build:extendConfig': 1,
Expand Down Expand Up @@ -191,6 +196,7 @@ describe('Hooks', () => {
'build:done': true,
'build:publicAssets': true,
'build:manifestGenerated': true,
'entrypoints:found': true,
'entrypoints:grouped': true,
'entrypoints:resolved': true,
'vite:build:extendConfig': 2,
Expand Down
12 changes: 5 additions & 7 deletions packages/wxt/src/core/utils/building/find-entrypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BackgroundEntrypoint,
ContentScriptEntrypoint,
Entrypoint,
EntrypointInfo,
GenericEntrypoint,
OptionsEntrypoint,
PopupEntrypoint,
Expand Down Expand Up @@ -67,6 +68,8 @@ export async function findEntrypoints(): Promise<Entrypoint[]> {
return results;
}, []);

await wxt.hooks.callHook('entrypoints:found', wxt, entrypointInfos);

// Validation
preventNoEntrypoints(entrypointInfos);
preventDuplicateEntrypointNames(entrypointInfos);
Expand Down Expand Up @@ -137,6 +140,8 @@ export async function findEntrypoints(): Promise<Entrypoint[]> {
skipped: isEntrypointSkipped(entry),
}));

await wxt.hooks.callHook('entrypoints:resolved', wxt, entrypoints);

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

return entrypoints;
}

interface EntrypointInfo {
name: string;
inputPath: string;
type: Entrypoint['type'];
}

/** Returns a map of input paths to the file's options. */
async function importEntrypoints(infos: EntrypointInfo[]) {
const resMap: Record<string, Record<string, any> | undefined> = {};
Expand Down
13 changes: 13 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,13 @@ export type Entrypoint =
| OptionsEntrypoint
| SidepanelEntrypoint;

export interface EntrypointInfo {
name: string;
/** Absolute path to the entrypoint file. */
inputPath: string;
type: Entrypoint['type'];
}

export type EntrypointGroup = Entrypoint | Entrypoint[];

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