Skip to content

Commit

Permalink
feat: basic preset support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 0dfb39f commit 1c0f772
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
4 changes: 3 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { defineBuildConfig } from './src'

export default defineBuildConfig({
declaration: true,
emitCJS: false,
rollup: {
emitCJS: false
},
entries: [
'src/index',
'src/cli'
Expand Down
18 changes: 14 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ export async function build (rootDir: string, stub: boolean) {
rootDir = resolve(process.cwd(), rootDir || '.')

// Read build.config and package.json
const _require = jiti(rootDir)
const _require = jiti(rootDir, { interopDefault: true })
let buildConfigFile
try { buildConfigFile = _require.resolve('./build.config') } catch (e) {}
const buildConfig: BuildConfig = buildConfigFile ? _require('./build.config').default : {}
const buildConfig: BuildConfig = buildConfigFile ? _require('./build.config') : {}
const pkg = _require('./package.json')

// Resolve preset
let preset = buildConfig.preset || pkg.unbuild.preset || pkg.build.preset || {}
if (typeof preset === 'string') {
preset = _require(preset)
}

// Merge options
const options = defu(buildConfig, pkg.unbuild || pkg.build, <BuildOptions>{
const options = defu(buildConfig, pkg.unbuild || pkg.build, preset, <BuildOptions>{
rootDir,
entries: [],
clean: true,
Expand Down Expand Up @@ -56,6 +62,9 @@ export async function build (rootDir: string, stub: boolean) {
if (buildConfig.hooks) {
ctx.hooks.addHooks(buildConfig.hooks)
}
if (preset.hooks) {
ctx.hooks.addHooks(preset.hooks)
}

// Normalize entries
options.entries = options.entries.map(entry =>
Expand Down Expand Up @@ -143,8 +152,9 @@ export async function build (rootDir: string, stub: boolean) {

// Validate
validateDependencies(ctx)
consola.log('')

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

consola.log('')
}
9 changes: 5 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export interface MkdistBuildEntry extends BaseBuildEntry {
export type BuildEntry = BaseBuildEntry | RollupBuildEntry | UntypedBuildEntry | MkdistBuildEntry

export interface RollupBuildOptions {
emitCJS: boolean
cjsBridge: boolean
inlineDependencies: boolean
emitCJS?: boolean
cjsBridge?: boolean
inlineDependencies?: boolean
}

export interface BuildOptions {
Expand All @@ -57,7 +57,8 @@ export interface BuildContext {
}

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

Expand Down
9 changes: 2 additions & 7 deletions test/fixture/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import type { BuildConfig } from 'unbuild'

export default <BuildConfig>{
cjsBridge: true,
declaration: true,
preset: './build.preset',
entries: [
'src/index',
{ input: 'src/schema', builder: 'untyped' }
],
hooks: {
'build:before': () => { console.log('Before build') },
'build:after': () => { console.log('After build') }
}
]
}
10 changes: 10 additions & 0 deletions test/fixture/build.preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { BuildConfig } from 'unbuild'

export default <BuildConfig>{
cjsBridge: true,
declaration: true,
hooks: {
'build:before': () => { console.log('Before build') },
'build:done': () => { console.log('After build') }
}
}

0 comments on commit 1c0f772

Please sign in to comment.