Skip to content

Commit 553bef9

Browse files
authored
feat: cli bin init and NX integrated (#14)
1 parent 16c28b3 commit 553bef9

File tree

16 files changed

+925
-40
lines changed

16 files changed

+925
-40
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ node_modules/
66

77
dist/
88
dist-types/
9+
compiled/
910
coverage/
1011
doc_build/
1112
playwright-report/
1213

1314
.vscode/**/*
1415
!.vscode/settings.json
1516
!.vscode/extensions.json
16-
.idea/
17+
.idea/
18+
.nx/

nx.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "./node_modules/nx/schemas/nx-schema.json",
3+
"namedInputs": {
4+
"default": ["{projectRoot}/src/**/*"],
5+
"build": [
6+
"default",
7+
"!{projectRoot}/**/*.{md,mdx}",
8+
"{projectRoot}/tsconfig.json",
9+
"{projectRoot}/package.json",
10+
"{projectRoot}/modern.config.*",
11+
"{projectRoot}/scripts/**/*"
12+
],
13+
"prebundle": [
14+
"{projectRoot}/package.json",
15+
"{projectRoot}/prebundle.config.mjs"
16+
]
17+
},
18+
"targetDefaults": {
19+
"build": {
20+
"cache": true,
21+
"dependsOn": ["^build", "prebundle"],
22+
"inputs": ["build", "^build"],
23+
"outputs": ["{projectRoot}/dist", "{projectRoot}/dist-types"]
24+
},
25+
"prebundle": {
26+
"cache": true,
27+
"inputs": ["prebundle"],
28+
"outputs": ["{projectRoot}/compiled"]
29+
}
30+
},
31+
"defaultBase": "main"
32+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "rslib-monorepo",
33
"private": true,
44
"scripts": {
5-
"build": "pnpm -r --filter='./packages/*' run build",
5+
"build": "cross-env NX_DAEMON=false nx run-many -t build --parallel=10",
66
"check-dependency-version": "check-dependency-version-consistency .",
77
"lint": "biome check . --diagnostic-level=warn",
8+
"prebundle": "nx run-many -t prebundle",
89
"prepare": "pnpm run build && simple-git-hooks",
910
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
1011
"test:artifact": "vitest run --project artifact",
@@ -28,6 +29,7 @@
2829
"check-dependency-version-consistency": "^4.1.0",
2930
"cross-env": "^7.0.3",
3031
"nano-staged": "^0.8.0",
32+
"nx": "^19.3.0",
3133
"prettier": "^3.2.4",
3234
"prettier-plugin-packagejson": "^2.5.0",
3335
"simple-git-hooks": "^2.10.0",

packages/core/bin/rslib.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env node
2-
import { runCli } from '../dist/index.js';
2+
import { logger, prepareCli, runCli } from '../dist/index.js';
33

44
async function main() {
5-
runCli();
5+
prepareCli();
6+
7+
try {
8+
runCli();
9+
} catch (err) {
10+
logger.error(err);
11+
}
612
}
713

814
main();

packages/core/modern.config.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
import { defineConfig, moduleTools } from '@modern-js/module-tools';
2+
import prebundleConfig from './prebundle.config.mjs';
3+
4+
const externals = ['@rsbuild/core', /[\\/]compiled[\\/]/, /node:/];
5+
const define = {
6+
RSLIB_VERSION: require('./package.json').version,
7+
};
8+
9+
const aliasCompiledPlugin = {
10+
name: 'alias-compiled-plugin',
11+
setup(build) {
12+
const { dependencies } = prebundleConfig;
13+
for (const item of dependencies) {
14+
const depName = typeof item === 'string' ? item : item.name;
15+
build.onResolve({ filter: new RegExp(`^${depName}$`) }, () => ({
16+
path: `../compiled/${depName}/index.js`,
17+
external: true,
18+
}));
19+
}
20+
},
21+
};
222

323
export default defineConfig({
424
plugins: [moduleTools()],
@@ -8,14 +28,26 @@ export default defineConfig({
828
target: 'es2020',
929
buildType: 'bundle',
1030
autoExtension: true,
31+
externals,
1132
dts: false,
33+
define,
34+
esbuildOptions(options) {
35+
options.plugins?.unshift(aliasCompiledPlugin);
36+
return options;
37+
},
1238
},
1339
{
1440
format: 'esm',
1541
target: 'es2020',
1642
buildType: 'bundle',
1743
autoExtension: true,
44+
externals,
1845
dts: false,
46+
define,
47+
esbuildOptions(options) {
48+
options.plugins?.unshift(aliasCompiledPlugin);
49+
return options;
50+
},
1951
},
2052
{
2153
buildType: 'bundleless',

packages/core/package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@
2929
"files": [
3030
"bin",
3131
"dist",
32-
"dist-types"
32+
"dist-types",
33+
"compiled"
3334
],
3435
"scripts": {
3536
"build": "modern build",
36-
"dev": "modern build --watch"
37+
"dev": "modern build --watch",
38+
"prebundle": "prebundle"
3739
},
3840
"dependencies": {
39-
"@rsbuild/core": "0.6.2",
40-
"@rsbuild/shared": "0.6.2"
41+
"@rsbuild/core": "0.6.2"
4142
},
4243
"devDependencies": {
4344
"@rslib/tsconfig": "workspace:*",
44-
"commander": "^12.0.0",
45+
"@types/fs-extra": "^11.0.4",
46+
"commander": "^12.1.0",
47+
"fs-extra": "^11.2.0",
48+
"picocolors": "1.0.1",
49+
"prebundle": "1.1.0",
50+
"rslog": "^1.2.2",
4551
"typescript": "^5.4.5"
4652
},
4753
"engines": {

packages/core/prebundle.config.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-check
2+
import { join } from 'node:path';
3+
import fs from 'fs-extra';
4+
5+
/**
6+
* Tip: please add the prebundled packages to `tsconfig.json#paths`.
7+
*/
8+
9+
/** @type {import('prebundle').Config} */
10+
export default {
11+
externals: {
12+
typescript: 'typescript',
13+
},
14+
dependencies: [
15+
'commander',
16+
{
17+
name: 'picocolors',
18+
beforeBundle({ depPath }) {
19+
const typesFile = join(depPath, 'types.ts');
20+
// Fix type bundle
21+
if (fs.existsSync(typesFile)) {
22+
fs.renameSync(typesFile, join(depPath, 'types.d.ts'));
23+
}
24+
},
25+
},
26+
],
27+
};

packages/core/src/cli.ts renamed to packages/core/src/cli/commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { program } from 'commander';
2-
import { build } from './build';
3-
import type { RslibConfig } from './types';
2+
import { build } from '../build';
3+
import type { RslibConfig } from '../types';
44

55
export function runCli() {
6+
program.name('rslib').usage('<command> [options]').version(RSLIB_VERSION);
7+
68
const buildCommand = program.command('build');
79

810
buildCommand

packages/core/src/cli/prepare.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { logger } from '../utils/logger';
2+
3+
function initNodeEnv() {
4+
if (!process.env.NODE_ENV) {
5+
const command = process.argv[2] ?? '';
6+
process.env.NODE_ENV = ['build'].includes(command)
7+
? 'production'
8+
: 'development';
9+
}
10+
}
11+
12+
export function prepareCli() {
13+
initNodeEnv();
14+
15+
// Print a blank line to keep the greet log nice.
16+
// Some package managers automatically output a blank line, some do not.
17+
const { npm_execpath } = process.env;
18+
if (
19+
!npm_execpath ||
20+
npm_execpath.includes('npx-cli.js') ||
21+
npm_execpath.includes('.bun')
22+
) {
23+
console.log();
24+
}
25+
26+
logger.greet(` ${`Rslib v${RSLIB_VERSION}`}\n`);
27+
}

packages/core/src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const RSLIB_VERSION;

0 commit comments

Comments
 (0)