Skip to content

Commit

Permalink
feat(plugin): add buildStart hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 28, 2024
1 parent 506445e commit 6a360d9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-mugs-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat(plugin): add buildStart hook
16 changes: 14 additions & 2 deletions docs/plugin.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# Plugin

## Properties

### name

- Type: `string`

Plugin name.

### enforce

- Type: `'pre' | 'post'`

Plugin execution order.

## General Hooks

### buildStart

> Not implemented
- Type: `(ctx: { command: string }) => void | Promise<void>`

Called when build start.
Expand Down
3 changes: 3 additions & 0 deletions examples/normal/.tnfrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default defineConfig({
},
plugins: [
{
buildStart: ({ command }) => {
console.log('buildStart', command);
},
buildEnd: ({ command, err }) => {
console.log('buildEnd', command, err);
},
Expand Down
12 changes: 11 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { debug, error, info, warn } from './fishkit/logger';
import * as logger from './fishkit/logger';
import { checkVersion, setNoDeprecation, setNodeTitle } from './fishkit/node';
import { mock } from './funplugins/mock/mock';
import { PluginManager } from './plugin/plugin_manager';
import { PluginHookType, PluginManager } from './plugin/plugin_manager';
import { type Context, Mode } from './types';

async function buildContext(cwd: string): Promise<Context> {
Expand Down Expand Up @@ -46,6 +46,16 @@ async function run(cwd: string) {
const context = await buildContext(cwd);
const cmd = context.argv._[0];
assert(cmd, 'Command is required');

if (cmd === 'build' || cmd === 'dev') {
await context.pluginManager.apply({
hook: 'buildStart',
args: [{ command: cmd }],
type: PluginHookType.Parallel,
pluginContext: context.pluginContext,
});
}

switch (cmd) {
case 'build':
const { build } = await import('./build.js');
Expand Down

0 comments on commit 6a360d9

Please sign in to comment.