@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
@@ -0,0 +1,18 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ /**
3
+ * Unified plugin for RSC (React Server Components) configuration
4
+ * Handles:
5
+ * 1. Adding layer configuration to server-side entries
6
+ * 2. Excluding /universal[/\\]async_storage/ from react-server-components layer
7
+ * 3. Adding rsc-common layer for /universal[/\\]async_storage/
8
+ * 4. Adding entry name virtual module for client-side entries
9
+ * 5. Adding 'use server-entry' directive to route components
10
+ */
11
+ export declare function pluginRscConfig(): RsbuildPlugin;
12
+ /**
13
+ * Get RSC plugins based on configuration
14
+ * @param enableRsc - Whether RSC is enabled
15
+ * @param internalDirectory - Internal directory path for route matching
16
+ * @returns Array of RSC-related plugins
17
+ */
18
+ export declare function getRscPlugins(enableRsc: boolean, internalDirectory: string): Promise<RsbuildPlugin[]>;
@@ -0,0 +1,5 @@
1
+ import type { Rspack } from '@rsbuild/core';
2
+ /**
3
+ * Loader to add 'use server-entry'; directive at the top of route components
4
+ */
5
+ export default function rscServerEntryLoader(this: Rspack.LoaderContext, source: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/builder",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "A builder for Modern.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,7 +26,7 @@
26
26
  "dist"
27
27
  ],
28
28
  "dependencies": {
29
- "@rsbuild/core": "2.0.0-beta.2",
29
+ "@rsbuild/core": "2.0.0-beta.4",
30
30
  "@rsbuild/plugin-assets-retry": "1.5.1",
31
31
  "@rsbuild/plugin-check-syntax": "1.6.1",
32
32
  "@rsbuild/plugin-css-minimizer": "1.1.1",
@@ -38,7 +38,8 @@
38
38
  "@rsbuild/plugin-svgr": "1.3.0",
39
39
  "@rsbuild/plugin-type-check": "1.3.3",
40
40
  "@rsbuild/plugin-typed-css-modules": "1.2.1",
41
- "@swc/core": "1.15.10",
41
+ "@swc/core": "1.15.11",
42
+ "rsbuild-plugin-rsc": "0.0.1-beta.0",
42
43
  "@swc/helpers": "^0.5.17",
43
44
  "autoprefixer": "10.4.24",
44
45
  "browserslist": "4.28.1",
@@ -56,19 +57,18 @@
56
57
  "postcss-page-break": "3.0.4",
57
58
  "rspack-manifest-plugin": "5.2.1",
58
59
  "ts-deepmerge": "7.0.3",
59
- "@modern-js/flight-server-transform-plugin": "3.0.1",
60
- "@modern-js/utils": "3.0.1"
60
+ "@modern-js/utils": "3.0.3"
61
61
  },
62
62
  "devDependencies": {
63
- "@rslib/core": "0.19.4",
63
+ "@rslib/core": "0.19.6",
64
64
  "@types/html-minifier-terser": "^7.0.2",
65
65
  "@types/lodash": "^4.17.23",
66
66
  "react": "^19.2.4",
67
67
  "terser": "^5.46.0",
68
68
  "typescript": "^5.3.0",
69
- "@modern-js/rslib": "2.68.10",
70
- "@modern-js/types": "3.0.1",
71
- "@scripts/rstest-config": "2.66.0"
69
+ "@modern-js/types": "3.0.3",
70
+ "@scripts/rstest-config": "2.66.0",
71
+ "@modern-js/rslib": "2.68.10"
72
72
  },
73
73
  "publishConfig": {
74
74
  "access": "public",
@@ -1,157 +0,0 @@
1
- "use strict";
2
- var __webpack_require__ = {};
3
- (()=>{
4
- __webpack_require__.d = (exports1, definition)=>{
5
- for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
- enumerable: true,
7
- get: definition[key]
8
- });
9
- };
10
- })();
11
- (()=>{
12
- __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
- })();
14
- (()=>{
15
- __webpack_require__.r = (exports1)=>{
16
- if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
- value: 'Module'
18
- });
19
- Object.defineProperty(exports1, '__esModule', {
20
- value: true
21
- });
22
- };
23
- })();
24
- var __webpack_exports__ = {};
25
- __webpack_require__.r(__webpack_exports__);
26
- __webpack_require__.d(__webpack_exports__, {
27
- rspackRscLayerName: ()=>rspackRscLayerName,
28
- getExportNames: ()=>getExportNames,
29
- MODERN_RSC_INFO: ()=>MODERN_RSC_INFO,
30
- isCssModule: ()=>isCssModule,
31
- findRootIssuer: ()=>findRootIssuer,
32
- getRscBuildInfo: ()=>getRscBuildInfo,
33
- parseSource: ()=>parseSource,
34
- sharedData: ()=>sharedData,
35
- setBuildInfo: ()=>setBuildInfo,
36
- isServerModule: ()=>isServerModule,
37
- isClientModule: ()=>isClientModule,
38
- removeRscBuildInfo: ()=>removeRscBuildInfo,
39
- setRscBuildInfo: ()=>setRscBuildInfo
40
- });
41
- const utils_namespaceObject = require("@modern-js/utils");
42
- const core_namespaceObject = require("@swc/core");
43
- const rspackRscLayerName = "react-server";
44
- const MODERN_RSC_INFO = 'modernRscInfo';
45
- const sharedData = {
46
- store: new Map(),
47
- get (key) {
48
- return this.store.get(key);
49
- },
50
- set (key, value) {
51
- this.store.set(key, value);
52
- },
53
- clear () {
54
- this.store.clear();
55
- }
56
- };
57
- function setBuildInfo(mod, property) {
58
- if (!mod.buildInfo) mod.buildInfo = {};
59
- Object.assign(mod.buildInfo, property);
60
- }
61
- function setRscBuildInfo(mod, property) {
62
- if (!mod.buildInfo) mod.buildInfo = {};
63
- const rscBuildInfo = mod.buildInfo[MODERN_RSC_INFO] || {};
64
- Object.assign(rscBuildInfo, property);
65
- setBuildInfo(mod, {
66
- [MODERN_RSC_INFO]: rscBuildInfo
67
- });
68
- }
69
- function removeRscBuildInfo(mod) {
70
- delete mod.buildInfo?.[MODERN_RSC_INFO];
71
- }
72
- function getRscBuildInfo(mod) {
73
- return mod.buildInfo?.[MODERN_RSC_INFO];
74
- }
75
- function isCssModule(mod) {
76
- if (!mod) return false;
77
- return getRscBuildInfo(mod)?.isCssModule;
78
- }
79
- const parseSource = async (source)=>await (0, core_namespaceObject.parse)(source, {
80
- syntax: "typescript",
81
- tsx: true,
82
- dynamicImport: true
83
- });
84
- const getExportNames = async (ast, collectFuncOnly = false)=>{
85
- const exportNames = [];
86
- ast.body.forEach((node)=>{
87
- if ('ExportNamedDeclaration' === node.type) {
88
- const namedExport = node;
89
- namedExport.specifiers.forEach((specifier)=>{
90
- if ('ExportSpecifier' === specifier.type) exportNames.push(specifier.exported?.value || specifier.orig.value);
91
- });
92
- }
93
- if ('ExportDeclaration' === node.type) {
94
- if ('VariableDeclaration' === node.declaration.type) node.declaration.declarations.forEach((decl)=>{
95
- if ('Identifier' === decl.id.type) if (collectFuncOnly) {
96
- if (decl.init?.type === 'FunctionExpression' || decl.init?.type === 'ArrowFunctionExpression') exportNames.push(decl.id.value);
97
- } else exportNames.push(decl.id.value);
98
- });
99
- if ('ClassDeclaration' === node.declaration.type || 'FunctionDeclaration' === node.declaration.type) {
100
- if (node.declaration.identifier) exportNames.push(node.declaration.identifier.value);
101
- }
102
- }
103
- if ('ExportDefaultExpression' === node.type || 'ExportDefaultDeclaration' === node.type) exportNames.push('default');
104
- });
105
- return exportNames;
106
- };
107
- const checkDirective = async (ast, directive)=>{
108
- try {
109
- for(let i = 0; i < ast.body.length; i++){
110
- const node = ast.body[i];
111
- if ('ExpressionStatement' !== node.type) break;
112
- if ('StringLiteral' === node.expression.type && node.expression.value === directive) return true;
113
- }
114
- } catch (e) {
115
- utils_namespaceObject.logger.error(e);
116
- }
117
- return false;
118
- };
119
- const isServerModule = async (ast)=>checkDirective(ast, 'use server');
120
- const isClientModule = async (ast)=>checkDirective(ast, 'use client');
121
- function findRootIssuer(modulegraph, module) {
122
- const currentModule = module;
123
- const issuer = modulegraph.getIssuer(currentModule);
124
- if (!issuer) return currentModule;
125
- return findRootIssuer(modulegraph, issuer);
126
- }
127
- exports.MODERN_RSC_INFO = __webpack_exports__.MODERN_RSC_INFO;
128
- exports.findRootIssuer = __webpack_exports__.findRootIssuer;
129
- exports.getExportNames = __webpack_exports__.getExportNames;
130
- exports.getRscBuildInfo = __webpack_exports__.getRscBuildInfo;
131
- exports.isClientModule = __webpack_exports__.isClientModule;
132
- exports.isCssModule = __webpack_exports__.isCssModule;
133
- exports.isServerModule = __webpack_exports__.isServerModule;
134
- exports.parseSource = __webpack_exports__.parseSource;
135
- exports.removeRscBuildInfo = __webpack_exports__.removeRscBuildInfo;
136
- exports.rspackRscLayerName = __webpack_exports__.rspackRscLayerName;
137
- exports.setBuildInfo = __webpack_exports__.setBuildInfo;
138
- exports.setRscBuildInfo = __webpack_exports__.setRscBuildInfo;
139
- exports.sharedData = __webpack_exports__.sharedData;
140
- for(var __rspack_i in __webpack_exports__)if (-1 === [
141
- "MODERN_RSC_INFO",
142
- "findRootIssuer",
143
- "getExportNames",
144
- "getRscBuildInfo",
145
- "isClientModule",
146
- "isCssModule",
147
- "isServerModule",
148
- "parseSource",
149
- "removeRscBuildInfo",
150
- "rspackRscLayerName",
151
- "setBuildInfo",
152
- "setRscBuildInfo",
153
- "sharedData"
154
- ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
155
- Object.defineProperty(exports, '__esModule', {
156
- value: true
157
- });
@@ -1,169 +0,0 @@
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
- rsbuildRscPlugin: ()=>rsbuildRscPlugin
37
- });
38
- const external_node_path_namespaceObject = require("node:path");
39
- var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
40
- const fs_extra_namespaceObject = require("@modern-js/utils/fs-extra");
41
- var fs_extra_default = /*#__PURE__*/ __webpack_require__.n(fs_extra_namespaceObject);
42
- const core_namespaceObject = require("@rsbuild/core");
43
- const external_common_js_namespaceObject = require("../common.js");
44
- const external_rspack_rsc_client_plugin_js_namespaceObject = require("./rspack-rsc-client-plugin.js");
45
- const external_rspack_rsc_server_plugin_js_namespaceObject = require("./rspack-rsc-server-plugin.js");
46
- const CSS_RULE_NAMES = [
47
- 'less',
48
- 'css',
49
- 'scss',
50
- 'sass'
51
- ];
52
- const createVirtualModule = (content)=>`data:text/javascript,${encodeURIComponent(content)}`;
53
- const checkReactVersionAtLeast19 = async (appDir)=>{
54
- const packageJsonPath = external_node_path_default().resolve(appDir, 'package.json');
55
- const packageJson = await fs_extra_default().readJSON(packageJsonPath);
56
- if (!packageJson.dependencies) return false;
57
- const { dependencies } = packageJson;
58
- const reactVersion = dependencies.react;
59
- const reactDomVersion = dependencies['react-dom'];
60
- if (!reactVersion || !reactDomVersion) return false;
61
- const cleanVersion = (version)=>version.replace(/[\^~]/g, '');
62
- const reactVersionParts = cleanVersion(reactVersion).split('.');
63
- const reactDomVersionParts = cleanVersion(reactDomVersion).split('.');
64
- const reactMajor = parseInt(reactVersionParts[0], 10);
65
- const reactDomMajor = parseInt(reactDomVersionParts[0], 10);
66
- if (Number.isNaN(reactMajor) || Number.isNaN(reactDomMajor)) return false;
67
- return reactMajor >= 19 && reactDomMajor >= 19;
68
- };
69
- const rsbuildRscPlugin = ({ appDir, rscClientRuntimePath, rscServerRuntimePath, internalDirectory })=>({
70
- name: 'builder:rsc-rsbuild-plugin',
71
- setup (api) {
72
- api.modifyBundlerChain({
73
- handler: async (chain, { isServer, CHAIN_ID, isWebWorker })=>{
74
- if (!await checkReactVersionAtLeast19(appDir)) {
75
- core_namespaceObject.logger.error('Enable react server component, please make sure the react and react-dom versions are greater than or equal to 19.0.0');
76
- process.exit(1);
77
- }
78
- const entryPath2Name = new Map();
79
- for (const [name, entry] of Object.entries(chain.entryPoints.entries()))entry.values().forEach((value)=>{
80
- entryPath2Name.set(value, name);
81
- });
82
- const jsHandler = ()=>{
83
- const originalJsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
84
- if (!originalJsRule) throw new Error('Original JS rule not found when setup RSC plugin.');
85
- const originalJsMainRule = originalJsRule.oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN);
86
- if (!originalJsMainRule) throw new Error('Original JS main rule not found when setup RSC plugin.');
87
- const useBabel = originalJsMainRule.uses.has(CHAIN_ID.USE.BABEL);
88
- const jsLoader = useBabel ? CHAIN_ID.USE.BABEL : CHAIN_ID.USE.SWC;
89
- const jsLoaderOptions = originalJsMainRule.use(jsLoader).get('options');
90
- const jsLoaderPath = originalJsMainRule.use(jsLoader).get('loader');
91
- originalJsRule.oneOfs.delete(CHAIN_ID.ONE_OF.JS_MAIN);
92
- chain.module.rule(CHAIN_ID.RULE.JS).oneOf('rsc-server').issuerLayer(external_common_js_namespaceObject.rspackRscLayerName).exclude.add(/universal[/\\]async_storage/).end().use('rsc-server-loader').loader(require.resolve('../rsc-server-loader')).options({
93
- entryPath2Name,
94
- appDir,
95
- runtimePath: rscServerRuntimePath,
96
- internalDirectory
97
- }).end().use(jsLoader).loader(jsLoaderPath).options(jsLoaderOptions).end().end().oneOf('rsc-ssr').exclude.add(/universal[/\\]async_storage/).end().use('rsc-ssr-loader').loader(require.resolve('../rsc-ssr-loader')).options({
98
- entryPath2Name,
99
- internalDirectory
100
- }).end().use(jsLoader).loader(jsLoaderPath).options(jsLoaderOptions).end().end();
101
- };
102
- const layerHandler = ()=>{
103
- const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, '[/\\\\]')}[/\\\\][^/\\\\]*[/\\\\]routes`);
104
- chain.module.rule('server-module').resource([
105
- /render[/\\].*[/\\]server[/\\]rsc/,
106
- /AppProxy/,
107
- routesFileReg
108
- ]).layer(external_common_js_namespaceObject.rspackRscLayerName).end();
109
- chain.module.rule(external_common_js_namespaceObject.rspackRscLayerName).issuerLayer(external_common_js_namespaceObject.rspackRscLayerName).resolve.conditionNames.add(external_common_js_namespaceObject.rspackRscLayerName).add('...');
110
- chain.module.rule('rsc-common').resource([
111
- /universal[/\\]async_storage/
112
- ]).layer('rsc-common');
113
- };
114
- const flightCssHandler = ()=>{
115
- CSS_RULE_NAMES.forEach((ruleName)=>{
116
- const rule = chain.module.rules.get(ruleName);
117
- if (rule) chain.module.rule(ruleName).use('custom-loader').before('ignore-css').loader(require.resolve('../rsc-css-loader'));
118
- });
119
- };
120
- const addServerRscPlugin = ()=>{
121
- const ServerPlugin = external_rspack_rsc_server_plugin_js_namespaceObject.RscServerPlugin;
122
- chain.plugin('rsc-server-plugin').use(ServerPlugin, [
123
- {
124
- entryPath2Name
125
- }
126
- ]);
127
- };
128
- const addRscClientLoader = ()=>{
129
- chain.module.rule('js').use('rsc-client-loader').loader(require.resolve('../rsc-client-loader')).before('babel').options({
130
- callServerImport: rscClientRuntimePath,
131
- registerImport: rscClientRuntimePath
132
- }).end();
133
- };
134
- const addRscClientPlugin = ()=>{
135
- const ClientPlugin = external_rspack_rsc_client_plugin_js_namespaceObject.RspackRscClientPlugin;
136
- chain.plugin('rsc-client-plugin').use(ClientPlugin);
137
- };
138
- if (isServer) {
139
- chain.name('server');
140
- layerHandler();
141
- flightCssHandler();
142
- jsHandler();
143
- addServerRscPlugin();
144
- } else if (!isWebWorker) {
145
- chain.name('client');
146
- chain.dependencies([
147
- 'server'
148
- ]);
149
- const entries = chain.entryPoints.entries();
150
- for (const entryName of Object.keys(entries)){
151
- const entryPoint = chain.entry(entryName);
152
- const code = `window.__MODERN_JS_ENTRY_NAME="${entryName}";`;
153
- entryPoint.add(createVirtualModule(code));
154
- }
155
- addRscClientLoader();
156
- addRscClientPlugin();
157
- }
158
- },
159
- order: 'post'
160
- });
161
- }
162
- });
163
- exports.rsbuildRscPlugin = __webpack_exports__.rsbuildRscPlugin;
164
- for(var __rspack_i in __webpack_exports__)if (-1 === [
165
- "rsbuildRscPlugin"
166
- ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
167
- Object.defineProperty(exports, '__esModule', {
168
- value: true
169
- });
@@ -1,187 +0,0 @@
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
- RspackRscClientPlugin: ()=>RspackRscClientPlugin
37
- });
38
- const external_path_namespaceObject = require("path");
39
- var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
40
- const external_common_js_namespaceObject = require("../common.js");
41
- const hasExtension = (filePath)=>'' !== external_path_default().extname(filePath);
42
- class RspackRscClientPlugin {
43
- apply(compiler) {
44
- const { EntryPlugin, RuntimeGlobals, WebpackError, sources: { RawSource } } = compiler.rspack;
45
- const ssrManifest = {
46
- moduleMap: {},
47
- moduleLoading: null,
48
- styles: []
49
- };
50
- const getEntryModule = (compilation)=>{
51
- const entryModules = [];
52
- for (const [, entryValue] of compilation.entries.entries()){
53
- const entryDependency = entryValue.dependencies[0];
54
- if (!entryDependency) {
55
- compilation.errors.push(new WebpackError("Could not find an entry dependency."));
56
- continue;
57
- }
58
- const resolvedModule = compilation.moduleGraph.getModule(entryDependency);
59
- if (resolvedModule) entryModules.push(resolvedModule);
60
- }
61
- if (0 === entryModules.length) {
62
- compilation.errors.push(new WebpackError("Could not find any entries in the compilation."));
63
- return [];
64
- }
65
- return entryModules;
66
- };
67
- const addClientReferencesChunks = (compilation, callback)=>{
68
- const promises = [];
69
- [
70
- ...this.clientReferencesMap.keys()
71
- ].forEach((resourcePath)=>{
72
- const entries = compilation.entries.entries();
73
- for (const [entryName, entry] of entries){
74
- const runtimeName = entry.options.runtime || entryName;
75
- if (hasExtension(entryName)) continue;
76
- const dependency = EntryPlugin.createDependency(resourcePath);
77
- promises.push(new Promise((resolve, reject)=>{
78
- compilation.addInclude(compiler.context, dependency, {
79
- name: entryName
80
- }, (error, module)=>{
81
- if (error) reject(error);
82
- else {
83
- compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(runtimeName);
84
- this.dependencies.push(dependency);
85
- resolve(void 0);
86
- }
87
- });
88
- }));
89
- }
90
- });
91
- if (this.styles && this.styles.size > 0) for (const style of this.styles){
92
- const dependency = EntryPlugin.createDependency(style);
93
- promises.push(new Promise((resolve, reject)=>{
94
- compilation.addInclude(compiler.context, dependency, {
95
- name: void 0
96
- }, (error, module)=>{
97
- if (error) reject(error);
98
- else {
99
- compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(void 0);
100
- this.dependencies.push(dependency);
101
- resolve(void 0);
102
- }
103
- });
104
- }));
105
- }
106
- Promise.all(promises).then(()=>callback(null)).catch((error)=>callback(error));
107
- };
108
- compiler.hooks.finishMake.tapAsync(RspackRscClientPlugin.name, (compilation, callback)=>{
109
- const entryModules = getEntryModule(compilation);
110
- for (const entryModule of entryModules)if (entryModule) addClientReferencesChunks(compilation, callback);
111
- });
112
- compiler.hooks.thisCompilation.tap(RspackRscClientPlugin.name, (compilation)=>{
113
- this.styles = external_common_js_namespaceObject.sharedData.get('styles');
114
- this.clientReferencesMap = external_common_js_namespaceObject.sharedData.get('clientReferencesMap');
115
- compilation.hooks.additionalTreeRuntimeRequirements.tap(RspackRscClientPlugin.name, (_chunk, runtimeRequirements)=>{
116
- runtimeRequirements.add(RuntimeGlobals.ensureChunkHandlers);
117
- runtimeRequirements.add(RuntimeGlobals.ensureChunk);
118
- runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
119
- });
120
- compilation.hooks.processAssets.tap(RspackRscClientPlugin.name, ()=>{
121
- const clientManifest = {};
122
- const { chunkGraph, moduleGraph } = compilation;
123
- for (const dependency of this.dependencies){
124
- const module = moduleGraph.getModule(dependency);
125
- if (!module) continue;
126
- const resourcePath = module.nameForCondition();
127
- const clientReferences = resourcePath ? this.clientReferencesMap.get(resourcePath) : void 0;
128
- if (clientReferences) {
129
- const moduleId = chunkGraph.getModuleId(module);
130
- const ssrModuleMetaData = {};
131
- for (const { id, exportName, ssrId } of clientReferences){
132
- const clientExportName = exportName;
133
- const ssrExportName = exportName;
134
- const chunksSet = new Set();
135
- for (const chunk of chunkGraph.getModuleChunksIterable(module))chunksSet.add(chunk);
136
- for (const connection of moduleGraph.getOutgoingConnections(module))for (const chunk of chunkGraph.getModuleChunksIterable(connection.module))chunksSet.add(chunk);
137
- const chunks = [];
138
- const styles = [];
139
- for (const chunk of chunksSet)if (chunk.id) {
140
- for (const file of chunk.files)if (file.endsWith('.js')) chunks.push(chunk.id, file);
141
- }
142
- clientManifest[id] = {
143
- id: moduleId,
144
- name: clientExportName,
145
- chunks,
146
- styles
147
- };
148
- if (ssrId) ssrModuleMetaData[clientExportName] = {
149
- id: ssrId,
150
- name: ssrExportName,
151
- chunks: []
152
- };
153
- }
154
- ssrManifest.moduleMap[moduleId] = ssrModuleMetaData;
155
- }
156
- }
157
- compilation.emitAsset(this.clientManifestFilename, new RawSource(JSON.stringify(clientManifest, null, 2), false));
158
- const { crossOriginLoading, publicPath = "" } = compilation.outputOptions;
159
- ssrManifest.moduleLoading = {
160
- prefix: compilation.getPath(publicPath, {
161
- hash: compilation.hash ?? "XXXX"
162
- }),
163
- crossOrigin: crossOriginLoading ? "use-credentials" === crossOriginLoading ? crossOriginLoading : "" : void 0
164
- };
165
- if (this.styles && this.styles.size > 0) {
166
- const assets = compilation.getAssets();
167
- const cssAsset = assets.find((asset)=>asset.name.endsWith('.css'));
168
- if (cssAsset) ssrManifest.styles.push(cssAsset.name);
169
- }
170
- compilation.emitAsset(this.ssrManifestFilename, new RawSource(JSON.stringify(ssrManifest, null, 2), false));
171
- });
172
- });
173
- }
174
- constructor(options){
175
- this.clientReferencesMap = new Map();
176
- this.dependencies = [];
177
- this.clientManifestFilename = options?.clientManifestFilename || "react-client-manifest.json";
178
- this.ssrManifestFilename = options?.ssrManifestFilename || "react-ssr-manifest.json";
179
- }
180
- }
181
- exports.RspackRscClientPlugin = __webpack_exports__.RspackRscClientPlugin;
182
- for(var __rspack_i in __webpack_exports__)if (-1 === [
183
- "RspackRscClientPlugin"
184
- ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
185
- Object.defineProperty(exports, '__esModule', {
186
- value: true
187
- });