diff --git a/packages/wxt/e2e/tests/hooks.test.ts b/packages/wxt/e2e/tests/hooks.test.ts index ff093d17..a00b986a 100644 --- a/packages/wxt/e2e/tests/hooks.test.ts +++ b/packages/wxt/e2e/tests/hooks.test.ts @@ -5,6 +5,7 @@ import { WxtHooks } from '../../src/types'; const hooks: WxtHooks = { ready: vi.fn(), 'prepare:types': vi.fn(), + 'prepare:publicPaths': vi.fn(), 'build:before': vi.fn(), 'build:done': vi.fn(), 'build:manifestGenerated': vi.fn(), @@ -43,6 +44,7 @@ describe('Hooks', () => { expectHooksToBeCalled({ ready: true, 'prepare:types': true, + 'prepare:publicPaths': true, 'build:before': false, 'build:done': false, 'build:publicAssets': false, @@ -63,6 +65,7 @@ describe('Hooks', () => { expectHooksToBeCalled({ ready: true, 'prepare:types': true, + 'prepare:publicPaths': true, 'build:before': true, 'build:done': true, 'build:publicAssets': true, @@ -83,6 +86,7 @@ describe('Hooks', () => { expectHooksToBeCalled({ ready: true, 'prepare:types': true, + 'prepare:publicPaths': true, 'build:before': true, 'build:done': true, 'build:publicAssets': true, @@ -109,6 +113,7 @@ describe('Hooks', () => { expectHooksToBeCalled({ ready: true, 'prepare:types': true, + 'prepare:publicPaths': true, 'build:before': true, 'build:done': true, 'build:publicAssets': true, diff --git a/packages/wxt/src/core/utils/building/generate-wxt-dir.ts b/packages/wxt/src/core/utils/building/generate-wxt-dir.ts index a91fa169..386dd55d 100644 --- a/packages/wxt/src/core/utils/building/generate-wxt-dir.ts +++ b/packages/wxt/src/core/utils/building/generate-wxt-dir.ts @@ -75,7 +75,7 @@ export async function generateTypesDir( async function getPathsDeclarationEntry( entrypoints: Entrypoint[], ): Promise { - const unions = entrypoints + const paths = entrypoints .map((entry) => getEntrypointBundlePath( entry, @@ -83,10 +83,14 @@ async function getPathsDeclarationEntry( isHtmlEntrypoint(entry) ? '.html' : '.js', ), ) - .concat(await getPublicFiles()) + .concat(await getPublicFiles()); + + await wxt.hooks.callHook('prepare:publicPaths', wxt, paths); + + const unions = paths .map(normalizePath) - .map((path) => ` | "/${path}"`) .sort() + .map((path) => ` | "/${path}"`) .join('\n'); const template = `// Generated by wxt diff --git a/packages/wxt/src/types.ts b/packages/wxt/src/types.ts index 47bf7064..6c9f22ee 100644 --- a/packages/wxt/src/types.ts +++ b/packages/wxt/src/types.ts @@ -1050,6 +1050,20 @@ export interface WxtHooks { * }) */ 'prepare:types': (wxt: Wxt, entries: WxtDirEntry[]) => HookResult; + /** + * Called before generating the list of public paths inside + * `.wxt/types/paths.d.ts`. Use this hook to add additional paths (relative + * to output directory) WXT doesn't add automatically. + * + * @param wxt The configured WXT object + * @param paths This list of paths TypeScript allows `browser.runtime.getURL` to be called with. + * + * @example + * wxt.hooks.hook('prepare:publicPaths', (wxt, paths) => { + * paths.push('/icons/128.png'); + * }) + */ + 'prepare:publicPaths': (wxt: Wxt, paths: string[]) => HookResult; /** * Called before the build is started in both dev mode and build mode. * @@ -1057,7 +1071,9 @@ export interface WxtHooks { */ 'build:before': (wxt: Wxt) => HookResult; /** - * Called once the build process has finished. + * Called once the build process has finished. You can add files to the build + * summary here by pushing to `output.publicAssets`. + * * @param wxt The configured WXT object * @param output The results of the build */