Skip to content

Commit

Permalink
feat: merge input & output options from user
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Dec 10, 2024
1 parent a515e10 commit cd64612
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
62 changes: 30 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import {
build as rolldownBuild,
type InputOptions,
type OutputOptions,
} from 'rolldown'
import { build as rolldownBuild, type OutputOptions } from 'rolldown'
import { transformPlugin } from 'rolldown/experimental'
import { IsolatedDecl } from 'unplugin-isolated-decl'
import { Unused } from 'unplugin-unused'
Expand All @@ -17,6 +13,7 @@ import { getShimsInject } from './features/shims'
import { shortcuts } from './features/shortcuts'
import { watchBuild } from './features/watch'
import {
mergeUserOptions,
resolveOptions,
type Config,
type Options,
Expand Down Expand Up @@ -118,36 +115,37 @@ export async function buildSingle(

await Promise.all(
format.map(async (format) => {
const inputOptions: InputOptions = {
input: entry,
external,
resolve: { alias },
treeshake,
platform,
define,
plugins,
...resolved.inputOptions,
inject: {
...(shims && getShimsInject(format, platform)),
...resolved.inputOptions?.inject,
const inputOptions = await mergeUserOptions(
{
input: entry,
external,
resolve: { alias },
treeshake,
platform,
define,
plugins,
inject: {
...(shims && getShimsInject(format, platform)),
},
},
}
resolved.inputOptions,
[format],
)

const extension = resolveOutputExtension(pkg, format)
let outputOptions: OutputOptions = {
format,
name: resolved.globalName,
sourcemap,
dir: outDir,
minify,
entryFileNames: `[name].${extension}`,
chunkFileNames: `[name]-[hash].${extension}`,
}
const userOutputOptions =
typeof resolved.outputOptions === 'function'
? await resolved.outputOptions(outputOptions, format)
: resolved.outputOptions
outputOptions = { ...outputOptions, ...userOutputOptions }
const outputOptions: OutputOptions = await mergeUserOptions(
{
format,
name: resolved.globalName,
sourcemap,
dir: outDir,
minify,
entryFileNames: `[name].${extension}`,
chunkFileNames: `[name]-[hash].${extension}`,
},
resolved.outputOptions,
[format],
)

await rolldownBuild({
...inputOptions,
Expand Down
21 changes: 20 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export interface Options {
*/
unused?: boolean | UnusedOptions
watch?: boolean | string | string[]
inputOptions?: InputOptions
inputOptions?:
| InputOptions
| ((
options: InputOptions,
format: ModuleFormat,
) => MaybePromise<InputOptions | void | null>)
outputOptions?:
| OutputOptions
| ((
Expand Down Expand Up @@ -235,3 +240,17 @@ async function loadConfigFile(
const file = sources[0]
return [toArray(config), file]
}

export async function mergeUserOptions<T extends object, A extends unknown[]>(
defaults: T,
user:
| T
| undefined
| null
| ((options: T, ...args: A) => MaybePromise<T | void | null>),
args: A,
): Promise<T> {
const userOutputOptions =
typeof user === 'function' ? await user(defaults, ...args) : user
return { ...defaults, ...userOutputOptions }
}

0 comments on commit cd64612

Please sign in to comment.