Skip to content

Commit

Permalink
feat(rollup): replace and per-plugin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 14, 2021
1 parent 73f7ed8 commit 6d185b2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
21 changes: 17 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,26 @@ export async function build (rootDir: string, stub: boolean, inputConfig: BuildC
devDependencies: [],
peerDependencies: [],
alias: {},
replace: {},
rollup: {
esbuild: {
target: 'es2020'
},
emitCJS: false,
cjsBridge: false,
inlineDependencies: false
inlineDependencies: false,
// Plugins
replace: {
preventAssignment: true
},
alias: {},
resolve: {
preferBuiltins: true
},
json: {
preferConst: true
},
commonjs: {
ignoreTryCatch: true
},
esbuild: { target: 'es2020' }
}
}) as BuildOptions

Expand Down
31 changes: 22 additions & 9 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
import alias from '@rollup/plugin-alias'
import _esbuild from 'rollup-plugin-esbuild'
import dts from 'rollup-plugin-dts'
import replace from '@rollup/plugin-replace'
import { relative, resolve } from 'pathe'
import consola from 'consola'
import { getpkg, tryResolve } from '../utils'
Expand Down Expand Up @@ -145,29 +146,41 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
},

plugins: [
alias({
ctx.options.rollup.replace && replace({
...ctx.options.rollup.replace,
values: {
...ctx.options.replace,
...ctx.options.rollup.replace.values
}
}),

ctx.options.rollup.alias && alias({
...ctx.options.rollup.alias,
entries: {
[ctx.pkg.name!]: ctx.options.rootDir,
...ctx.options.alias
...ctx.options.alias,
...ctx.options.rollup.alias.entries
}
}),

nodeResolve({
ctx.options.rollup.resolve && nodeResolve({
extensions,
preferBuiltins: true
...ctx.options.rollup.resolve
}),

JSONPlugin({
preferConst: true
ctx.options.rollup.json && JSONPlugin({
...ctx.options.rollup.json
}),

shebangPlugin(),

esbuild(ctx.options.rollup.esbuild),
ctx.options.rollup.esbuild && esbuild({
...ctx.options.rollup.esbuild
}),

commonjs({
ctx.options.rollup.commonjs && commonjs({
extensions,
ignoreTryCatch: true
...ctx.options.rollup.commonjs
}),

// Preserve dynamic imports for CommonJS
Expand Down
18 changes: 16 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import type { Hookable } from 'hookable'
import type { RollupOptions, RollupBuild } from 'rollup'
import type { MkdistOptions } from 'mkdist'
import type { Options as EsbuildOptions } from 'rollup-plugin-esbuild'
import { Schema } from 'untyped'
import type { Schema } from 'untyped'
import type { RollupReplaceOptions } from '@rollup/plugin-replace'
import type { RollupAliasOptions } from '@rollup/plugin-alias'
import type { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve'
import type { RollupJsonOptions } from '@rollup/plugin-json'
import type commonjs from '@rollup/plugin-commonjs'

export type RollupCommonJSOptions = Parameters<typeof commonjs>[0] & {}

export interface BaseBuildEntry {
builder?: 'untyped' | 'rollup' | 'mkdist'
Expand Down Expand Up @@ -35,7 +42,13 @@ export interface RollupBuildOptions {
emitCJS?: boolean
cjsBridge?: boolean
inlineDependencies?: boolean
esbuild: EsbuildOptions
// Plugins
replace: RollupReplaceOptions | false
alias: RollupAliasOptions | false
resolve: RollupNodeResolveOptions | false
json: RollupJsonOptions | false
esbuild: EsbuildOptions | false
commonjs: RollupCommonJSOptions | false
}

export interface BuildOptions {
Expand All @@ -50,6 +63,7 @@ export interface BuildOptions {
peerDependencies: string[]
devDependencies: string[]
alias: { [find: string]: string },
replace: { [find: string]: string },
rollup: RollupBuildOptions
}

Expand Down

0 comments on commit 6d185b2

Please sign in to comment.