Skip to content

Commit ee00275

Browse files
authored
feat: basic load config and build architecture (#15)
1 parent 553bef9 commit ee00275

File tree

16 files changed

+556
-505
lines changed

16 files changed

+556
-505
lines changed

.github/workflows/test-ubuntu.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,8 @@ jobs:
100100
run: |
101101
pnpm run test:artifact
102102
pnpm run test:e2e
103+
104+
- name: Examples Test
105+
if: steps.changes.outputs.changed == 'true'
106+
run: |
107+
pnpm run build:examples

e2e/cases/define/index.test.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1-
import { build } from '@rslib/core';
1+
import { join } from 'node:path';
2+
import { type RslibConfig, build } from '@rslib/core';
23
import { expect, test } from 'vitest';
34
import { globContentJSON } from '#helper';
45

56
test.fails('define', async () => {
67
delete process.env.NODE_ENV;
78

8-
const rslibConfig = {
9-
root: __dirname,
10-
entry: './js/src/index.js',
11-
outDir: 'dist',
9+
const rslibConfig: RslibConfig = {
10+
lib: [
11+
{
12+
format: 'esm',
13+
output: {
14+
distPath: {
15+
root: join(__dirname, './dist/esm'),
16+
},
17+
},
18+
},
19+
{
20+
format: 'cjs',
21+
output: {
22+
distPath: {
23+
root: join(__dirname, './dist/cjs'),
24+
},
25+
},
26+
},
27+
],
28+
source: {
29+
entry: {
30+
main: join(__dirname, './js/src/index.js'),
31+
},
32+
},
1233
};
1334

1435
const instance = await build(rslibConfig);
15-
const results = await globContentJSON(instance.context.distPath, {
36+
37+
const results = await globContentJSON(instance[0]!.context.distPath, {
1638
absolute: true,
1739
ignore: ['/**/*.map'],
1840
});

examples/basic/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@examples/basic",
3+
"private": true,
4+
"scripts": {
5+
"build": "rslib build"
6+
},
7+
"devDependencies": {
8+
"@rslib/core": "workspace:*",
9+
"typescript": "^5.4.5"
10+
}
11+
}

examples/basic/rslib.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineConfig } from '@rslib/core';
2+
3+
export default defineConfig({
4+
lib: [
5+
{
6+
format: 'esm',
7+
output: {
8+
distPath: {
9+
root: './dist/esm',
10+
},
11+
},
12+
},
13+
{
14+
format: 'cjs',
15+
output: {
16+
distPath: {
17+
root: './dist/cjs',
18+
},
19+
},
20+
},
21+
],
22+
source: {
23+
entry: {
24+
main: './src/index.ts',
25+
},
26+
},
27+
});

examples/basic/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const message: string = 'Hello, Rslib';
2+
3+
console.log(message);

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "rslib-monorepo",
33
"private": true,
44
"scripts": {
5-
"build": "cross-env NX_DAEMON=false nx run-many -t build --parallel=10",
5+
"build": "cross-env NX_DAEMON=false nx run-many -t build --exclude @examples/* --parallel=10",
6+
"build:examples": "cross-env NX_DAEMON=false nx run-many -t build --projects @examples/* --parallel=10",
67
"check-dependency-version": "check-dependency-version-consistency .",
78
"lint": "biome check . --diagnostic-level=warn",
89
"prebundle": "nx run-many -t prebundle",
@@ -24,16 +25,16 @@
2425
"package.json": "pnpm run check-dependency-version"
2526
},
2627
"devDependencies": {
27-
"@biomejs/biome": "^1.7.0",
28-
"@modern-js/module-tools": "^2.48.6",
28+
"@biomejs/biome": "^1.8.1",
29+
"@modern-js/module-tools": "^2.53.0",
2930
"check-dependency-version-consistency": "^4.1.0",
3031
"cross-env": "^7.0.3",
3132
"nano-staged": "^0.8.0",
3233
"nx": "^19.3.0",
33-
"prettier": "^3.2.4",
34+
"prettier": "^3.3.2",
3435
"prettier-plugin-packagejson": "^2.5.0",
35-
"simple-git-hooks": "^2.10.0",
36-
"vitest": "^1.5.0"
36+
"simple-git-hooks": "^2.11.1",
37+
"vitest": "^1.6.0"
3738
},
3839
"packageManager": "[email protected]",
3940
"engines": {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"prebundle": "prebundle"
3939
},
4040
"dependencies": {
41-
"@rsbuild/core": "0.6.2"
41+
"@rsbuild/core": "0.7.9"
4242
},
4343
"devDependencies": {
4444
"@rslib/tsconfig": "workspace:*",

packages/core/src/build.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { createRsbuild } from '@rsbuild/core';
2-
import { composeCreateRsbuildConfig } from './config';
3-
import type { RslibConfig } from './types';
4-
5-
export async function build(config: RslibConfig) {
6-
const createRsbuildConfig = composeCreateRsbuildConfig(config);
7-
const rsbuildInstance = await createRsbuild(createRsbuildConfig);
8-
await rsbuildInstance.build({
9-
mode: 'production',
10-
});
11-
return rsbuildInstance;
1+
import type { RsbuildInstance } from '@rsbuild/core';
2+
import type { BuildOptions } from './cli/commands';
3+
import { initRsbuild } from './config';
4+
import type { RslibConfig } from './types/config';
5+
6+
export async function build(config: RslibConfig, options?: BuildOptions) {
7+
const rsbuildInstances = await initRsbuild(config);
8+
9+
const buildPromises = rsbuildInstances.map(
10+
async (rsbuildInstance: RsbuildInstance) => {
11+
return await rsbuildInstance.build({
12+
mode: 'production',
13+
watch: options?.watch,
14+
});
15+
},
16+
);
17+
18+
await Promise.all(buildPromises);
19+
20+
return rsbuildInstances;
1221
}

packages/core/src/cli/commands.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1-
import { program } from 'commander';
1+
import { type Command, program } from 'commander';
22
import { build } from '../build';
3-
import type { RslibConfig } from '../types';
3+
import { loadConfig } from '../config';
4+
import { logger } from '../utils/logger';
5+
6+
export type CommonOptions = {
7+
config?: string;
8+
envMode?: string;
9+
};
10+
11+
export type BuildOptions = CommonOptions & {
12+
watch?: boolean;
13+
};
14+
15+
const applyCommonOptions = (command: Command) => {
16+
command
17+
.option(
18+
'-c --config <config>',
19+
'specify the configuration file, can be a relative or absolute path',
20+
)
21+
.option(
22+
'--env-mode <mode>',
23+
'specify the env mode to load the `.env.[mode]` file',
24+
);
25+
};
426

527
export function runCli() {
628
program.name('rslib').usage('<command> [options]').version(RSLIB_VERSION);
729

830
const buildCommand = program.command('build');
931

32+
[buildCommand].forEach(applyCommonOptions);
33+
1034
buildCommand
11-
.description('build the app for production')
12-
.option('--entry <entry>', 'entrypoint file to build')
13-
.option('--outDir <outDir>', 'dist directory to build to')
14-
.action(async (options: RslibConfig) => {
35+
.option('-w --watch', 'turn on watch mode, watch for changes and rebuild')
36+
.description('build the library for production')
37+
.action(async (options: BuildOptions) => {
1538
try {
16-
await build({
17-
entry: options.entry,
18-
outDir: options.outDir,
19-
});
39+
const rslibConfig = await loadConfig(options.config, options.envMode);
40+
await build(rslibConfig, options);
2041
} catch (err) {
42+
logger.error('Failed to build.');
43+
logger.error(err);
2144
process.exit(1);
2245
}
2346
});

0 commit comments

Comments
 (0)