Skip to content

Commit

Permalink
feat: rollup: hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 67dd59f commit 43cb0da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,6 +44,12 @@ export interface BuildConfig extends Partial<Omit<BuildOptions, 'entries'>> {
export interface BuildHooks extends Hooks {
'build:before': (ctx: BuildContext) => void | Promise<void>
'build:after': (ctx: BuildContext) => void | Promise<void>

'rollup:options': (ctx: BuildContext, options: RollupOptions) => void | Promise<void>
'rollup:build': (ctx: BuildContext, build: RollupBuild) => void | Promise<void>
'rollup:dts:options': (ctx: BuildContext, options: RollupOptions) => void | Promise<void>
'rollup:dts:build': (ctx: BuildContext, build: RollupBuild) => void | Promise<void>
'rollup:done': (ctx: BuildContext) => void | Promise<void>
}

export function defineBuildConfig (config: BuildConfig): BuildConfig {
Expand Down

0 comments on commit 43cb0da

Please sign in to comment.