Skip to content

Commit ac96b35

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

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
@@ -126,6 +126,13 @@ export async function createDevServer(options?: {
126126
{type: 'dev', rootConfig}
127127
);
128128

129+
// Run any "startup" hooks.
130+
for (const plugin of plugins) {
131+
if (typeof plugin.hooks?.startup === 'function') {
132+
await plugin.hooks.startup({command: 'dev', rootConfig});
133+
}
134+
}
135+
129136
return server;
130137
}
131138

packages/root/src/cli/preview.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ export async function createPreviewServer(options: {
113113
plugins,
114114
{type: 'preview', rootConfig}
115115
);
116+
117+
// Run any "startup" hooks.
118+
for (const plugin of plugins) {
119+
if (typeof plugin.hooks?.startup === 'function') {
120+
await plugin.hooks.startup({command: 'preview', rootConfig});
121+
}
122+
}
123+
116124
return server;
117125
}
118126

packages/root/src/cli/start.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export async function createProdServer(options: {
108108
plugins,
109109
{type: 'prod', rootConfig}
110110
);
111+
112+
// Run any "startup" hooks.
113+
for (const plugin of plugins) {
114+
if (typeof plugin.hooks?.startup === 'function') {
115+
await plugin.hooks.startup({command: 'start', rootConfig});
116+
}
117+
}
118+
111119
return server;
112120
}
113121

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)