@modern-js/builder 3.0.0 → 3.0.2

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 (59) hide show
  1. package/dist/cjs/createBuilder.js +3 -8
  2. package/dist/cjs/plugins/environmentDefaults.js +4 -4
  3. package/dist/cjs/plugins/postcss.js +8 -31
  4. package/dist/cjs/plugins/rscConfig.js +159 -0
  5. package/dist/cjs/{rsc/rsc-css-loader.js → shared/rsc/rsc-server-entry-loader.js} +6 -7
  6. package/dist/esm/createBuilder.mjs +3 -8
  7. package/dist/esm/plugins/environmentDefaults.mjs +4 -4
  8. package/dist/esm/plugins/postcss.mjs +8 -31
  9. package/dist/esm/plugins/rscConfig.mjs +112 -0
  10. package/dist/esm/shared/rsc/rsc-server-entry-loader.mjs +7 -0
  11. package/dist/esm-node/createBuilder.mjs +3 -8
  12. package/dist/esm-node/plugins/environmentDefaults.mjs +4 -4
  13. package/dist/esm-node/plugins/postcss.mjs +8 -31
  14. package/dist/esm-node/plugins/rscConfig.mjs +117 -0
  15. package/dist/esm-node/shared/rsc/rsc-server-entry-loader.mjs +8 -0
  16. package/dist/types/plugins/postcss.d.ts +1 -1
  17. package/dist/types/plugins/rscConfig.d.ts +18 -0
  18. package/dist/types/shared/rsc/rsc-server-entry-loader.d.ts +5 -0
  19. package/package.json +7 -8
  20. package/compiled/postcss-load-config/index.d.ts +0 -1
  21. package/compiled/postcss-load-config/index.js +0 -14
  22. package/compiled/postcss-load-config/license +0 -20
  23. package/compiled/postcss-load-config/package.json +0 -1
  24. package/dist/cjs/rsc/common.js +0 -157
  25. package/dist/cjs/rsc/plugins/rsbuild-rsc-plugin.js +0 -169
  26. package/dist/cjs/rsc/plugins/rspack-rsc-client-plugin.js +0 -187
  27. package/dist/cjs/rsc/plugins/rspack-rsc-server-plugin.js +0 -245
  28. package/dist/cjs/rsc/rsc-client-loader.js +0 -71
  29. package/dist/cjs/rsc/rsc-server-loader.js +0 -102
  30. package/dist/cjs/rsc/rsc-ssr-loader.js +0 -60
  31. package/dist/compiled/postcss-load-config/index.d.ts +0 -1
  32. package/dist/compiled/postcss-load-config/index.js +0 -608
  33. package/dist/compiled/postcss-load-config/index.js.LICENSE.txt +0 -13
  34. package/dist/compiled/postcss-load-config/license +0 -20
  35. package/dist/compiled/postcss-load-config/package.json +0 -1
  36. package/dist/esm/rsc/common.mjs +0 -87
  37. package/dist/esm/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -124
  38. package/dist/esm/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -143
  39. package/dist/esm/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -211
  40. package/dist/esm/rsc/rsc-client-loader.mjs +0 -37
  41. package/dist/esm/rsc/rsc-css-loader.mjs +0 -8
  42. package/dist/esm/rsc/rsc-server-loader.mjs +0 -58
  43. package/dist/esm/rsc/rsc-ssr-loader.mjs +0 -26
  44. package/dist/esm-node/rsc/common.mjs +0 -88
  45. package/dist/esm-node/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -126
  46. package/dist/esm-node/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -144
  47. package/dist/esm-node/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -212
  48. package/dist/esm-node/rsc/rsc-client-loader.mjs +0 -38
  49. package/dist/esm-node/rsc/rsc-css-loader.mjs +0 -9
  50. package/dist/esm-node/rsc/rsc-server-loader.mjs +0 -60
  51. package/dist/esm-node/rsc/rsc-ssr-loader.mjs +0 -27
  52. package/dist/types/rsc/common.d.ts +0 -22
  53. package/dist/types/rsc/plugins/rsbuild-rsc-plugin.d.ts +0 -7
  54. package/dist/types/rsc/plugins/rspack-rsc-client-plugin.d.ts +0 -14
  55. package/dist/types/rsc/plugins/rspack-rsc-server-plugin.d.ts +0 -32
  56. package/dist/types/rsc/rsc-client-loader.d.ts +0 -7
  57. package/dist/types/rsc/rsc-css-loader.d.ts +0 -2
  58. package/dist/types/rsc/rsc-server-loader.d.ts +0 -6
  59. 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));
@@ -30,33 +30,11 @@ __webpack_require__.d(__webpack_exports__, {
30
30
  pluginPostcss: ()=>pluginPostcss
31
31
  });
32
32
  const external_node_module_namespaceObject = require("node:module");
33
- const external_node_url_namespaceObject = require("node:url");
34
33
  const utils_namespaceObject = require("@modern-js/utils");
34
+ const core_namespaceObject = require("@rsbuild/core");
35
35
  const getCssSupport_js_namespaceObject = require("../shared/getCssSupport.js");
36
36
  const postcss_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
37
37
  const importPostcssPlugin = (name)=>Promise.resolve(postcss_require(name));
38
- const userPostcssrcCache = new Map();
39
- const clonePostCSSConfig = (config)=>({
40
- ...config,
41
- plugins: config.plugins ? [
42
- ...config.plugins
43
- ] : void 0
44
- });
45
- async function loadUserPostcssrc(root) {
46
- const cached = userPostcssrcCache.get(root);
47
- if (cached) return clonePostCSSConfig(await cached);
48
- const compiledPath = postcss_require.resolve('../../compiled/postcss-load-config');
49
- const { default: postcssrc } = await import((0, external_node_url_namespaceObject.pathToFileURL)(compiledPath).href);
50
- const promise = postcssrc({}, root).catch((err)=>{
51
- if (err.message?.includes('No PostCSS Config found')) return {};
52
- throw err;
53
- });
54
- userPostcssrcCache.set(root, promise);
55
- return promise.then((config)=>{
56
- userPostcssrcCache.set(root, config);
57
- return clonePostCSSConfig(config);
58
- });
59
- }
60
38
  const pluginPostcss = (options = {})=>({
61
39
  name: 'builder:postcss-plugins',
62
40
  pre: [
@@ -92,17 +70,16 @@ const pluginPostcss = (options = {})=>({
92
70
  overrideBrowserslist: config.output.overrideBrowserslist
93
71
  }, autoprefixer)))
94
72
  ]).then((results)=>results.filter(Boolean));
95
- const userOptions = await loadUserPostcssrc(api.context.rootPath);
96
73
  return mergeEnvironmentConfig({
97
74
  tools: {
98
- postcss: {
99
- postcssOptions: {
100
- ...userOptions,
101
- plugins: [
102
- ...userOptions.plugins || [],
103
- ...plugins
104
- ]
75
+ postcss: (opts)=>{
76
+ if ('function' == typeof opts.postcssOptions) {
77
+ core_namespaceObject.logger.warn('unexpected function type postcssOptions, the default postcss plugins will not be applied.');
78
+ return opts;
105
79
  }
80
+ opts.postcssOptions ??= {};
81
+ opts.postcssOptions.plugins ??= [];
82
+ opts.postcssOptions.plugins.push(...plugins);
106
83
  }
107
84
  }
108
85
  }, config);
@@ -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
+ });
@@ -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 === [
@@ -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));
@@ -1,31 +1,9 @@
1
1
  import { createRequire } from "node:module";
2
- import { pathToFileURL } from "node:url";
3
2
  import { applyOptionsChain, isProd } from "@modern-js/utils";
3
+ import { logger } from "@rsbuild/core";
4
4
  import { getCssSupport } from "../shared/getCssSupport.mjs";
5
5
  const postcss_require = createRequire(import.meta.url);
6
6
  const importPostcssPlugin = (name)=>Promise.resolve(postcss_require(name));
7
- const userPostcssrcCache = new Map();
8
- const clonePostCSSConfig = (config)=>({
9
- ...config,
10
- plugins: config.plugins ? [
11
- ...config.plugins
12
- ] : void 0
13
- });
14
- async function loadUserPostcssrc(root) {
15
- const cached = userPostcssrcCache.get(root);
16
- if (cached) return clonePostCSSConfig(await cached);
17
- const compiledPath = postcss_require.resolve('../../compiled/postcss-load-config');
18
- const { default: postcssrc } = await import(pathToFileURL(compiledPath).href);
19
- const promise = postcssrc({}, root).catch((err)=>{
20
- if (err.message?.includes('No PostCSS Config found')) return {};
21
- throw err;
22
- });
23
- userPostcssrcCache.set(root, promise);
24
- return promise.then((config)=>{
25
- userPostcssrcCache.set(root, config);
26
- return clonePostCSSConfig(config);
27
- });
28
- }
29
7
  const pluginPostcss = (options = {})=>({
30
8
  name: 'builder:postcss-plugins',
31
9
  pre: [
@@ -61,17 +39,16 @@ const pluginPostcss = (options = {})=>({
61
39
  overrideBrowserslist: config.output.overrideBrowserslist
62
40
  }, autoprefixer)))
63
41
  ]).then((results)=>results.filter(Boolean));
64
- const userOptions = await loadUserPostcssrc(api.context.rootPath);
65
42
  return mergeEnvironmentConfig({
66
43
  tools: {
67
- postcss: {
68
- postcssOptions: {
69
- ...userOptions,
70
- plugins: [
71
- ...userOptions.plugins || [],
72
- ...plugins
73
- ]
44
+ postcss: (opts)=>{
45
+ if ('function' == typeof opts.postcssOptions) {
46
+ logger.warn('unexpected function type postcssOptions, the default postcss plugins will not be applied.');
47
+ return opts;
74
48
  }
49
+ opts.postcssOptions ??= {};
50
+ opts.postcssOptions.plugins ??= [];
51
+ opts.postcssOptions.plugins.push(...plugins);
75
52
  }
76
53
  }
77
54
  }, config);
@@ -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 };
@@ -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 };
@@ -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));
@@ -1,32 +1,10 @@
1
1
  import "node:module";
2
2
  import { createRequire } from "node:module";
3
- import { pathToFileURL } from "node:url";
4
3
  import { applyOptionsChain, isProd } from "@modern-js/utils";
4
+ import { logger } from "@rsbuild/core";
5
5
  import { getCssSupport } from "../shared/getCssSupport.mjs";
6
6
  const postcss_require = createRequire(import.meta.url);
7
7
  const importPostcssPlugin = (name)=>Promise.resolve(postcss_require(name));
8
- const userPostcssrcCache = new Map();
9
- const clonePostCSSConfig = (config)=>({
10
- ...config,
11
- plugins: config.plugins ? [
12
- ...config.plugins
13
- ] : void 0
14
- });
15
- async function loadUserPostcssrc(root) {
16
- const cached = userPostcssrcCache.get(root);
17
- if (cached) return clonePostCSSConfig(await cached);
18
- const compiledPath = postcss_require.resolve('../../compiled/postcss-load-config');
19
- const { default: postcssrc } = await import(pathToFileURL(compiledPath).href);
20
- const promise = postcssrc({}, root).catch((err)=>{
21
- if (err.message?.includes('No PostCSS Config found')) return {};
22
- throw err;
23
- });
24
- userPostcssrcCache.set(root, promise);
25
- return promise.then((config)=>{
26
- userPostcssrcCache.set(root, config);
27
- return clonePostCSSConfig(config);
28
- });
29
- }
30
8
  const pluginPostcss = (options = {})=>({
31
9
  name: 'builder:postcss-plugins',
32
10
  pre: [
@@ -62,17 +40,16 @@ const pluginPostcss = (options = {})=>({
62
40
  overrideBrowserslist: config.output.overrideBrowserslist
63
41
  }, autoprefixer)))
64
42
  ]).then((results)=>results.filter(Boolean));
65
- const userOptions = await loadUserPostcssrc(api.context.rootPath);
66
43
  return mergeEnvironmentConfig({
67
44
  tools: {
68
- postcss: {
69
- postcssOptions: {
70
- ...userOptions,
71
- plugins: [
72
- ...userOptions.plugins || [],
73
- ...plugins
74
- ]
45
+ postcss: (opts)=>{
46
+ if ('function' == typeof opts.postcssOptions) {
47
+ logger.warn('unexpected function type postcssOptions, the default postcss plugins will not be applied.');
48
+ return opts;
75
49
  }
50
+ opts.postcssOptions ??= {};
51
+ opts.postcssOptions.plugins ??= [];
52
+ opts.postcssOptions.plugins.push(...plugins);
76
53
  }
77
54
  }
78
55
  }, config);