Skip to content

Commit

Permalink
fix(builder): config resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcherGu committed Jul 25, 2022
1 parent b4b4d30 commit 8d68b47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
20 changes: 7 additions & 13 deletions packages/builder/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,14 @@ export async function resolveConfig(inlineConfig: InlineConfig, cwd: string = pr

config = cfgMod.default || cfgMod
}
else {
logger.warn(TAG, 'No config file loaded, using inline config\n')
}

config = merge(config, inlineConfig)

// resolve app type
const appType = inlineConfig.type || config.type || 'node'
const appType = config.type || 'node'

// resolve main file
const mainFile = await getMainFileAndCheck(cwd, inlineConfig.main || config.main)

// resolve user tsup options
config.entry = inlineConfig.entry || config.entry
config.outDir = inlineConfig.outDir || config.outDir
config.tsconfig = inlineConfig.tsconfig || config.tsconfig
config.external = inlineConfig.external || config.external
config.tsupConfig = inlineConfig.tsupConfig || config.tsupConfig
const mainFile = await getMainFileAndCheck(cwd, config.main)

// resolve entry file tsup config
if (!config.entry)
Expand All @@ -193,7 +185,9 @@ export async function resolveConfig(inlineConfig: InlineConfig, cwd: string = pr

// resolve electron preload file tsup config, entry must be specified
if (config.electron?.preload || inlineConfig.preload) {
const preloadConfig = config.electron?.preload || { entry: inlineConfig.preload }
let preloadConfig = { ...(config.electron?.preload || {}) }
if (inlineConfig.preload)
preloadConfig = { ...preloadConfig, entry: inlineConfig.preload }

if (preloadConfig.entry)
tsupConfigArr.push(await mergeTsupConfig(preloadConfig, cwd, tsupConfigArr[0]))
Expand Down
44 changes: 38 additions & 6 deletions packages/builder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,43 @@ export function arraify<T>(target: T | T[]): T[] {
return Array.isArray(target) ? target : [target]
}

export function merge<T>(obj1: T, obj2: T): T {
const result = Object.assign({}, obj1)
for (const key in obj2) {
if (obj2[key] !== undefined)
result[key] = obj2[key]
export function isObject(value: unknown): value is Record<string, any> {
return Object.prototype.toString.call(value) === '[object Object]'
}

export function merge(defaults: Record<string, any>,
overrides: Record<string, any>) {
const merged: Record<string, any> = { ...defaults }
for (const key in overrides) {
const value = overrides[key]
if (value == null)
continue

const existing = merged[key]

if (existing == null) {
merged[key] = value
continue
}
// fields that require special handling
if (key === 'entry') {
merged[key] = value || existing
continue
}

if (Array.isArray(existing) || Array.isArray(value)) {
merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])]
continue
}
if (isObject(existing) && isObject(value)) {
merged[key] = merge(
existing,
value,
)
continue
}

merged[key] = value
}
return result
return merged
}

0 comments on commit 8d68b47

Please sign in to comment.