Skip to content

Commit

Permalink
refactor!: extract rollup specific global options
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 4ef79b1 commit 0dfb39f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ export async function build (rootDir: string, stub: boolean) {
// Merge options
const options = defu(buildConfig, pkg.unbuild || pkg.build, <BuildOptions>{
rootDir,
outDir: 'dist',
emitCJS: true,
cjsBridge: false,
inlineDependencies: false,
entries: [],
clean: true,
declaration: false,
outDir: 'dist',
stub,
entries: [],
externals: [...Module.builtinModules],
dependencies: [],
devDependencies: [],
externals: [...Module.builtinModules]
rollup: {
emitCJS: true,
cjsBridge: false,
inlineDependencies: false
}
}) as BuildOptions

// Build context
Expand Down Expand Up @@ -81,11 +83,11 @@ export async function build (rootDir: string, stub: boolean) {
entry.outDir = resolve(options.rootDir, entry.outDir || options.outDir)
}

// Collect dependencies and devDependnecies
// Infer dependencies from pkg
options.dependencies = Object.keys(pkg.dependencies || {})
options.devDependencies = Object.keys(pkg.devDependencies || {})

// Add dependencies from package.json as externals
// Add all dependencies as externals
options.externals.push(...options.dependencies)

// Call build:before
Expand Down
8 changes: 4 additions & 4 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function rollupBuild (ctx: BuildContext) {
if (ctx.options.stub) {
for (const entry of ctx.options.entries.filter(entry => entry.builder === 'rollup')) {
const output = resolve(ctx.options.rootDir, ctx.options.outDir, entry.name!)
if (ctx.options.emitCJS) {
if (ctx.options.rollup.emitCJS) {
await writeFile(output + '.cjs', `module.exports = require('jiti')(null, { interopDefault: true })('${entry.input}')`)
}
await writeFile(output + '.mjs', `import jiti from 'jiti';\nexport default jiti(null, { interopDefault: true })('${entry.input}');`)
Expand Down Expand Up @@ -86,7 +86,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
),

output: [
ctx.options.emitCJS && {
ctx.options.rollup.emitCJS && {
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: '[name].cjs',
chunkFileNames: 'chunks/[name].cjs',
Expand Down Expand Up @@ -114,7 +114,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
if (isExplicitExternal) {
return true
}
if (ctx.options.inlineDependencies || id[0] === '.' || id[0] === '/' || id.match(/src[\\/]/) || id.startsWith(ctx.pkg.name!)) {
if (ctx.options.rollup.inlineDependencies || id[0] === '.' || id[0] === '/' || id.match(/src[\\/]/) || id.startsWith(ctx.pkg.name!)) {
return false
}
if (!isExplicitExternal) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
// Preserve dynamic imports for CommonJS
{ renderDynamicImport () { return { left: 'import(', right: ')' } } },

ctx.options.cjsBridge && cjsPlugin({}),
ctx.options.rollup.cjsBridge && cjsPlugin({}),

rawPlugin()
].filter(Boolean)
Expand Down
14 changes: 9 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ export interface MkdistBuildEntry extends BaseBuildEntry {

export type BuildEntry = BaseBuildEntry | RollupBuildEntry | UntypedBuildEntry | MkdistBuildEntry

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

export interface BuildOptions {
rootDir: string
declaration?: boolean
entries: BuildEntry[],
clean: boolean
declaration?: boolean
outDir: string
stub: boolean
externals: string[]
dependencies: string[],
devDependencies: string[]
externals: string[]
inlineDependencies: boolean
emitCJS: boolean
cjsBridge: boolean
rollup: RollupBuildOptions
}

export interface BuildContext {
Expand Down

0 comments on commit 0dfb39f

Please sign in to comment.