From 9673c9c6423d6deabd8d0a2a5bbe38a3c5d28f52 Mon Sep 17 00:00:00 2001 From: Vida Xie Date: Sat, 11 Jan 2025 02:20:20 +0800 Subject: [PATCH] fix: merge options correctly --- package.json | 1 + pnpm-lock.yaml | 8 ++++++++ src/index.ts | 1 + src/normalizeOptions.ts | 21 +++++++++++++++++++-- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c9bc41e..8fbe8a5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dd41ec1..7804550 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,6 +24,9 @@ importers: '@vida0905/eslint-config': specifier: ^1.2.0 version: 1.2.0(@typescript-eslint/utils@8.19.0(eslint@9.17.0(jiti@1.21.6))(typescript@5.7.2))(@vue/compiler-sfc@3.5.13)(eslint-plugin-import-x@4.6.1(eslint@9.17.0(jiti@1.21.6))(typescript@5.7.2))(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.19.0(eslint@9.17.0(jiti@1.21.6))(typescript@5.7.2))(eslint@9.17.0(jiti@1.21.6)))(eslint@9.17.0(jiti@1.21.6))(typescript@5.7.2)(vitest@2.1.8(@types/node@22.10.4)) + es-toolkit: + specifier: ^1.31.0 + version: 1.31.0 eslint: specifier: ^9.17.0 version: 9.17.0(jiti@1.21.6) @@ -1271,6 +1274,9 @@ packages: resolution: {integrity: sha512-w+5mJ3GuFL+NjVtJlvydShqE1eN3h3PbI7/5LAsYJP/2qtuMXjfL2LpHSRqo4b4eSF5K/DH1JXKUAHSB2UW50g==} engines: {node: '>= 0.4'} + es-toolkit@1.31.0: + resolution: {integrity: sha512-vwS0lv/tzjM2/t4aZZRAgN9I9TP0MSkWuvt6By+hEXfG/uLs8yg2S1/ayRXH/x3pinbLgVJYT+eppueg3cM6tg==} + esbuild@0.21.5: resolution: {integrity: sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==} engines: {node: '>=12'} @@ -4115,6 +4121,8 @@ snapshots: is-date-object: 1.1.0 is-symbol: 1.1.1 + es-toolkit@1.31.0: {} + esbuild@0.21.5: optionalDependencies: '@esbuild/aix-ppc64': 0.21.5 diff --git a/src/index.ts b/src/index.ts index ac5f821..06a2a40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/normalizeOptions.ts b/src/normalizeOptions.ts index 5ecdca7..ec35c3c 100644 --- a/src/normalizeOptions.ts +++ b/src/normalizeOptions.ts @@ -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' @@ -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']) @@ -81,7 +96,9 @@ export function normalizeOptions(options: OxcResolverOptions | null = {}): OxcRe return { ...defaultOptions, - ...getBundlerConfig(options?.bundlerConfig), - ...options, + ...mergeOptions( + options, + getBundlerConfig(options?.bundlerConfig), + ), } }