Skip to content

Commit f6477b0

Browse files
committed
feat: [v2] add "startup" hook to plugins (#411)
1 parent e217aea commit f6477b0

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@blinkk/root': patch
3+
---
4+
5+
feat: [v2] add "startup" hook to plugins

packages/root/src/cli/build.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ export async function build(rootProjectDir?: string, options?: BuildOptions) {
8282
}
8383

8484
const rootPlugins = rootConfig.plugins || [];
85+
86+
// Run any "startup" hooks.
87+
for (const plugin of rootPlugins) {
88+
if (typeof plugin.hooks?.startup === 'function') {
89+
await plugin.hooks.startup({command: 'build', rootConfig});
90+
}
91+
}
92+
8593
const viteConfig = rootConfig.vite || {};
8694
const vitePlugins = [
8795
...(viteConfig.plugins || []),

packages/root/src/cli/dev.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ export async function createDevServer(options?: {
135135
{type: 'dev', rootConfig}
136136
);
137137

138+
// Run any "startup" hooks.
139+
for (const plugin of plugins) {
140+
if (typeof plugin.hooks?.startup === 'function') {
141+
await plugin.hooks.startup({command: 'dev', rootConfig});
142+
}
143+
}
144+
138145
return server;
139146
}
140147

packages/root/src/cli/preview.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ export async function createPreviewServer(options: {
120120
plugins,
121121
{type: 'preview', rootConfig}
122122
);
123+
124+
// Run any "startup" hooks.
125+
for (const plugin of plugins) {
126+
if (typeof plugin.hooks?.startup === 'function') {
127+
await plugin.hooks.startup({command: 'preview', rootConfig});
128+
}
129+
}
130+
123131
return server;
124132
}
125133

packages/root/src/cli/start.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ export async function createProdServer(options: {
115115
plugins,
116116
{type: 'prod', rootConfig}
117117
);
118+
119+
// Run any "startup" hooks.
120+
for (const plugin of plugins) {
121+
if (typeof plugin.hooks?.startup === 'function') {
122+
await plugin.hooks.startup({command: 'start', rootConfig});
123+
}
124+
}
125+
118126
return server;
119127
}
120128

packages/root/src/core/plugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ export interface ConfigureServerOptions {
1515
}
1616

1717
export interface PluginHooks {
18+
/**
19+
* Startup hook called when root is initialized.
20+
*/
21+
startup?: (options: {
22+
command: string;
23+
rootConfig: RootConfig;
24+
}) => void | Promise<void>;
25+
1826
/**
1927
* Post-render hook that's called before the HTML is rendered to the response
2028
* object. If a string is returned from this hook, it will replace the
2129
* rendered HTML.
2230
*/
23-
preRender: (html: string) => void | string | Promise<string>;
31+
preRender?: (html: string) => void | string | Promise<string>;
2432
}
2533

2634
export interface Plugin {

0 commit comments

Comments
 (0)