Skip to content

Commit df12da2

Browse files
authored
feat: use environment API to build (#23)
1 parent a645e60 commit df12da2

File tree

7 files changed

+130
-86
lines changed

7 files changed

+130
-86
lines changed

e2e/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"devDependencies": {
1313
"@playwright/test": "1.43.1",
14+
"@rsbuild/core": "1.0.0-alpha.0",
1415
"@rslib/core": "workspace:*",
1516
"@rslib/tsconfig": "workspace:*",
1617
"@types/fs-extra": "^11.0.4",

e2e/scripts/shared.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
import { join } from 'node:path';
2+
import { mergeRsbuildConfig as mergeConfig } from '@rsbuild/core';
23
import type { LibConfig, RslibConfig } from '@rslib/core';
34
import { globContentJSON } from '#helper';
45

5-
export function generateBundleEsmConfig(cwd: string): LibConfig {
6-
return {
6+
export function generateBundleEsmConfig(
7+
cwd: string,
8+
config: LibConfig = {},
9+
): LibConfig {
10+
const esmBasicConfig: LibConfig = {
711
format: 'esm',
812
output: {
913
distPath: {
1014
root: join(cwd, './dist/esm'),
1115
},
1216
},
1317
};
18+
19+
return mergeConfig(esmBasicConfig, config);
1420
}
1521

16-
export function generateBundleCjsConfig(cwd: string): LibConfig {
17-
return {
22+
export function generateBundleCjsConfig(
23+
cwd: string,
24+
config: LibConfig = {},
25+
): LibConfig {
26+
const cjsBasicConfig: LibConfig = {
1827
format: 'cjs',
1928
output: {
2029
distPath: {
2130
root: join(cwd, './dist/cjs'),
2231
},
2332
},
2433
};
34+
35+
return mergeConfig(cjsBasicConfig, config);
2536
}
2637

2738
export async function getEntryJsResults(rslibConfig: RslibConfig) {

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"prebundle": "prebundle"
3939
},
4040
"dependencies": {
41-
"@rsbuild/core": "0.7.10"
41+
"@rsbuild/core": "1.0.0-alpha.0"
4242
},
4343
"devDependencies": {
4444
"@rslib/tsconfig": "workspace:*",

packages/core/src/build.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
import type { RsbuildInstance } from '@rsbuild/core';
21
import type { BuildOptions } from './cli/commands';
32
import { initRsbuild } from './config';
43
import type { RslibConfig } from './types/config';
54

65
export async function build(config: RslibConfig, options?: BuildOptions) {
7-
const rsbuildInstances = await initRsbuild(config);
6+
const rsbuildInstance = await initRsbuild(config);
87

9-
const buildPromises = rsbuildInstances.map(
10-
async (rsbuildInstance: RsbuildInstance) => {
11-
return await rsbuildInstance.build({
12-
mode: 'production',
13-
watch: options?.watch,
14-
});
15-
},
16-
);
8+
await rsbuildInstance.build({
9+
mode: 'production',
10+
watch: options?.watch,
11+
});
1712

18-
await Promise.all(buildPromises);
19-
20-
return rsbuildInstances;
13+
return rsbuildInstance;
2114
}

packages/core/src/config.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@rsbuild/core';
99
import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
1010
import type {
11+
Format,
1112
LibConfig,
1213
RslibConfig,
1314
RslibConfigAsyncFn,
@@ -73,6 +74,9 @@ export async function loadConfig(
7374

7475
export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
7576
return defineRsbuildConfig({
77+
dev: {
78+
progressBar: false,
79+
},
7680
tools: {
7781
htmlPlugin: false,
7882
},
@@ -143,17 +147,20 @@ export function convertLibConfigToRsbuildConfig(
143147

144148
export async function composeCreateRsbuildConfig(
145149
rslibConfig: RslibConfig,
146-
): Promise<RsbuildConfig[]> {
150+
): Promise<Partial<Record<Format, RsbuildConfig>>> {
147151
const internalRsbuildConfig = await createInternalRsbuildConfig();
148152

149153
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
150154

151155
if (!libConfigsArray) {
152-
logger.error('You must specify lib field in config file.');
153-
return [];
156+
throw new Error(
157+
`Expect lib field to be an array, but got ${libConfigsArray}.`,
158+
);
154159
}
155160

156-
const composedRsbuildConfig = libConfigsArray.map((libConfig: LibConfig) => {
161+
const composedRsbuildConfig: Partial<Record<Format, RsbuildConfig>> = {};
162+
163+
for (const libConfig of libConfigsArray) {
157164
const { format, ...overrideRsbuildConfig } = libConfig;
158165

159166
// Merge order matters, keep `internalRsbuildConfig` at the last position
@@ -164,23 +171,21 @@ export async function composeCreateRsbuildConfig(
164171
internalRsbuildConfig,
165172
);
166173

167-
return convertLibConfigToRsbuildConfig(libConfig, mergedRsbuildConfig);
168-
});
174+
composedRsbuildConfig[format!] = convertLibConfigToRsbuildConfig(
175+
libConfig,
176+
mergedRsbuildConfig,
177+
);
178+
}
169179

170180
return composedRsbuildConfig;
171181
}
172182

173183
export async function initRsbuild(rslibConfig: RslibConfig) {
174-
// TODO: use environment API instead
175-
const rsbuildConfigArray = await composeCreateRsbuildConfig(rslibConfig);
184+
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
176185

177-
const rsbuildPromises = rsbuildConfigArray.map(
178-
async (rsbuildConfig: RsbuildConfig) => {
179-
return createRsbuild({ rsbuildConfig });
186+
return createRsbuild({
187+
rsbuildConfig: {
188+
environments: rsbuildConfigObject,
180189
},
181-
);
182-
183-
const rsbuildInstances = await Promise.all(rsbuildPromises);
184-
185-
return rsbuildInstances;
190+
});
186191
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = `
4-
[
5-
{
4+
{
5+
"cjs": {
6+
"dev": {
7+
"progressBar": false,
8+
},
69
"output": {
710
"distPath": {
811
"js": "./",
@@ -12,31 +15,30 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
1215
},
1316
"source": {
1417
"alias": {
15-
"bar": "bar",
16-
"foo": "foo/esm",
18+
"bar": "bar/cjs",
19+
"foo": "foo",
1720
},
18-
"preEntry": "./b.js",
21+
"preEntry": [
22+
"./a.js",
23+
"./c.js",
24+
"./d.js",
25+
],
1926
},
2027
"tools": {
2128
"htmlPlugin": false,
2229
"rspack": {
23-
"experiments": {
24-
"outputModule": true,
25-
},
26-
"optimization": {
27-
"concatenateModules": true,
28-
},
2930
"output": {
30-
"iife": false,
3131
"library": {
32-
"type": "modern-module",
32+
"type": "commonjs",
3333
},
34-
"module": true,
3534
},
3635
},
3736
},
3837
},
39-
{
38+
"esm": {
39+
"dev": {
40+
"progressBar": false,
41+
},
4042
"output": {
4143
"distPath": {
4244
"js": "./",
@@ -46,27 +48,34 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
4648
},
4749
"source": {
4850
"alias": {
49-
"bar": "bar/cjs",
50-
"foo": "foo",
51+
"bar": "bar",
52+
"foo": "foo/esm",
5153
},
52-
"preEntry": [
53-
"./a.js",
54-
"./c.js",
55-
"./d.js",
56-
],
54+
"preEntry": "./b.js",
5755
},
5856
"tools": {
5957
"htmlPlugin": false,
6058
"rspack": {
59+
"experiments": {
60+
"outputModule": true,
61+
},
62+
"optimization": {
63+
"concatenateModules": true,
64+
},
6165
"output": {
66+
"iife": false,
6267
"library": {
63-
"type": "commonjs",
68+
"type": "modern-module",
6469
},
70+
"module": true,
6571
},
6672
},
6773
},
6874
},
69-
{
75+
"umd": {
76+
"dev": {
77+
"progressBar": false,
78+
},
7079
"output": {
7180
"distPath": {
7281
"js": "./",
@@ -92,5 +101,5 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
92101
},
93102
},
94103
},
95-
]
104+
}
96105
`;

0 commit comments

Comments
 (0)