Skip to content

Commit 5c0feca

Browse files
committed
feat: add support for Rslib
1 parent f9de917 commit 5c0feca

8 files changed

Lines changed: 86 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ bun add -d rstack
3939
"scripts": {
4040
"dev": "rs dev",
4141
"build": "rs build",
42-
"preview": "rs preview"
42+
"preview": "rs preview",
43+
"lib": "rs lib",
44+
"test": "rs test"
4345
}
4446
}
4547
```
@@ -50,6 +52,8 @@ Then run the scripts with your preferred package manager:
5052
pnpm dev
5153
pnpm build
5254
pnpm preview
55+
pnpm lib
56+
pnpm test
5357
```
5458

5559
## Credits

packages/rstack/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ bun add -d rstack
3939
"scripts": {
4040
"dev": "rs dev",
4141
"build": "rs build",
42-
"preview": "rs preview"
42+
"preview": "rs preview",
43+
"lib": "rs lib",
44+
"test": "rs test"
4345
}
4446
}
4547
```
@@ -50,6 +52,8 @@ Then run the scripts with your preferred package manager:
5052
pnpm dev
5153
pnpm build
5254
pnpm preview
55+
pnpm lib
56+
pnpm test
5357
```
5458

5559
## Credits

packages/rstack/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"rs": "./bin/rs.js",
88
"rstack": "./bin/rs.js"
99
},
10-
"engines": {
11-
"node": ">=22.12.0"
12-
},
1310
"files": [
1411
"bin",
1512
"dist"
@@ -33,12 +30,15 @@
3330
},
3431
"dependencies": {
3532
"@rsbuild/core": "catalog:",
33+
"@rslib/core": "catalog:",
3634
"@rstest/adapter-rsbuild": "catalog:",
3735
"@rstest/core": "catalog:"
3836
},
3937
"devDependencies": {
40-
"@rslib/core": "catalog:",
4138
"@types/node": "catalog:",
4239
"typescript": "catalog:"
40+
},
41+
"engines": {
42+
"node": ">=22.12.0"
4343
}
4444
}

packages/rstack/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig({
77
entry: {
88
index: './src/index.ts',
99
rsbuildConfig: './src/rsbuildConfig.ts',
10+
rslibConfig: './src/rslibConfig.ts',
1011
rstestConfig: './src/rstestConfig.ts',
1112
},
1213
define: {

packages/rstack/src/cli/commands.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Commands:
1313
dev [Rsbuild] Start the dev server
1414
build [Rsbuild] Build the app for production
1515
preview [Rsbuild] Preview the production build locally
16+
lib [Rslib] Build libraries
1617
test [Rstest] Run tests
1718
1819
For details on a sub-command, run:
@@ -50,6 +51,20 @@ async function runRstestCLI(args: string[]): Promise<void> {
5051
runCLI();
5152
}
5253

54+
async function runRslibCLI(args: string[]): Promise<void> {
55+
const argv = [process.execPath, 'rslib', ...args];
56+
57+
if (!hasConfigArg(args)) {
58+
argv.push('-c', join(import.meta.dirname, 'rslibConfig.js'));
59+
}
60+
61+
// TODO
62+
process.argv = argv;
63+
64+
const { runCLI } = await import('@rslib/core');
65+
runCLI();
66+
}
67+
5368
export async function setupCommands(): Promise<void> {
5469
const args = process.argv.slice(2);
5570
const command = args[0];
@@ -64,6 +79,11 @@ export async function setupCommands(): Promise<void> {
6479
return;
6580
}
6681

82+
if (command === 'lib') {
83+
await runRslibCLI(args.slice(1));
84+
return;
85+
}
86+
6787
if (command === 'test') {
6888
await runRstestCLI(args.slice(1));
6989
return;

packages/rstack/src/define.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import type { RsbuildConfigDefinition } from '@rsbuild/core';
2+
import type { ConfigParams as RslibConfigParams, RslibConfig } from '@rslib/core';
23
import type { RstestConfigExport } from '@rstest/core';
34

4-
export type ConfigType = 'app' | 'test';
5+
export type ConfigType = 'app' | 'lib' | 'test';
56

6-
type Config = RsbuildConfigDefinition | RstestConfigExport;
7+
export type RslibConfigDefinition =
8+
| RslibConfig
9+
| ((params: RslibConfigParams) => RslibConfig | Promise<RslibConfig>);
10+
11+
type Config = RsbuildConfigDefinition | RslibConfigDefinition | RstestConfigExport;
712

813
const registry = new Map<ConfigType, Config>();
914

1015
export function getConfig(type: 'app'): RsbuildConfigDefinition | undefined;
16+
export function getConfig(type: 'lib'): RslibConfigDefinition | undefined;
1117
export function getConfig(type: 'test'): RstestConfigExport | undefined;
1218
export function getConfig(type: ConfigType): Config | undefined {
13-
const result = registry.get(type);
14-
return result;
19+
return registry.get(type);
1520
}
1621

1722
export const clearConfig = () => {
@@ -32,11 +37,17 @@ type Define = {
3237
* This config is used by the `rs dev`, `rs build`, and `rs preview` commands.
3338
*/
3439
app: (config: RsbuildConfigDefinition) => void;
40+
/**
41+
* Defines the Rslib config for libraries.
42+
*
43+
* This config is used by the `rs lib` command.
44+
*/
45+
lib: (config: RslibConfigDefinition) => void;
3546
/**
3647
* Defines the Rstest config for tests.
3748
*
38-
* This config is used by the `rs test` command.
39-
*
49+
* This config is used by the `rs test` command.
50+
*
4051
* If `define.app` is also used, Rstest automatically extends the app config unless
4152
* `extends` is set explicitly.
4253
*/
@@ -45,5 +56,6 @@ type Define = {
4556

4657
export const define: Define = {
4758
app: (config) => setConfig('app', config),
59+
lib: (config) => setConfig('lib', config),
4860
test: (config) => setConfig('test', config),
4961
};

packages/rstack/src/rslibConfig.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { loadConfig } from '@rsbuild/core';
2+
import type { ConfigParams, RslibConfig } from '@rslib/core';
3+
import type { RslibConfigDefinition } from './define.js';
4+
import { getConfig, clearConfig } from './define.js';
5+
import { configFileNames } from './constants.js';
6+
7+
const resolveRslibConfig = async (params: ConfigParams): Promise<RslibConfig> => {
8+
const libConfig = getConfig('lib');
9+
if (!libConfig) {
10+
// TODO: should allow empty object to be returned
11+
return { lib: [{}] };
12+
}
13+
if (typeof libConfig === 'function') {
14+
return libConfig(params);
15+
}
16+
return libConfig;
17+
};
18+
19+
const loadRslibConfig = (async (params: ConfigParams) => {
20+
await loadConfig({
21+
loader: 'native',
22+
configFileNames,
23+
});
24+
25+
const libConfig = await resolveRslibConfig(params);
26+
clearConfig();
27+
return libConfig;
28+
}) as RslibConfigDefinition;
29+
30+
export default loadRslibConfig;

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)