From 73f550ad2598313813731fcc2f135b5c29e3a86e Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 10 Sep 2021 23:53:46 +0800 Subject: [PATCH] feat: support `buildStart` hook, close #14 --- README.md | 6 ++++-- src/types.ts | 1 + src/webpack/index.ts | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0ee6af32..9830b8d6 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,15 @@ Currently supports: | Hook | Rollup | Vite | Webpack | | ---- | ------ | ---- | ------- | -| `transformInclude` | ✅ | ✅ | ✅ | +| [`buildStart`](https://rollupjs.org/guide/en/#buildstart) | ✅ | ✅ | ✅ | +| `transformInclude`* | ✅ | ✅ | ✅ | | [`transform`](https://rollupjs.org/guide/en/#transformers) | ✅ | ✅ | ✅ | | [`enforce`](https://rollupjs.org/guide/en/#enforce) | ❌* | ✅ | ✅ | | [`resolveId`](https://rollupjs.org/guide/en/#resolveid) | ✅ | ✅ | ✅ | | [`load`](https://rollupjs.org/guide/en/#load) | ✅ | ✅ | ✅ | -- *: Rollup does not support using `enforce` to control the order of plugins. Users will need to maintain the order manually. +- *: Webpack's id filter is outside of loader logic; an additional hook is needed for better perf on Webpack. In Rollup and Vite, this hook has been polyfilled to match the behaviors. See for following usage examples. +- **: Rollup does not support using `enforce` to control the order of plugins. Users need to maintain the order manually. ## Usage diff --git a/src/types.ts b/src/types.ts index d248a3be..b60b9ade 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,6 +16,7 @@ export type TransformResult = string | { code: string; map?: SourceMap | null; } export interface UnpluginOptions { name: string; enforce?: 'post' | 'pre' | undefined; + buildStart?: () => Promise | void; transformInclude?: (id: string) => boolean; transform?: (this: UnpluginContext, code: string, id: string) => Thenable; load?: (this: UnpluginContext, id: string) => Thenable diff --git a/src/webpack/index.ts b/src/webpack/index.ts index b325a3f3..bc36cb2f 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -137,6 +137,8 @@ export function getWebpackPlugin ( if (plugin.webpack) { plugin.webpack(compiler) } + + plugin.buildStart?.() } } }