Skip to content

Commit

Permalink
feat: mkdist: hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 43cb0da commit 46f8b96
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function build (rootDir: string, stub: boolean) {

// Skip rest for stub
if (options.stub) {
await ctx.hooks.callHook('build:after', ctx)
await ctx.hooks.callHook('build:done', ctx)
return
}

Expand Down
21 changes: 14 additions & 7 deletions src/builder/mkdist.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { mkdist } from 'mkdist'
import { mkdist, MkdistOptions } from 'mkdist'
import { symlink, rmdir } from '../utils'
import type { BuildContext } from '../types'

import type { BuildEntry, BuildContext } from '../types'
export async function mkdistBuild (ctx: BuildContext) {
for (const entry of ctx.options.entries.filter(e => e.builder === 'mkdist')) {
type MkdistEntry = BuildEntry & { builder: 'mkdist' }
const entries = ctx.options.entries.filter(e => e.builder === 'mkdist') as MkdistEntry[]
await ctx.hooks.callHook('mkdist:before', ctx, entries)
for (const entry of entries) {
const distDir = entry.outDir!
if (ctx.options.stub) {
await rmdir(distDir)
await symlink(entry.input, distDir)
} else {
const { writtenFiles } = await mkdist({
const mkdistOptions: MkdistOptions = {
rootDir: ctx.options.rootDir,
srcDir: entry.input,
distDir,
format: entry.format,
cleanDist: false,
declaration: entry.declaration,
// @ts-ignore
ext: entry.ext
})
}
await ctx.hooks.callHook('mkdist:options', ctx, mkdistOptions)
const output = await mkdist(mkdistOptions)
ctx.buildEntries.push({
path: distDir,
chunks: [`${writtenFiles.length} files`]
chunks: [`${output.writtenFiles.length} files`]
})
await ctx.hooks.callHook('mkdist:build', ctx, output)
}
}
await ctx.hooks.callHook('mkdist:done', ctx)
}
14 changes: 11 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PackageJson } from 'pkg-types'
import type { Hooks, Hookable } from 'hookable'
import type { Hookable } from 'hookable'
import type { RollupOptions, RollupBuild } from 'rollup'
import type { MkdistOptions } from 'mkdist'

export interface BuildEntry {
input: string
Expand All @@ -13,6 +14,8 @@ export interface BuildEntry {
ext?: 'cjs' | 'mjs' | 'js' | 'ts'
}

export type MkdistEntry = BuildEntry & { builder: 'mkdist' }

export interface BuildOptions {
rootDir: string
declaration?: boolean
Expand Down Expand Up @@ -41,15 +44,20 @@ export interface BuildConfig extends Partial<Omit<BuildOptions, 'entries'>> {
hooks?: BuildHooks // eslint-disable-line no-use-before-define
}

export interface BuildHooks extends Hooks {
export interface BuildHooks {
'build:before': (ctx: BuildContext) => void | Promise<void>
'build:after': (ctx: BuildContext) => void | Promise<void>
'build:done': (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>

'mkdist:before': (ctx: BuildContext, entries: MkdistEntry[]) => void | Promise<void>
'mkdist:options': (ctx: BuildContext, options: MkdistOptions) => void | Promise<void>
'mkdist:build': (ctx: BuildContext, output: { writtenFiles: string[] }) => void | Promise<void>
'mkdist:done': (ctx: BuildContext) => void | Promise<void>
}

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

0 comments on commit 46f8b96

Please sign in to comment.