@@ -10,11 +10,11 @@ import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
1010import type {
1111 Format ,
1212 LibConfig ,
13- Platform ,
1413 RslibConfig ,
1514 RslibConfigAsyncFn ,
1615 RslibConfigExport ,
1716 RslibConfigSyncFn ,
17+ Syntax ,
1818} from './types/config' ;
1919import { getDefaultExtension } from './utils/extension' ;
2020import { color } from './utils/helper' ;
@@ -155,27 +155,6 @@ const getDefaultFormatConfig = (format: Format): RsbuildConfig => {
155155 }
156156} ;
157157
158- const getDefaultPlatformConfig = ( platform : Platform ) : RsbuildConfig => {
159- switch ( platform ) {
160- case 'browser' :
161- return { } ;
162- case 'node' :
163- return {
164- output : {
165- // When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
166- // Simply override the built-in modules to make them external.
167- // https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
168- externals : nodeBuiltInModules ,
169- target : 'node' ,
170- } ,
171- } ;
172- case 'neutral' :
173- return { } ;
174- default :
175- throw new Error ( `Unsupported platform: ${ platform } ` ) ;
176- }
177- } ;
178-
179158const getDefaultAutoExtensionConfig = (
180159 format : Format ,
181160 root : string ,
@@ -196,33 +175,61 @@ const getDefaultAutoExtensionConfig = (
196175 } ;
197176} ;
198177
178+ const getDefaultSyntax = ( _syntax ?: Syntax ) : RsbuildConfig => {
179+ return { } ;
180+ } ;
181+
199182export function convertLibConfigToRsbuildConfig (
200183 libConfig : LibConfig ,
201- rsbuildConfig : RsbuildConfig ,
184+ configPath : string ,
202185) : RsbuildConfig {
203- const { format, platform = 'browser' , autoExtension = false } = libConfig ;
186+ const { format, autoExtension = false } = libConfig ;
204187
205188 const formatConfig = getDefaultFormatConfig ( format ! ) ;
206- const platformConfig = getDefaultPlatformConfig ( platform ) ;
207189 const autoExtensionConfig = getDefaultAutoExtensionConfig (
208190 format ! ,
209- dirname ( rsbuildConfig . _privateMeta ?. configFilePath ?? process . cwd ( ) ) ,
191+ dirname ( configPath ) ,
210192 autoExtension ,
211193 ) ;
194+ const syntaxConfig = getDefaultSyntax ( libConfig . output ?. syntax ) ;
195+
196+ return mergeRsbuildConfig ( formatConfig , autoExtensionConfig , syntaxConfig ) ;
197+ }
212198
213- return mergeRsbuildConfig (
214- rsbuildConfig ,
215- formatConfig ,
216- platformConfig ,
217- autoExtensionConfig ,
199+ function postUpdateRsbuildConfig ( rsbuildConfig : RsbuildConfig ) {
200+ const defaultTargetConfig = getDefaultTargetConfig (
201+ rsbuildConfig . output ?. target ?? 'web' ,
218202 ) ;
203+
204+ return mergeRsbuildConfig ( defaultTargetConfig ) ;
219205}
220206
207+ const getDefaultTargetConfig = ( target : string ) : RsbuildConfig => {
208+ switch ( target ) {
209+ case 'web' :
210+ return { } ;
211+ case 'node' :
212+ return {
213+ output : {
214+ // When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
215+ // Simply override the built-in modules to make them external.
216+ // https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
217+ externals : nodeBuiltInModules ,
218+ target : 'node' ,
219+ } ,
220+ } ;
221+ case 'neutral' :
222+ return { } ;
223+ default :
224+ throw new Error ( `Unsupported platform: ${ target } ` ) ;
225+ }
226+ } ;
227+
221228export async function composeCreateRsbuildConfig (
222229 rslibConfig : RslibConfig ,
223230) : Promise < Partial < Record < Format , RsbuildConfig > > > {
224231 const internalRsbuildConfig = await createInternalRsbuildConfig ( ) ;
225-
232+ const configPath = rslibConfig . _privateMeta ?. configFilePath ?? process . cwd ( ) ;
226233 const { lib : libConfigsArray , ...sharedRsbuildConfig } = rslibConfig ;
227234
228235 if ( ! libConfigsArray ) {
@@ -234,19 +241,29 @@ export async function composeCreateRsbuildConfig(
234241 const composedRsbuildConfig : Partial < Record < Format , RsbuildConfig > > = { } ;
235242
236243 for ( const libConfig of libConfigsArray ) {
237- const { format, platform, ...overrideRsbuildConfig } = libConfig ;
244+ const { format, ...overrideRsbuildConfig } = libConfig ;
245+
246+ const libConvertedRsbuildConfig = convertLibConfigToRsbuildConfig (
247+ libConfig ,
248+ configPath ,
249+ ) ;
238250
239- // Merge order matters, keep `internalRsbuildConfig` at the last position
240- // to ensure that the internal config is not overridden by the user's config.
241251 const mergedRsbuildConfig = mergeRsbuildConfig (
242252 sharedRsbuildConfig ,
243253 overrideRsbuildConfig ,
244- internalRsbuildConfig ,
254+ libConvertedRsbuildConfig ,
245255 ) ;
246256
247- composedRsbuildConfig [ format ! ] = convertLibConfigToRsbuildConfig (
248- libConfig ,
257+ // Some configurations can be defined both in the shared config and the lib config.
258+ // So we need to do the post process after lib config is converted and merged.
259+ const postUpdatedConfig = postUpdateRsbuildConfig ( mergedRsbuildConfig ) ;
260+
261+ composedRsbuildConfig [ format ! ] = mergeRsbuildConfig (
249262 mergedRsbuildConfig ,
263+ postUpdatedConfig ,
264+ // Merge order matters, keep `internalRsbuildConfig` at the last position
265+ // to ensure that the internal config is not overridden by user's config.
266+ internalRsbuildConfig ,
250267 ) ;
251268 }
252269
0 commit comments