Skip to content

Commit 1e4dd8d

Browse files
authored
feat: add syntax placeholder, supersede platform with target (#30)
1 parent ec80e4d commit 1e4dd8d

File tree

4 files changed

+93
-42
lines changed

4 files changed

+93
-42
lines changed

e2e/cases/externals/node/rslib.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';
44

55
export default defineConfig({
66
lib: [
7-
generateBundleEsmConfig(__dirname, { platform: 'node' }),
8-
generateBundleCjsConfig(__dirname, { platform: 'node' }),
7+
generateBundleEsmConfig(__dirname, { output: { target: 'node' } }),
8+
generateBundleCjsConfig(__dirname, { output: { target: 'node' } }),
99
],
1010
source: {
1111
entry: {

packages/core/src/config.ts

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
1010
import type {
1111
Format,
1212
LibConfig,
13-
Platform,
1413
RslibConfig,
1514
RslibConfigAsyncFn,
1615
RslibConfigExport,
1716
RslibConfigSyncFn,
17+
Syntax,
1818
} from './types/config';
1919
import { getDefaultExtension } from './utils/extension';
2020
import { 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-
179158
const 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+
199182
export 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+
221228
export 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

packages/core/src/types/config/index.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
import type { RsbuildConfig } from '@rsbuild/core';
22

33
export type Format = 'esm' | 'cjs' | 'umd';
4-
export type Platform = 'node' | 'browser' | 'neutral';
4+
5+
export type EcmaScriptVersion =
6+
| 'esnext'
7+
| 'es5'
8+
| 'es6'
9+
| 'es2015'
10+
| 'es2016'
11+
| 'es2017'
12+
| 'es2018'
13+
| 'es2019'
14+
| 'es2020'
15+
| 'es2021'
16+
| 'es2022'
17+
| 'es2023'
18+
| 'es2024';
19+
20+
export type Syntax =
21+
// Use browserslist config file
22+
| 'browserslist'
23+
// ECMAScript versions as an common used addition to browserslist query
24+
| EcmaScriptVersion
25+
| EcmaScriptVersion[]
26+
// Support inline browserslist query, like defined in package.json
27+
| string[];
528

629
export interface LibConfig extends RsbuildConfig {
730
format?: Format;
8-
platform?: Platform;
931
autoExtension?: boolean;
32+
output?: RsbuildConfig['output'] & {
33+
/** Support esX and browserslist query */
34+
syntax?: Syntax;
35+
};
1036
}
1137

1238
export interface RslibConfig extends RsbuildConfig {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { EcmaScriptVersion } from '../types/config';
2+
3+
export const esVersionToBrowserList = (
4+
_esVersion: EcmaScriptVersion,
5+
): string[] => {
6+
// TODO: transform esX to browserslist
7+
return [];
8+
};

0 commit comments

Comments
 (0)