From 43cb0da484380d9208bd28a0f39bbcb6ae8ae113 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 29 Nov 2021 17:45:31 +0100 Subject: [PATCH] feat: `rollup:` hooks --- src/builder/rollup.ts | 13 ++++++++++--- src/types.ts | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 88ec0662..52a90af0 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -27,15 +27,20 @@ export async function rollupBuild (ctx: BuildContext) { await writeFile(output + '.mjs', `import jiti from 'jiti';\nexport default jiti(null, { interopDefault: true })('${entry.input}');`) await writeFile(output + '.d.ts', `export * from '${entry.input}'`) } + await ctx.hooks.callHook('rollup:done', ctx) return } const rollupOptions = getRollupOptions(ctx) + await ctx.hooks.callHook('rollup:options', ctx, rollupOptions) + if (!Object.keys(rollupOptions.input as any).length) { return } const buildResult = await rollup(rollupOptions) + await ctx.hooks.callHook('rollup:build', ctx, buildResult) + const allOutputOptions = rollupOptions.output! as OutputOptions[] for (const outputOptions of allOutputOptions) { const { output } = await buildResult.write(outputOptions) @@ -56,15 +61,17 @@ export async function rollupBuild (ctx: BuildContext) { // Types if (ctx.options.declaration) { rollupOptions.plugins = rollupOptions.plugins || [] - rollupOptions.plugins.push(dts({ - respectExternal: true - })) + rollupOptions.plugins.push(dts({ respectExternal: true })) + await ctx.hooks.callHook('rollup:dts:options', ctx, rollupOptions) const typesBuild = await rollup(rollupOptions) + await ctx.hooks.callHook('rollup:dts:build', ctx, typesBuild) await typesBuild.write({ dir: resolve(ctx.options.rootDir, ctx.options.outDir), format: 'esm' }) } + + await ctx.hooks.callHook('rollup:done', ctx) } export function getRollupOptions (ctx: BuildContext): RollupOptions { diff --git a/src/types.ts b/src/types.ts index 59878566..544b89a8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ import type { PackageJson } from 'pkg-types' import type { Hooks, Hookable } from 'hookable' +import type { RollupOptions, RollupBuild } from 'rollup' export interface BuildEntry { input: string @@ -43,6 +44,12 @@ export interface BuildConfig extends Partial> { export interface BuildHooks extends Hooks { 'build:before': (ctx: BuildContext) => void | Promise 'build:after': (ctx: BuildContext) => void | Promise + + 'rollup:options': (ctx: BuildContext, options: RollupOptions) => void | Promise + 'rollup:build': (ctx: BuildContext, build: RollupBuild) => void | Promise + 'rollup:dts:options': (ctx: BuildContext, options: RollupOptions) => void | Promise + 'rollup:dts:build': (ctx: BuildContext, build: RollupBuild) => void | Promise + 'rollup:done': (ctx: BuildContext) => void | Promise } export function defineBuildConfig (config: BuildConfig): BuildConfig {