Skip to content

Commit

Permalink
fix: merge options correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
9romise committed Jan 10, 2025
1 parent e8c9118 commit 9673c9c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"devDependencies": {
"@types/node": "^22.10.4",
"@vida0905/eslint-config": "^1.2.0",
"es-toolkit": "^1.31.0",
"eslint": "^9.17.0",
"eslint-vitest-rule-tester": "^0.7.1",
"lint-staged": "^15.3.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { normalizeOptions } from './normalizeOptions'
import { hashObject } from './utils'

export { transformViteConfig } from './bundler/vite'
export { mergeOptions } from './normalizeOptions'

let cachedOptionsHash: string | undefined
let cachedResolver: ResolverFactory | undefined
Expand Down
21 changes: 19 additions & 2 deletions src/normalizeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { OxcResolverOptions } from './typings'
import { cwd } from 'node:process'
import { isPlainObject, mergeWith } from 'es-toolkit'
import { getBundlerConfig } from './bundler'
import { detectFile } from './utils'

Expand Down Expand Up @@ -68,6 +69,20 @@ const defaultOptions: OxcResolverOptions = {
roots: [cwd()],
}

export function mergeOptions(a?: OxcResolverOptions | null, b?: OxcResolverOptions | null): OxcResolverOptions {
a ??= {}
b ??= {}

function mergeFunc(objVal: any, srcVal: any) {
if (Array.isArray(objVal) || Array.isArray(srcVal)) {
return objVal.concat(srcVal)
} else if (isPlainObject(objVal) && isPlainObject(srcVal)) {
return mergeWith(objVal, srcVal, mergeFunc)
}
}
return mergeWith(a, b, mergeFunc)
}

export function normalizeOptions(options: OxcResolverOptions | null = {}): OxcResolverOptions {
if (!options?.tsconfig) {
const configFile = detectFile(['tsconfig.json', 'jsconfig.json'])
Expand All @@ -81,7 +96,9 @@ export function normalizeOptions(options: OxcResolverOptions | null = {}): OxcRe

return {
...defaultOptions,
...getBundlerConfig(options?.bundlerConfig),
...options,
...mergeOptions(
options,
getBundlerConfig(options?.bundlerConfig),
),
}
}

0 comments on commit 9673c9c

Please sign in to comment.