Skip to content
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
19 changes: 19 additions & 0 deletions packages/wxt/e2e/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const hooks: WxtHooks = {
'zip:sources:start': vi.fn(),
'zip:sources:done': vi.fn(),
'zip:done': vi.fn(),
'server:created': vi.fn(),
'server:started': vi.fn(),
'server:closed': vi.fn(),
};

function expectHooksToBeCalled(
Expand Down Expand Up @@ -67,6 +70,9 @@ describe('Hooks', () => {
'zip:sources:start': false,
'zip:sources:done': false,
'zip:done': false,
'server:created': false,
'server:started': false,
'server:closed': false,
});
});

Expand Down Expand Up @@ -95,6 +101,9 @@ describe('Hooks', () => {
'zip:sources:start': false,
'zip:sources:done': false,
'zip:done': false,
'server:created': false,
'server:started': false,
'server:closed': false,
});
});

Expand Down Expand Up @@ -123,6 +132,9 @@ describe('Hooks', () => {
'zip:sources:start': false,
'zip:sources:done': false,
'zip:done': true,
'server:created': false,
'server:started': false,
'server:closed': false,
});
});

Expand Down Expand Up @@ -151,6 +163,9 @@ describe('Hooks', () => {
'zip:sources:start': true,
'zip:sources:done': true,
'zip:done': true,
'server:created': false,
'server:started': false,
'server:closed': false,
});
});

Expand All @@ -164,6 +179,7 @@ describe('Hooks', () => {
disabled: true,
},
});
expect(hooks['server:closed']).not.toBeCalled();
await server.stop();

expectHooksToBeCalled({
Expand All @@ -185,6 +201,9 @@ describe('Hooks', () => {
'zip:sources:start': false,
'zip:sources:done': false,
'zip:done': false,
'server:created': 1,
'server:started': 1,
'server:closed': 1,
});
});
});
2 changes: 1 addition & 1 deletion packages/wxt/e2e/tests/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { normalizePath } from '../../src/core/utils/paths';

describe('Module Helpers', () => {
describe('options', () => {
it('should recieve the options defined in wxt.config.ts based on the configKey field', async () => {
it('should receive the options defined in wxt.config.ts based on the configKey field', async () => {
const options = { key: '123' };
const reportOptions = vi.fn();
vi.stubGlobal('reportOptions', reportOptions);
Expand Down
8 changes: 7 additions & 1 deletion packages/wxt/src/core/create-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function createServer(
): Promise<WxtDevServer> {
await registerWxt('serve', inlineConfig);

return (wxt.server = await createServerInternal());
wxt.server = await createServerInternal();
await wxt.hooks.callHook('server:created', wxt, wxt.server);
return wxt.server;
}

async function createServerInternal(): Promise<WxtDevServer> {
Expand Down Expand Up @@ -92,6 +94,8 @@ async function createServerInternal(): Promise<WxtDevServer> {

await builderServer.listen();
wxt.logger.success(`Started dev server @ ${server.origin}`);
await wxt.hooks.callHook('server:started', wxt, server);

await buildAndOpenBrowser();

// Register content scripts for the first time after the background starts up since they're not
Expand All @@ -109,6 +113,8 @@ async function createServerInternal(): Promise<WxtDevServer> {
wasStopped = true;
await runner.closeBrowser();
await builderServer.close();
await wxt.hooks.callHook('server:closed', wxt, server);

deinitWxtModules();
server.currentOutput = undefined;
},
Expand Down
26 changes: 20 additions & 6 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,8 @@ export type HookResult = Promise<void> | void;

export interface WxtHooks {
/**
* Called only one time after WXT initialization, when the WXT instance is ready to work.
* Called after WXT modules are initialized, when the WXT instance is ready to
* be used. `wxt.server` isn't available yet, use `server:created` to get it.
* @param wxt The configured WXT object
*/
ready: (wxt: Wxt) => HookResult;
Expand Down Expand Up @@ -1196,39 +1197,52 @@ export interface WxtHooks {
* @param wxt The configured WXT object
*/
'zip:start': (wxt: Wxt) => HookResult;

/**
* Called before zipping the extension files.
* @param wxt The configured WXT object
*/
'zip:extension:start': (wxt: Wxt) => HookResult;

/**
* Called after zipping the extension files.
* @param wxt The configured WXT object
* @param zipPath The path to the created extension zip file
*/
'zip:extension:done': (wxt: Wxt, zipPath: string) => HookResult;

/**
* Called before zipping the source files (for Firefox).
* @param wxt The configured WXT object
*/
'zip:sources:start': (wxt: Wxt) => HookResult;

/**
* Called after zipping the source files (for Firefox).
* @param wxt The configured WXT object
* @param zipPath The path to the created sources zip file
*/
'zip:sources:done': (wxt: Wxt, zipPath: string) => HookResult;

/**
* Called after the entire zip process is complete.
* @param wxt The configured WXT object
* @param zipFiles An array of paths to all created zip files
*/
'zip:done': (wxt: Wxt, zipFiles: string[]) => HookResult;
/**
* Called when the dev server is created (and `wxt.server` is assigned). Server has not been started yet.
* @param wxt The configured WXT object
* @param server Same as `wxt.server`, the object WXT uses to control the dev server.
*/
'server:created': (wxt: Wxt, server: WxtDevServer) => HookResult;
/**
* Called when the dev server is started.
* @param wxt The configured WXT object
* @param server Same as `wxt.server`, the object WXT uses to control the dev server.
*/
'server:started': (wxt: Wxt, server: WxtDevServer) => HookResult;
/**
* Called when the dev server is stopped.
* @param wxt The configured WXT object
* @param server Same as `wxt.server`, the object WXT uses to control the dev server.
*/
'server:closed': (wxt: Wxt, server: WxtDevServer) => HookResult;
}

export interface Wxt {
Expand Down