-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite-plugin-nitro): add support for running hooks during pre-ren…
…dering (#548) Co-authored-by: Q <q.ray@jbhunt.com>
- Loading branch information
1 parent
0fe63ba
commit 46af10e
Showing
5 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/vite-plugin-nitro/src/lib/hooks/post-rendering-hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Nitro, PrerenderRoute } from 'nitropack'; | ||
|
||
export function addPostRenderingHooks( | ||
nitro: Nitro, | ||
hooks: ((pr: PrerenderRoute) => Promise<void>)[] | ||
): void { | ||
hooks.forEach((hook: (preRoute: PrerenderRoute) => void) => { | ||
nitro.hooks.hook('prerender:generate', (route: PrerenderRoute) => { | ||
hook(route); | ||
}); | ||
}); | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/vite-plugin-nitro/src/lib/hooks/post-rendering-hooks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { addPostRenderingHooks } from './post-rendering-hook'; | ||
import { vi } from 'vitest'; | ||
import { Nitro } from 'nitropack'; | ||
|
||
describe('postRenderingHook', () => { | ||
const genRoute = { | ||
route: 'test/testRoute', | ||
contents: 'This is a test.', | ||
}; | ||
|
||
const nitroMock = { | ||
hooks: { | ||
hook: vi.fn((name: string, callback: (route) => void) => | ||
callback(genRoute) | ||
), | ||
}, | ||
} as Nitro; | ||
|
||
const mockFunc1 = vi.fn(); | ||
const mockFunc2 = vi.fn(); | ||
|
||
it('should not attempt to call nitro mocks if no callbacks provided', () => { | ||
addPostRenderingHooks(nitroMock, []); | ||
expect(nitroMock.hooks.hook).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call provided hooks', () => { | ||
addPostRenderingHooks(nitroMock, [mockFunc1, mockFunc2]); | ||
expect(mockFunc1).toHaveBeenCalledWith(genRoute); | ||
expect(mockFunc2).toHaveBeenCalled(genRoute); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters