feat(hooks): Add prepare:publicPaths hook (#858)

This commit is contained in:
Aaron
2024-07-25 11:32:16 -05:00
committed by GitHub
parent ab234c0f45
commit d79d7ed778
3 changed files with 29 additions and 4 deletions
+5
View File
@@ -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,
@@ -75,7 +75,7 @@ export async function generateTypesDir(
async function getPathsDeclarationEntry(
entrypoints: Entrypoint[],
): Promise<WxtDirFileEntry> {
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
+17 -1
View File
@@ -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
*/