Skip to content

Commit

Permalink
feat: support build:before and build:after hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 9a95278 commit c834e56
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"consola": "^2.15.3",
"defu": "^5.0.0",
"esbuild": "^0.13.14",
"hookable": "^5.0.0",
"jiti": "^1.12.9",
"magic-string": "^0.25.7",
"mkdirp": "^1.0.4",
Expand Down
31 changes: 22 additions & 9 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve, basename } from 'pathe'
import chalk from 'chalk'
import consola from 'consola'
import defu from 'defu'
import { createHooks } from 'hookable'
import prettyBytes from 'pretty-bytes'
import jiti from 'jiti'
import mkdirp from 'mkdirp'
Expand Down Expand Up @@ -40,6 +41,20 @@ export async function build (rootDir: string, stub: boolean) {
externals: [...Module.builtinModules]
}) as BuildOptions

// Build context
const ctx: BuildContext = {
options,
pkg,
buildEntries: [],
usedImports: new Set(),
hooks: createHooks()
}

// Register hooks
if (buildConfig.hooks) {
ctx.hooks.addHooks(buildConfig.hooks)
}

// Normalize entries
options.entries = options.entries.map(entry =>
typeof entry === 'string' ? { input: entry } : entry
Expand Down Expand Up @@ -73,6 +88,9 @@ export async function build (rootDir: string, stub: boolean) {
// Add dependencies from package.json as externals
options.externals.push(...options.dependencies)

// Call build:before
await ctx.hooks.callHook('build:before', ctx)

// Start info
consola.info(chalk.cyan(`${options.stub ? 'Stubbing' : 'Building'} ${pkg.name}`))
if (process.env.DEBUG) {
Expand All @@ -90,14 +108,6 @@ export async function build (rootDir: string, stub: boolean) {
}
}

// Build context
const ctx: BuildContext = {
options,
pkg,
buildEntries: [],
usedImports: new Set()
}

// Try to selflink
// if (ctx.stub && ctx.pkg.name) {
// const nodemodulesDir = resolve(ctx.rootDir, 'node_modules', ctx.pkg.name)
Expand All @@ -115,6 +125,7 @@ export async function build (rootDir: string, stub: boolean) {

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

Expand All @@ -130,6 +141,8 @@ export async function build (rootDir: string, stub: boolean) {

// Validate
validateDependencies(ctx)

consola.log('')

// Call build:after
await ctx.hooks.callHook('build:after', ctx)
}
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PackageJson } from 'pkg-types'
import type { Hooks, Hookable } from 'hookable'

export interface BuildEntry {
input: string
Expand Down Expand Up @@ -31,10 +32,17 @@ export interface BuildContext {
pkg: PackageJson,
buildEntries: { path: string, bytes?: number, exports?: string[], chunks?: string[] }[]
usedImports: Set<string>
hooks: Hookable<BuildHooks> // eslint-disable-line no-use-before-define
}

export interface BuildConfig extends Partial<Omit<BuildOptions, 'entries'>> {
entries: (BuildEntry | string)[],
hooks: BuildHooks // eslint-disable-line no-use-before-define
}

export interface BuildHooks extends Hooks {
'build:before': (ctx: BuildContext) => void | Promise<void>
'build:after': (ctx: BuildContext) => void | Promise<void>
}

export function defineBuildConfig (config: BuildConfig): BuildConfig {
Expand Down
6 changes: 5 additions & 1 deletion test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ export default <BuildConfig>{
entries: [
'src/index',
{ input: 'src/schema', builder: 'untyped' }
]
],
hooks: {
'build:before': () => { console.log('Before build') },
'build:after': () => { console.log('After build') }
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,11 @@ has@^1.0.3:
dependencies:
function-bind "^1.1.1"

hookable@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/hookable/-/hookable-5.0.0.tgz#bac6f1d4b56e3f590f21cfe3f813731372c0c69f"
integrity sha512-IqoJ8oXCNTUtNfqwbUQvLd+6ebVXk5qqGpSMOe4BS514vd4bEEH+hd9lva48mbbbe9q4eFKmsOViTZkr7ludHg==

hosted-git-info@^2.1.4:
version "2.8.9"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
Expand Down

0 comments on commit c834e56

Please sign in to comment.