Skip to content

Commit cd64612

Browse files
committedDec 10, 2024··
feat: merge input & output options from user
1 parent a515e10 commit cd64612

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed
 

‎src/index.ts

+30-32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import path from 'node:path'
22
import process from 'node:process'
33
import { fileURLToPath } from 'node:url'
4-
import {
5-
build as rolldownBuild,
6-
type InputOptions,
7-
type OutputOptions,
8-
} from 'rolldown'
4+
import { build as rolldownBuild, type OutputOptions } from 'rolldown'
95
import { transformPlugin } from 'rolldown/experimental'
106
import { IsolatedDecl } from 'unplugin-isolated-decl'
117
import { Unused } from 'unplugin-unused'
@@ -17,6 +13,7 @@ import { getShimsInject } from './features/shims'
1713
import { shortcuts } from './features/shortcuts'
1814
import { watchBuild } from './features/watch'
1915
import {
16+
mergeUserOptions,
2017
resolveOptions,
2118
type Config,
2219
type Options,
@@ -118,36 +115,37 @@ export async function buildSingle(
118115

119116
await Promise.all(
120117
format.map(async (format) => {
121-
const inputOptions: InputOptions = {
122-
input: entry,
123-
external,
124-
resolve: { alias },
125-
treeshake,
126-
platform,
127-
define,
128-
plugins,
129-
...resolved.inputOptions,
130-
inject: {
131-
...(shims && getShimsInject(format, platform)),
132-
...resolved.inputOptions?.inject,
118+
const inputOptions = await mergeUserOptions(
119+
{
120+
input: entry,
121+
external,
122+
resolve: { alias },
123+
treeshake,
124+
platform,
125+
define,
126+
plugins,
127+
inject: {
128+
...(shims && getShimsInject(format, platform)),
129+
},
133130
},
134-
}
131+
resolved.inputOptions,
132+
[format],
133+
)
135134

136135
const extension = resolveOutputExtension(pkg, format)
137-
let outputOptions: OutputOptions = {
138-
format,
139-
name: resolved.globalName,
140-
sourcemap,
141-
dir: outDir,
142-
minify,
143-
entryFileNames: `[name].${extension}`,
144-
chunkFileNames: `[name]-[hash].${extension}`,
145-
}
146-
const userOutputOptions =
147-
typeof resolved.outputOptions === 'function'
148-
? await resolved.outputOptions(outputOptions, format)
149-
: resolved.outputOptions
150-
outputOptions = { ...outputOptions, ...userOutputOptions }
136+
const outputOptions: OutputOptions = await mergeUserOptions(
137+
{
138+
format,
139+
name: resolved.globalName,
140+
sourcemap,
141+
dir: outDir,
142+
minify,
143+
entryFileNames: `[name].${extension}`,
144+
chunkFileNames: `[name]-[hash].${extension}`,
145+
},
146+
resolved.outputOptions,
147+
[format],
148+
)
151149

152150
await rolldownBuild({
153151
...inputOptions,

‎src/options.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ export interface Options {
6060
*/
6161
unused?: boolean | UnusedOptions
6262
watch?: boolean | string | string[]
63-
inputOptions?: InputOptions
63+
inputOptions?:
64+
| InputOptions
65+
| ((
66+
options: InputOptions,
67+
format: ModuleFormat,
68+
) => MaybePromise<InputOptions | void | null>)
6469
outputOptions?:
6570
| OutputOptions
6671
| ((
@@ -235,3 +240,17 @@ async function loadConfigFile(
235240
const file = sources[0]
236241
return [toArray(config), file]
237242
}
243+
244+
export async function mergeUserOptions<T extends object, A extends unknown[]>(
245+
defaults: T,
246+
user:
247+
| T
248+
| undefined
249+
| null
250+
| ((options: T, ...args: A) => MaybePromise<T | void | null>),
251+
args: A,
252+
): Promise<T> {
253+
const userOutputOptions =
254+
typeof user === 'function' ? await user(defaults, ...args) : user
255+
return { ...defaults, ...userOutputOptions }
256+
}

0 commit comments

Comments
 (0)
Please sign in to comment.