1- import { defineConfig , moduleTools } from '@modern-js/module-tools' ;
1+ import fs from 'node:fs' ;
2+ import path from 'node:path' ;
23import {
3- BUILD_TARGET ,
4- cjsBuildConfig ,
5- commonExternals ,
6- emitTypePkgJsonPlugin ,
7- esmBuildConfig ,
8- } from '@rsbuild/config/modern.config.ts' ;
4+ type CliPlugin ,
5+ type ModuleTools ,
6+ defineConfig ,
7+ moduleTools ,
8+ } from '@modern-js/module-tools' ;
99import prebundleConfig from './prebundle.config.mjs' ;
1010
11+ const define = {
12+ RSBUILD_VERSION : require ( '../../packages/core/package.json' ) . version ,
13+ } ;
14+
15+ const BUILD_TARGET = {
16+ node : 'es2021' ,
17+ client : 'es2017' ,
18+ } as const ;
19+
20+ const requireShim = {
21+ // use import.meta['url'] to bypass bundle-require replacement of import.meta.url
22+ js : `import { createRequire } from 'module';
23+ var require = createRequire(import.meta['url']);\n` ,
24+ } ;
25+
26+ const emitTypePkgJsonPlugin : CliPlugin < ModuleTools > = {
27+ name : 'emit-type-pkg-json-plugin' ,
28+
29+ setup ( ) {
30+ return {
31+ afterBuild ( ) {
32+ const typesDir = path . join ( process . cwd ( ) , 'dist-types' ) ;
33+ const pkgPath = path . join ( typesDir , 'package.json' ) ;
34+ if ( ! fs . existsSync ( typesDir ) ) {
35+ fs . mkdirSync ( typesDir ) ;
36+ }
37+ fs . writeFileSync (
38+ pkgPath ,
39+ JSON . stringify ( {
40+ '//' : 'This file is for making TypeScript work with moduleResolution node16+.' ,
41+ version : '1.0.0' ,
42+ } ) ,
43+ 'utf8' ,
44+ ) ;
45+ } ,
46+ } ;
47+ } ,
48+ } ;
49+
1150const externals = [
12- ...commonExternals ,
51+ 'webpack' ,
52+ '@rspack/core' ,
53+ '@rsbuild/core' ,
1354 '@rsbuild/core/client/hmr' ,
1455 '@rsbuild/core/client/overlay' ,
56+ / [ \\ / ] c o m p i l e d [ \\ / ] / ,
57+ / n o d e : / ,
1558] ;
1659
1760const aliasCompiledPlugin = {
@@ -33,7 +76,12 @@ export default defineConfig({
3376 buildConfig : [
3477 // Node / ESM
3578 {
36- ...esmBuildConfig ,
79+ format : 'esm' ,
80+ target : BUILD_TARGET . node ,
81+ define,
82+ autoExtension : true ,
83+ shims : true ,
84+ banner : requireShim ,
3785 input : [ 'src/index.ts' ] ,
3886 externals,
3987 dts : false ,
@@ -44,7 +92,11 @@ export default defineConfig({
4492 } ,
4593 // Node / CJS
4694 {
47- ...cjsBuildConfig ,
95+ format : 'cjs' ,
96+ target : BUILD_TARGET . node ,
97+ define,
98+ autoExtension : true ,
99+ dts : false ,
48100 input : [
49101 'src/index.ts' ,
50102 'src/loader/ignoreCssLoader.ts' ,
0 commit comments