Skip to content

Commit 5439dde

Browse files
authored
refactor(core): use Rslib to build (#3939)
1 parent b13ccc9 commit 5439dde

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+398
-1301
lines changed

e2e/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@scripts/test-helper": "workspace:*",
4343
"@types/fs-extra": "^11.0.4",
4444
"@types/lodash": "^4.17.13",
45-
"@types/node": "22.9.0",
45+
"@types/node": "^22.9.0",
4646
"@types/react": "^18.3.12",
4747
"@types/react-dom": "^18.3.1",
4848
"create-rsbuild": "workspace:*",

examples/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"devDependencies": {
1010
"@rsbuild/core": "workspace:*",
11-
"@types/node": "22.9.0",
11+
"@types/node": "^22.9.0",
1212
"typescript": "^5.6.3"
1313
}
1414
}

nx.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"{projectRoot}/tsconfig.json",
99
"{projectRoot}/package.json",
1010
"{projectRoot}/rslib.config.*",
11-
"{projectRoot}/modern.config.*",
1211
"{projectRoot}/scripts/**/*"
1312
],
1413
"prebundle": [

packages/compat/webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@rsbuild/core": "workspace:*",
3939
"@rslib/core": "0.0.16",
4040
"@scripts/test-helper": "workspace:*",
41-
"@types/node": "22.9.0",
41+
"@types/node": "^22.9.0",
4242
"ansi-escapes": "4.3.2",
4343
"cli-truncate": "2.1.0",
4444
"patch-console": "1.0.0",

packages/compat/webpack/src/index.cjs

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/core/modern.config.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

packages/core/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
"types.d.ts"
4747
],
4848
"scripts": {
49-
"build": "modern build && rslib build && tsc-alias -p tsconfig.json",
50-
"build:rslib": "rslib build",
51-
"dev": "modern build --watch",
49+
"build": "pnpm run build:types && rslib build",
50+
"build:types": "rimraf ./dist-types && tsc --build --force --emitDeclarationOnly",
51+
"dev": "pnpm run build:types && rslib build --watch",
5252
"prebundle": "prebundle"
5353
},
5454
"dependencies": {
@@ -58,11 +58,10 @@
5858
"core-js": "~3.39.0"
5959
},
6060
"devDependencies": {
61-
"@modern-js/module-tools": "^2.61.0",
6261
"@rslib/core": "0.0.16",
6362
"@types/connect": "3.4.38",
6463
"@types/fs-extra": "^11.0.4",
65-
"@types/node": "22.9.0",
64+
"@types/node": "^22.9.0",
6665
"@types/on-finished": "2.3.4",
6766
"@types/webpack-bundle-analyzer": "4.7.0",
6867
"@types/ws": "^8.5.13",
@@ -89,13 +88,13 @@
8988
"postcss-loader": "8.1.1",
9089
"prebundle": "1.2.5",
9190
"reduce-configs": "^1.0.0",
91+
"rimraf": "^6.0.1",
9292
"rsbuild-dev-middleware": "0.1.1",
9393
"rslog": "^1.2.3",
9494
"rspack-chain": "^1.0.3",
9595
"rspack-manifest-plugin": "5.0.2",
9696
"sirv": "^3.0.0",
9797
"style-loader": "3.3.4",
98-
"tsc-alias": "^1.8.10",
9998
"typescript": "^5.6.3",
10099
"webpack": "^5.96.1",
101100
"webpack-bundle-analyzer": "^4.10.2",

packages/core/prebundle.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import fs from 'node:fs';
21
// @ts-check
32
/**
43
* Tip: please add the prebundled packages to `tsconfig.json#paths`.
54
*/
5+
import fs from 'node:fs';
66
import { join } from 'node:path';
77

88
function replaceFileContent(filePath, replaceFn) {

packages/core/rslib.config.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,119 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
13
import { defineConfig } from '@rslib/core';
4+
import type { Configuration } from '@rspack/core';
5+
import pkgJson from './package.json';
6+
import prebundleConfig from './prebundle.config.mjs';
7+
import type { RsbuildPlugin } from './src';
8+
9+
const define = {
10+
RSBUILD_VERSION: JSON.stringify(pkgJson.version),
11+
};
12+
13+
const regexpMap: Record<string, RegExp> = {};
14+
15+
for (const item of prebundleConfig.dependencies) {
16+
const depName = typeof item === 'string' ? item : item.name;
17+
regexpMap[depName] = new RegExp(`compiled[\\/]${depName}(?:[\\/]|$)`);
18+
}
19+
20+
const externals: Configuration['externals'] = [
21+
'webpack',
22+
'@rspack/core',
23+
'@rsbuild/core',
24+
'@rsbuild/core/client/hmr',
25+
'@rsbuild/core/client/overlay',
26+
({ request }, callback) => {
27+
const entries = Object.entries(regexpMap);
28+
if (request) {
29+
for (const [name, test] of entries) {
30+
if (request === name) {
31+
throw new Error(
32+
`"${name}" is not allowed to be imported, use "../compiled/${name}/index.js" instead.`,
33+
);
34+
}
35+
if (test.test(request)) {
36+
return callback(undefined, `../compiled/${name}/index.js`);
37+
}
38+
}
39+
}
40+
callback();
41+
},
42+
];
43+
44+
const pluginFixDtsTypes: RsbuildPlugin = {
45+
name: 'fix-dts-types',
46+
setup(api) {
47+
api.onAfterBuild(() => {
48+
const typesDir = path.join(process.cwd(), 'dist-types');
49+
const pkgPath = path.join(typesDir, 'package.json');
50+
if (!fs.existsSync(typesDir)) {
51+
fs.mkdirSync(typesDir);
52+
}
53+
fs.writeFileSync(
54+
pkgPath,
55+
JSON.stringify({
56+
'//': 'This file is for making TypeScript work with moduleResolution node16+.',
57+
version: '1.0.0',
58+
}),
59+
'utf8',
60+
);
61+
});
62+
},
63+
};
264

365
export default defineConfig({
66+
source: {
67+
define,
68+
},
69+
output: {
70+
target: 'node',
71+
externals,
72+
},
473
lib: [
74+
// Node / ESM
75+
{
76+
format: 'esm',
77+
syntax: 'es2021',
78+
shims: {
79+
esm: {
80+
__filename: true,
81+
__dirname: true,
82+
},
83+
},
84+
plugins: [pluginFixDtsTypes],
85+
},
86+
// Node / CJS
87+
{
88+
format: 'cjs',
89+
syntax: 'es2021',
90+
source: {
91+
entry: {
92+
index: './src/index.ts',
93+
ignoreCssLoader: './src/loader/ignoreCssLoader.ts',
94+
transformLoader: './src/loader/transformLoader.ts',
95+
transformRawLoader: './src/loader/transformRawLoader.ts',
96+
},
97+
},
98+
footer: {
99+
// TODO https://github.com/web-infra-dev/rslib/issues/351
100+
js: `// Annotate the CommonJS export names for ESM import in node:
101+
0 && (module.exports = {
102+
PLUGIN_CSS_NAME,
103+
PLUGIN_SWC_NAME,
104+
__internalHelper,
105+
createRsbuild,
106+
defineConfig,
107+
ensureAssetPrefix,
108+
loadConfig,
109+
loadEnv,
110+
logger,
111+
mergeRsbuildConfig,
112+
rspack,
113+
version
114+
});`,
115+
},
116+
},
5117
// Client / ESM
6118
{
7119
format: 'esm',

packages/core/src/cli/init.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
22
import { loadConfig, watchFilesForRestart } from '../config';
3+
import { createRsbuild } from '../createRsbuild';
34
import { castArray, getAbsolutePath } from '../helpers';
45
import { loadEnv } from '../loadEnv';
56
import { logger } from '../logger';
@@ -69,8 +70,6 @@ export async function init({
6970
watchFilesForRestart(files);
7071
}
7172

72-
const { createRsbuild } = await import('../createRsbuild');
73-
7473
config.source ||= {};
7574
config.source.define = {
7675
...envs.publicVars,

0 commit comments

Comments
 (0)