@modern-js/builder 3.0.1 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/cjs/createBuilder.js +3 -8
  2. package/dist/cjs/plugins/environmentDefaults.js +4 -4
  3. package/dist/cjs/plugins/rscConfig.js +159 -0
  4. package/dist/cjs/plugins/runtimeChunk.js +2 -1
  5. package/dist/cjs/{rsc/rsc-css-loader.js → shared/rsc/rsc-server-entry-loader.js} +6 -7
  6. package/dist/cjs/shared/rsc/rscClientBrowserFallback.js +1 -1
  7. package/dist/esm/createBuilder.mjs +3 -8
  8. package/dist/esm/plugins/environmentDefaults.mjs +4 -4
  9. package/dist/esm/plugins/rscConfig.mjs +112 -0
  10. package/dist/esm/plugins/runtimeChunk.mjs +2 -1
  11. package/dist/esm/shared/rsc/rsc-server-entry-loader.mjs +7 -0
  12. package/dist/esm/shared/rsc/rscClientBrowserFallback.mjs +1 -1
  13. package/dist/esm-node/createBuilder.mjs +3 -8
  14. package/dist/esm-node/plugins/environmentDefaults.mjs +4 -4
  15. package/dist/esm-node/plugins/rscConfig.mjs +117 -0
  16. package/dist/esm-node/plugins/runtimeChunk.mjs +2 -1
  17. package/dist/esm-node/shared/rsc/rsc-server-entry-loader.mjs +8 -0
  18. package/dist/esm-node/shared/rsc/rscClientBrowserFallback.mjs +1 -1
  19. package/dist/types/plugins/rscConfig.d.ts +18 -0
  20. package/dist/types/shared/rsc/rsc-server-entry-loader.d.ts +5 -0
  21. package/package.json +9 -9
  22. package/dist/cjs/rsc/common.js +0 -157
  23. package/dist/cjs/rsc/plugins/rsbuild-rsc-plugin.js +0 -169
  24. package/dist/cjs/rsc/plugins/rspack-rsc-client-plugin.js +0 -187
  25. package/dist/cjs/rsc/plugins/rspack-rsc-server-plugin.js +0 -245
  26. package/dist/cjs/rsc/rsc-client-loader.js +0 -71
  27. package/dist/cjs/rsc/rsc-server-loader.js +0 -102
  28. package/dist/cjs/rsc/rsc-ssr-loader.js +0 -60
  29. package/dist/esm/rsc/common.mjs +0 -87
  30. package/dist/esm/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -124
  31. package/dist/esm/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -143
  32. package/dist/esm/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -211
  33. package/dist/esm/rsc/rsc-client-loader.mjs +0 -37
  34. package/dist/esm/rsc/rsc-css-loader.mjs +0 -8
  35. package/dist/esm/rsc/rsc-server-loader.mjs +0 -58
  36. package/dist/esm/rsc/rsc-ssr-loader.mjs +0 -26
  37. package/dist/esm-node/rsc/common.mjs +0 -88
  38. package/dist/esm-node/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -126
  39. package/dist/esm-node/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -144
  40. package/dist/esm-node/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -212
  41. package/dist/esm-node/rsc/rsc-client-loader.mjs +0 -38
  42. package/dist/esm-node/rsc/rsc-css-loader.mjs +0 -9
  43. package/dist/esm-node/rsc/rsc-server-loader.mjs +0 -60
  44. package/dist/esm-node/rsc/rsc-ssr-loader.mjs +0 -27
  45. package/dist/types/rsc/common.d.ts +0 -22
  46. package/dist/types/rsc/plugins/rsbuild-rsc-plugin.d.ts +0 -7
  47. package/dist/types/rsc/plugins/rspack-rsc-client-plugin.d.ts +0 -14
  48. package/dist/types/rsc/plugins/rspack-rsc-server-plugin.d.ts +0 -32
  49. package/dist/types/rsc/rsc-client-loader.d.ts +0 -7
  50. package/dist/types/rsc/rsc-css-loader.d.ts +0 -2
  51. package/dist/types/rsc/rsc-server-loader.d.ts +0 -6
  52. package/dist/types/rsc/rsc-ssr-loader.d.ts +0 -6
@@ -28,7 +28,7 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  createRspackBuilder: ()=>createRspackBuilder
29
29
  });
30
30
  const core_namespaceObject = require("@rsbuild/core");
31
- const rsbuild_rsc_plugin_js_namespaceObject = require("./rsc/plugins/rsbuild-rsc-plugin.js");
31
+ const rscConfig_js_namespaceObject = require("./plugins/rscConfig.js");
32
32
  const parseCommonConfig_js_namespaceObject = require("./shared/parseCommonConfig.js");
33
33
  const rscClientBrowserFallback_js_namespaceObject = require("./shared/rsc/rscClientBrowserFallback.js");
34
34
  async function parseConfig(builderConfig, options) {
@@ -54,13 +54,8 @@ async function parseConfig(builderConfig, options) {
54
54
  }
55
55
  const enableRsc = builderConfig.server?.rsc ?? false;
56
56
  if (enableRsc) {
57
- const { rscClientRuntimePath, rscServerRuntimePath, internalDirectory } = options;
58
- rsbuildPlugins.push((0, rsbuild_rsc_plugin_js_namespaceObject.rsbuildRscPlugin)({
59
- appDir: options.cwd,
60
- rscClientRuntimePath,
61
- rscServerRuntimePath,
62
- internalDirectory
63
- }));
57
+ const rscPlugins = await (0, rscConfig_js_namespaceObject.getRscPlugins)(enableRsc, options.internalDirectory);
58
+ rsbuildPlugins.push(...rscPlugins);
64
59
  } else rsbuildPlugins.push((0, rscClientBrowserFallback_js_namespaceObject.rscClientBrowserFallbackPlugin)());
65
60
  return {
66
61
  rsbuildConfig,
@@ -51,9 +51,9 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
51
51
  }
52
52
  };
53
53
  }
54
- if (config.environments?.node) {
54
+ if (config.environments?.server) {
55
55
  compatConfig.environments ??= {};
56
- compatConfig.environments.node = {
56
+ compatConfig.environments.server = {
57
57
  output: {
58
58
  emitAssets: false,
59
59
  distPath: {
@@ -71,8 +71,8 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
71
71
  api.modifyRsbuildConfig({
72
72
  handler: (config)=>{
73
73
  const environmentNameOrder = [
74
- 'web',
75
- 'node',
74
+ 'client',
75
+ 'server',
76
76
  'workerSSR'
77
77
  ];
78
78
  config.environments = Object.fromEntries(Object.entries(config.environments).sort((a1, a2)=>environmentNameOrder.includes(a1[0]) ? environmentNameOrder.indexOf(a1[0]) - environmentNameOrder.indexOf(a2[0]) : 1));
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ getRscPlugins: ()=>getRscPlugins,
37
+ pluginRscConfig: ()=>pluginRscConfig
38
+ });
39
+ const external_path_namespaceObject = require("path");
40
+ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
41
+ const ASYNC_STORAGE_PATTERN = /universal[/\\]async_storage/;
42
+ const RSC_COMMON_LAYER = 'rsc-common';
43
+ const ENTRY_NAME_VAR = '__MODERN_JS_ENTRY_NAME';
44
+ const createVirtualModule = (content)=>`data:text/javascript,${encodeURIComponent(content)}`;
45
+ const isAsyncStorageExclude = (exclude)=>{
46
+ if ('string' == typeof exclude) return ASYNC_STORAGE_PATTERN.test(exclude);
47
+ if (exclude instanceof RegExp) return exclude.test('universal/async_storage') || exclude.test('universal\\async_storage');
48
+ return false;
49
+ };
50
+ function pluginRscConfig() {
51
+ return {
52
+ name: 'builder:rsc-config',
53
+ setup (api) {
54
+ let layersCache = null;
55
+ const getLayers = async ()=>{
56
+ if (!layersCache) {
57
+ const { Layers } = await import("rsbuild-plugin-rsc");
58
+ layersCache = Layers;
59
+ }
60
+ return layersCache;
61
+ };
62
+ api.modifyBundlerChain({
63
+ handler: (chain, { isServer })=>{
64
+ if (isServer) {
65
+ const routeFilePattern = /[/\\]routes[/\\](?:.*[/\\])?(?:layout|page|\$)\.[tj]sx?$/;
66
+ const appFilePattern = /[/\\]App\.[tj]sx?$/;
67
+ const combinedPattern = new RegExp(`(${routeFilePattern.source}|${appFilePattern.source})`);
68
+ let loaderPath;
69
+ try {
70
+ loaderPath = require.resolve('../shared/rsc/rsc-server-entry-loader');
71
+ } catch {
72
+ loaderPath = external_path_default().resolve(__dirname, '../shared/rsc/rsc-server-entry-loader');
73
+ }
74
+ chain.module.rule('rsc-server-entry').test(/\.(tsx?|jsx?)$/).resource(combinedPattern).exclude.add(/node_modules/).end().use('rsc-server-entry-loader').loader(loaderPath).end();
75
+ }
76
+ },
77
+ order: 'pre'
78
+ });
79
+ api.modifyRspackConfig(async (config, utils)=>{
80
+ const isServer = 'node' === config.target || 'node' === utils.target || utils.environment?.name === 'server';
81
+ if (!isServer) return;
82
+ const Layers = await getLayers();
83
+ if (config.entry) {
84
+ const entries = config.entry;
85
+ const newEntries = {};
86
+ for (const [entryName, entryValue] of Object.entries(entries))if ('string' == typeof entryValue) newEntries[entryName] = {
87
+ import: entryValue,
88
+ layer: Layers.ssr
89
+ };
90
+ else if (Array.isArray(entryValue)) newEntries[entryName] = {
91
+ import: entryValue,
92
+ layer: Layers.ssr
93
+ };
94
+ else if ('object' == typeof entryValue && null !== entryValue) newEntries[entryName] = {
95
+ ...entryValue,
96
+ layer: Layers.ssr
97
+ };
98
+ else newEntries[entryName] = entryValue;
99
+ config.entry = newEntries;
100
+ }
101
+ if (config.module?.rules) {
102
+ const rules = config.module.rules;
103
+ for (const rule of rules)if (rule.layer === Layers.rsc) {
104
+ if (rule.exclude) {
105
+ if (!Array.isArray(rule.exclude)) rule.exclude = [
106
+ rule.exclude
107
+ ];
108
+ } else rule.exclude = [];
109
+ const hasExclude = rule.exclude.some(isAsyncStorageExclude);
110
+ if (!hasExclude) rule.exclude.push(ASYNC_STORAGE_PATTERN);
111
+ }
112
+ if (!Array.isArray(config.module.rules)) config.module.rules = [];
113
+ config.module.rules.push({
114
+ resource: ASYNC_STORAGE_PATTERN,
115
+ layer: RSC_COMMON_LAYER
116
+ });
117
+ }
118
+ });
119
+ api.modifyBundlerChain((chain, { isServer, isWebWorker })=>{
120
+ if (!isServer && !isWebWorker) {
121
+ const entries = chain.entryPoints.entries();
122
+ if (entries && 'object' == typeof entries) for (const entryName of Object.keys(entries)){
123
+ const entryPoint = chain.entry(entryName);
124
+ const code = `window.${ENTRY_NAME_VAR}="${entryName}";`;
125
+ entryPoint.add(createVirtualModule(code));
126
+ }
127
+ }
128
+ });
129
+ }
130
+ };
131
+ }
132
+ async function getRscPlugins(enableRsc, internalDirectory) {
133
+ if (enableRsc) {
134
+ const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, '[/\\\\]')}[/\\\\][^/\\\\]*[/\\\\]routes`);
135
+ const { pluginRSC } = await import("rsbuild-plugin-rsc");
136
+ return [
137
+ pluginRSC({
138
+ layers: {
139
+ rsc: [
140
+ /render[/\\].*[/\\]server[/\\]rsc/,
141
+ /AppProxy/,
142
+ routesFileReg
143
+ ]
144
+ }
145
+ }),
146
+ pluginRscConfig()
147
+ ];
148
+ }
149
+ return [];
150
+ }
151
+ exports.getRscPlugins = __webpack_exports__.getRscPlugins;
152
+ exports.pluginRscConfig = __webpack_exports__.pluginRscConfig;
153
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
154
+ "getRscPlugins",
155
+ "pluginRscConfig"
156
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
157
+ Object.defineProperty(exports, '__esModule', {
158
+ value: true
159
+ });
@@ -41,7 +41,8 @@ const pluginRuntimeChunk = (disableInlineRuntimeChunk)=>({
41
41
  api.modifyRsbuildConfig((config)=>{
42
42
  config.output ||= {};
43
43
  if (disableInlineRuntimeChunk) return;
44
- if (!config.output.inlineScripts) config.output.inlineScripts = utils_js_namespaceObject.RUNTIME_CHUNK_REGEX;
44
+ const originalConfig = api.getRsbuildConfig('original');
45
+ if (originalConfig.output?.inlineScripts === void 0) config.output.inlineScripts = utils_js_namespaceObject.RUNTIME_CHUNK_REGEX;
45
46
  });
46
47
  }
47
48
  });
@@ -24,14 +24,13 @@ var __webpack_require__ = {};
24
24
  var __webpack_exports__ = {};
25
25
  __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
- default: ()=>rscCssLoader
27
+ default: ()=>rscServerEntryLoader
28
28
  });
29
- const external_common_js_namespaceObject = require("./common.js");
30
- function rscCssLoader(source) {
31
- this._module && (0, external_common_js_namespaceObject.setRscBuildInfo)(this._module, {
32
- isCssModule: true
33
- });
34
- return source;
29
+ function rscServerEntryLoader(source) {
30
+ this.cacheable(true);
31
+ const hasServerEntryDirective = source.includes("'use server-entry'") || source.includes('"use server-entry"') || source.includes('`use server-entry`');
32
+ if (hasServerEntryDirective) return source;
33
+ return `'use server-entry';\n${source}`;
35
34
  }
36
35
  exports["default"] = __webpack_exports__["default"];
37
36
  for(var __rspack_i in __webpack_exports__)if (-1 === [
@@ -50,7 +50,7 @@ function rscClientBrowserFallbackPlugin() {
50
50
  api.modifyRspackConfig((config)=>{
51
51
  config.resolve ??= {};
52
52
  config.resolve.fallback ??= {};
53
- config.resolve.fallback['react-server-dom-webpack/client.browser'] = emptyModulePath;
53
+ config.resolve.fallback['react-server-dom-rspack/client.browser'] = emptyModulePath;
54
54
  });
55
55
  }
56
56
  };
@@ -1,5 +1,5 @@
1
1
  import { createRsbuild } from "@rsbuild/core";
2
- import { rsbuildRscPlugin } from "./rsc/plugins/rsbuild-rsc-plugin.mjs";
2
+ import { getRscPlugins } from "./plugins/rscConfig.mjs";
3
3
  import { parseCommonConfig } from "./shared/parseCommonConfig.mjs";
4
4
  import { rscClientBrowserFallbackPlugin } from "./shared/rsc/rscClientBrowserFallback.mjs";
5
5
  async function parseConfig(builderConfig, options) {
@@ -25,13 +25,8 @@ async function parseConfig(builderConfig, options) {
25
25
  }
26
26
  const enableRsc = builderConfig.server?.rsc ?? false;
27
27
  if (enableRsc) {
28
- const { rscClientRuntimePath, rscServerRuntimePath, internalDirectory } = options;
29
- rsbuildPlugins.push(rsbuildRscPlugin({
30
- appDir: options.cwd,
31
- rscClientRuntimePath,
32
- rscServerRuntimePath,
33
- internalDirectory
34
- }));
28
+ const rscPlugins = await getRscPlugins(enableRsc, options.internalDirectory);
29
+ rsbuildPlugins.push(...rscPlugins);
35
30
  } else rsbuildPlugins.push(rscClientBrowserFallbackPlugin());
36
31
  return {
37
32
  rsbuildConfig,
@@ -23,9 +23,9 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
23
23
  }
24
24
  };
25
25
  }
26
- if (config.environments?.node) {
26
+ if (config.environments?.server) {
27
27
  compatConfig.environments ??= {};
28
- compatConfig.environments.node = {
28
+ compatConfig.environments.server = {
29
29
  output: {
30
30
  emitAssets: false,
31
31
  distPath: {
@@ -43,8 +43,8 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
43
43
  api.modifyRsbuildConfig({
44
44
  handler: (config)=>{
45
45
  const environmentNameOrder = [
46
- 'web',
47
- 'node',
46
+ 'client',
47
+ 'server',
48
48
  'workerSSR'
49
49
  ];
50
50
  config.environments = Object.fromEntries(Object.entries(config.environments).sort((a1, a2)=>environmentNameOrder.includes(a1[0]) ? environmentNameOrder.indexOf(a1[0]) - environmentNameOrder.indexOf(a2[0]) : 1));
@@ -0,0 +1,112 @@
1
+ import path from "path";
2
+ const ASYNC_STORAGE_PATTERN = /universal[/\\]async_storage/;
3
+ const RSC_COMMON_LAYER = 'rsc-common';
4
+ const ENTRY_NAME_VAR = '__MODERN_JS_ENTRY_NAME';
5
+ const createVirtualModule = (content)=>`data:text/javascript,${encodeURIComponent(content)}`;
6
+ const isAsyncStorageExclude = (exclude)=>{
7
+ if ('string' == typeof exclude) return ASYNC_STORAGE_PATTERN.test(exclude);
8
+ if (exclude instanceof RegExp) return exclude.test('universal/async_storage') || exclude.test('universal\\async_storage');
9
+ return false;
10
+ };
11
+ function pluginRscConfig() {
12
+ return {
13
+ name: 'builder:rsc-config',
14
+ setup (api) {
15
+ let layersCache = null;
16
+ const getLayers = async ()=>{
17
+ if (!layersCache) {
18
+ const { Layers } = await import("rsbuild-plugin-rsc");
19
+ layersCache = Layers;
20
+ }
21
+ return layersCache;
22
+ };
23
+ api.modifyBundlerChain({
24
+ handler: (chain, { isServer })=>{
25
+ if (isServer) {
26
+ const routeFilePattern = /[/\\]routes[/\\](?:.*[/\\])?(?:layout|page|\$)\.[tj]sx?$/;
27
+ const appFilePattern = /[/\\]App\.[tj]sx?$/;
28
+ const combinedPattern = new RegExp(`(${routeFilePattern.source}|${appFilePattern.source})`);
29
+ let loaderPath;
30
+ try {
31
+ loaderPath = require.resolve('../shared/rsc/rsc-server-entry-loader');
32
+ } catch {
33
+ loaderPath = path.resolve(__dirname, '../shared/rsc/rsc-server-entry-loader');
34
+ }
35
+ chain.module.rule('rsc-server-entry').test(/\.(tsx?|jsx?)$/).resource(combinedPattern).exclude.add(/node_modules/).end().use('rsc-server-entry-loader').loader(loaderPath).end();
36
+ }
37
+ },
38
+ order: 'pre'
39
+ });
40
+ api.modifyRspackConfig(async (config, utils)=>{
41
+ const isServer = 'node' === config.target || 'node' === utils.target || utils.environment?.name === 'server';
42
+ if (!isServer) return;
43
+ const Layers = await getLayers();
44
+ if (config.entry) {
45
+ const entries = config.entry;
46
+ const newEntries = {};
47
+ for (const [entryName, entryValue] of Object.entries(entries))if ('string' == typeof entryValue) newEntries[entryName] = {
48
+ import: entryValue,
49
+ layer: Layers.ssr
50
+ };
51
+ else if (Array.isArray(entryValue)) newEntries[entryName] = {
52
+ import: entryValue,
53
+ layer: Layers.ssr
54
+ };
55
+ else if ('object' == typeof entryValue && null !== entryValue) newEntries[entryName] = {
56
+ ...entryValue,
57
+ layer: Layers.ssr
58
+ };
59
+ else newEntries[entryName] = entryValue;
60
+ config.entry = newEntries;
61
+ }
62
+ if (config.module?.rules) {
63
+ const rules = config.module.rules;
64
+ for (const rule of rules)if (rule.layer === Layers.rsc) {
65
+ if (rule.exclude) {
66
+ if (!Array.isArray(rule.exclude)) rule.exclude = [
67
+ rule.exclude
68
+ ];
69
+ } else rule.exclude = [];
70
+ const hasExclude = rule.exclude.some(isAsyncStorageExclude);
71
+ if (!hasExclude) rule.exclude.push(ASYNC_STORAGE_PATTERN);
72
+ }
73
+ if (!Array.isArray(config.module.rules)) config.module.rules = [];
74
+ config.module.rules.push({
75
+ resource: ASYNC_STORAGE_PATTERN,
76
+ layer: RSC_COMMON_LAYER
77
+ });
78
+ }
79
+ });
80
+ api.modifyBundlerChain((chain, { isServer, isWebWorker })=>{
81
+ if (!isServer && !isWebWorker) {
82
+ const entries = chain.entryPoints.entries();
83
+ if (entries && 'object' == typeof entries) for (const entryName of Object.keys(entries)){
84
+ const entryPoint = chain.entry(entryName);
85
+ const code = `window.${ENTRY_NAME_VAR}="${entryName}";`;
86
+ entryPoint.add(createVirtualModule(code));
87
+ }
88
+ }
89
+ });
90
+ }
91
+ };
92
+ }
93
+ async function getRscPlugins(enableRsc, internalDirectory) {
94
+ if (enableRsc) {
95
+ const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, '[/\\\\]')}[/\\\\][^/\\\\]*[/\\\\]routes`);
96
+ const { pluginRSC } = await import("rsbuild-plugin-rsc");
97
+ return [
98
+ pluginRSC({
99
+ layers: {
100
+ rsc: [
101
+ /render[/\\].*[/\\]server[/\\]rsc/,
102
+ /AppProxy/,
103
+ routesFileReg
104
+ ]
105
+ }
106
+ }),
107
+ pluginRscConfig()
108
+ ];
109
+ }
110
+ return [];
111
+ }
112
+ export { getRscPlugins, pluginRscConfig };
@@ -13,7 +13,8 @@ const pluginRuntimeChunk = (disableInlineRuntimeChunk)=>({
13
13
  api.modifyRsbuildConfig((config)=>{
14
14
  config.output ||= {};
15
15
  if (disableInlineRuntimeChunk) return;
16
- if (!config.output.inlineScripts) config.output.inlineScripts = RUNTIME_CHUNK_REGEX;
16
+ const originalConfig = api.getRsbuildConfig('original');
17
+ if (originalConfig.output?.inlineScripts === void 0) config.output.inlineScripts = RUNTIME_CHUNK_REGEX;
17
18
  });
18
19
  }
19
20
  });
@@ -0,0 +1,7 @@
1
+ function rscServerEntryLoader(source) {
2
+ this.cacheable(true);
3
+ const hasServerEntryDirective = source.includes("'use server-entry'") || source.includes('"use server-entry"') || source.includes('`use server-entry`');
4
+ if (hasServerEntryDirective) return source;
5
+ return `'use server-entry';\n${source}`;
6
+ }
7
+ export { rscServerEntryLoader as default };
@@ -12,7 +12,7 @@ function rscClientBrowserFallbackPlugin() {
12
12
  api.modifyRspackConfig((config)=>{
13
13
  config.resolve ??= {};
14
14
  config.resolve.fallback ??= {};
15
- config.resolve.fallback['react-server-dom-webpack/client.browser'] = emptyModulePath;
15
+ config.resolve.fallback['react-server-dom-rspack/client.browser'] = emptyModulePath;
16
16
  });
17
17
  }
18
18
  };
@@ -1,6 +1,6 @@
1
1
  import "node:module";
2
2
  import { createRsbuild } from "@rsbuild/core";
3
- import { rsbuildRscPlugin } from "./rsc/plugins/rsbuild-rsc-plugin.mjs";
3
+ import { getRscPlugins } from "./plugins/rscConfig.mjs";
4
4
  import { parseCommonConfig } from "./shared/parseCommonConfig.mjs";
5
5
  import { rscClientBrowserFallbackPlugin } from "./shared/rsc/rscClientBrowserFallback.mjs";
6
6
  async function parseConfig(builderConfig, options) {
@@ -26,13 +26,8 @@ async function parseConfig(builderConfig, options) {
26
26
  }
27
27
  const enableRsc = builderConfig.server?.rsc ?? false;
28
28
  if (enableRsc) {
29
- const { rscClientRuntimePath, rscServerRuntimePath, internalDirectory } = options;
30
- rsbuildPlugins.push(rsbuildRscPlugin({
31
- appDir: options.cwd,
32
- rscClientRuntimePath,
33
- rscServerRuntimePath,
34
- internalDirectory
35
- }));
29
+ const rscPlugins = await getRscPlugins(enableRsc, options.internalDirectory);
30
+ rsbuildPlugins.push(...rscPlugins);
36
31
  } else rsbuildPlugins.push(rscClientBrowserFallbackPlugin());
37
32
  return {
38
33
  rsbuildConfig,
@@ -24,9 +24,9 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
24
24
  }
25
25
  };
26
26
  }
27
- if (config.environments?.node) {
27
+ if (config.environments?.server) {
28
28
  compatConfig.environments ??= {};
29
- compatConfig.environments.node = {
29
+ compatConfig.environments.server = {
30
30
  output: {
31
31
  emitAssets: false,
32
32
  distPath: {
@@ -44,8 +44,8 @@ const pluginEnvironmentDefaults = (distPath = {})=>({
44
44
  api.modifyRsbuildConfig({
45
45
  handler: (config)=>{
46
46
  const environmentNameOrder = [
47
- 'web',
48
- 'node',
47
+ 'client',
48
+ 'server',
49
49
  'workerSSR'
50
50
  ];
51
51
  config.environments = Object.fromEntries(Object.entries(config.environments).sort((a1, a2)=>environmentNameOrder.includes(a1[0]) ? environmentNameOrder.indexOf(a1[0]) - environmentNameOrder.indexOf(a2[0]) : 1));
@@ -0,0 +1,117 @@
1
+ import __rslib_shim_module__ from "node:module";
2
+ const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
+ import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
4
+ import { dirname as __rspack_dirname } from "node:path";
5
+ import path from "path";
6
+ var rscConfig_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
7
+ const ASYNC_STORAGE_PATTERN = /universal[/\\]async_storage/;
8
+ const RSC_COMMON_LAYER = 'rsc-common';
9
+ const ENTRY_NAME_VAR = '__MODERN_JS_ENTRY_NAME';
10
+ const createVirtualModule = (content)=>`data:text/javascript,${encodeURIComponent(content)}`;
11
+ const isAsyncStorageExclude = (exclude)=>{
12
+ if ('string' == typeof exclude) return ASYNC_STORAGE_PATTERN.test(exclude);
13
+ if (exclude instanceof RegExp) return exclude.test('universal/async_storage') || exclude.test('universal\\async_storage');
14
+ return false;
15
+ };
16
+ function pluginRscConfig() {
17
+ return {
18
+ name: 'builder:rsc-config',
19
+ setup (api) {
20
+ let layersCache = null;
21
+ const getLayers = async ()=>{
22
+ if (!layersCache) {
23
+ const { Layers } = await import("rsbuild-plugin-rsc");
24
+ layersCache = Layers;
25
+ }
26
+ return layersCache;
27
+ };
28
+ api.modifyBundlerChain({
29
+ handler: (chain, { isServer })=>{
30
+ if (isServer) {
31
+ const routeFilePattern = /[/\\]routes[/\\](?:.*[/\\])?(?:layout|page|\$)\.[tj]sx?$/;
32
+ const appFilePattern = /[/\\]App\.[tj]sx?$/;
33
+ const combinedPattern = new RegExp(`(${routeFilePattern.source}|${appFilePattern.source})`);
34
+ let loaderPath;
35
+ try {
36
+ loaderPath = require.resolve('../shared/rsc/rsc-server-entry-loader');
37
+ } catch {
38
+ loaderPath = path.resolve(rscConfig_dirname, '../shared/rsc/rsc-server-entry-loader');
39
+ }
40
+ chain.module.rule('rsc-server-entry').test(/\.(tsx?|jsx?)$/).resource(combinedPattern).exclude.add(/node_modules/).end().use('rsc-server-entry-loader').loader(loaderPath).end();
41
+ }
42
+ },
43
+ order: 'pre'
44
+ });
45
+ api.modifyRspackConfig(async (config, utils)=>{
46
+ const isServer = 'node' === config.target || 'node' === utils.target || utils.environment?.name === 'server';
47
+ if (!isServer) return;
48
+ const Layers = await getLayers();
49
+ if (config.entry) {
50
+ const entries = config.entry;
51
+ const newEntries = {};
52
+ for (const [entryName, entryValue] of Object.entries(entries))if ('string' == typeof entryValue) newEntries[entryName] = {
53
+ import: entryValue,
54
+ layer: Layers.ssr
55
+ };
56
+ else if (Array.isArray(entryValue)) newEntries[entryName] = {
57
+ import: entryValue,
58
+ layer: Layers.ssr
59
+ };
60
+ else if ('object' == typeof entryValue && null !== entryValue) newEntries[entryName] = {
61
+ ...entryValue,
62
+ layer: Layers.ssr
63
+ };
64
+ else newEntries[entryName] = entryValue;
65
+ config.entry = newEntries;
66
+ }
67
+ if (config.module?.rules) {
68
+ const rules = config.module.rules;
69
+ for (const rule of rules)if (rule.layer === Layers.rsc) {
70
+ if (rule.exclude) {
71
+ if (!Array.isArray(rule.exclude)) rule.exclude = [
72
+ rule.exclude
73
+ ];
74
+ } else rule.exclude = [];
75
+ const hasExclude = rule.exclude.some(isAsyncStorageExclude);
76
+ if (!hasExclude) rule.exclude.push(ASYNC_STORAGE_PATTERN);
77
+ }
78
+ if (!Array.isArray(config.module.rules)) config.module.rules = [];
79
+ config.module.rules.push({
80
+ resource: ASYNC_STORAGE_PATTERN,
81
+ layer: RSC_COMMON_LAYER
82
+ });
83
+ }
84
+ });
85
+ api.modifyBundlerChain((chain, { isServer, isWebWorker })=>{
86
+ if (!isServer && !isWebWorker) {
87
+ const entries = chain.entryPoints.entries();
88
+ if (entries && 'object' == typeof entries) for (const entryName of Object.keys(entries)){
89
+ const entryPoint = chain.entry(entryName);
90
+ const code = `window.${ENTRY_NAME_VAR}="${entryName}";`;
91
+ entryPoint.add(createVirtualModule(code));
92
+ }
93
+ }
94
+ });
95
+ }
96
+ };
97
+ }
98
+ async function getRscPlugins(enableRsc, internalDirectory) {
99
+ if (enableRsc) {
100
+ const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, '[/\\\\]')}[/\\\\][^/\\\\]*[/\\\\]routes`);
101
+ const { pluginRSC } = await import("rsbuild-plugin-rsc");
102
+ return [
103
+ pluginRSC({
104
+ layers: {
105
+ rsc: [
106
+ /render[/\\].*[/\\]server[/\\]rsc/,
107
+ /AppProxy/,
108
+ routesFileReg
109
+ ]
110
+ }
111
+ }),
112
+ pluginRscConfig()
113
+ ];
114
+ }
115
+ return [];
116
+ }
117
+ export { getRscPlugins, pluginRscConfig };
@@ -14,7 +14,8 @@ const pluginRuntimeChunk = (disableInlineRuntimeChunk)=>({
14
14
  api.modifyRsbuildConfig((config)=>{
15
15
  config.output ||= {};
16
16
  if (disableInlineRuntimeChunk) return;
17
- if (!config.output.inlineScripts) config.output.inlineScripts = RUNTIME_CHUNK_REGEX;
17
+ const originalConfig = api.getRsbuildConfig('original');
18
+ if (originalConfig.output?.inlineScripts === void 0) config.output.inlineScripts = RUNTIME_CHUNK_REGEX;
18
19
  });
19
20
  }
20
21
  });
@@ -0,0 +1,8 @@
1
+ import "node:module";
2
+ function rscServerEntryLoader(source) {
3
+ this.cacheable(true);
4
+ const hasServerEntryDirective = source.includes("'use server-entry'") || source.includes('"use server-entry"') || source.includes('`use server-entry`');
5
+ if (hasServerEntryDirective) return source;
6
+ return `'use server-entry';\n${source}`;
7
+ }
8
+ export { rscServerEntryLoader as default };
@@ -17,7 +17,7 @@ function rscClientBrowserFallbackPlugin() {
17
17
  api.modifyRspackConfig((config)=>{
18
18
  config.resolve ??= {};
19
19
  config.resolve.fallback ??= {};
20
- config.resolve.fallback['react-server-dom-webpack/client.browser'] = emptyModulePath;
20
+ config.resolve.fallback['react-server-dom-rspack/client.browser'] = emptyModulePath;
21
21
  });
22
22
  }
23
23
  };