diff --git a/packages/rstack/src/rslibConfig.ts b/packages/rstack/src/rslibConfig.ts index cf581a1..3faaf14 100644 --- a/packages/rstack/src/rslibConfig.ts +++ b/packages/rstack/src/rslibConfig.ts @@ -1,28 +1,24 @@ -import type { - ConfigParams, - RslibConfig, - RslibConfigDefinition, +import { + type ConfigParams, + type RslibConfig, + type RslibConfigDefinition, + mergeRslibConfig, } from '@rslib/core'; import { withConfigMeta } from '@rstackjs/load-config'; import { loadRstackConfig, type Configs } from './config.ts'; +import { resolveConfigLayers } from './configLayers.ts'; -const resolveRslibConfig = async ( - configs: Configs, +export const resolveRslibConfig = async ( + layers: readonly Configs[], params: ConfigParams, ): Promise => { - const libConfig = configs.lib; - if (!libConfig) { - return {}; - } - if (typeof libConfig === 'function') { - return libConfig(params); - } - return libConfig; + const configs = await resolveConfigLayers(layers, 'lib', params); + return configs.length > 1 ? mergeRslibConfig(...configs) : (configs[0] ?? {}); }; const loadRslibConfig = (async (params: ConfigParams) => { const { configs, filePath, dependencies } = await loadRstackConfig(); - const config = await resolveRslibConfig(configs, params); + const config = await resolveRslibConfig([configs], params); return withConfigMeta(config, { filePath, dependencies }); }) as RslibConfigDefinition; diff --git a/packages/rstack/tests/config/lib-merge.test.ts b/packages/rstack/tests/config/lib-merge.test.ts new file mode 100644 index 0000000..e9f99c9 --- /dev/null +++ b/packages/rstack/tests/config/lib-merge.test.ts @@ -0,0 +1,27 @@ +import { expect, test } from 'rstack/test'; +import { resolveRslibConfig } from '../../src/rslibConfig.ts'; + +test('merges lib layers using native Rslib rules', async () => { + const config = await resolveRslibConfig( + [ + { + lib: { + source: { define: { SHARED: true, ENV: 'base' } }, + lib: [{ id: 'esm', format: 'esm', dts: true }], + }, + }, + { + lib: { + source: { define: { ENV: 'production' } }, + lib: [{ id: 'esm', dts: false }, { format: 'cjs' }], + }, + }, + ], + { command: 'build', env: 'production' }, + ); + + expect(config).toEqual({ + source: { define: { SHARED: true, ENV: 'production' } }, + lib: [{ id: 'esm', format: 'esm', dts: false }, { format: 'cjs' }], + }); +});