@modern-js/builder 3.0.0-alpha.0 → 3.0.0-alpha.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 (136) hide show
  1. package/dist/cjs/createBuilder.js +96 -0
  2. package/dist/cjs/index.js +59 -0
  3. package/dist/cjs/plugins/devtools.js +47 -0
  4. package/dist/cjs/plugins/emitRouteFile.js +75 -0
  5. package/dist/cjs/plugins/environmentDefaults.js +100 -0
  6. package/dist/cjs/plugins/globalVars.js +55 -0
  7. package/dist/cjs/plugins/htmlMinify.js +115 -0
  8. package/dist/cjs/plugins/manifest.js +52 -0
  9. package/dist/cjs/plugins/postcss.js +118 -0
  10. package/dist/cjs/plugins/runtimeChunk.js +54 -0
  11. package/dist/cjs/rsc/common.js +157 -0
  12. package/dist/cjs/rsc/plugins/rsbuild-rsc-plugin.js +169 -0
  13. package/dist/cjs/rsc/plugins/rspack-rsc-client-plugin.js +187 -0
  14. package/dist/cjs/rsc/plugins/rspack-rsc-server-plugin.js +245 -0
  15. package/dist/cjs/rsc/rsc-client-loader.js +71 -0
  16. package/dist/cjs/rsc/rsc-css-loader.js +42 -0
  17. package/dist/cjs/rsc/rsc-server-loader.js +102 -0
  18. package/dist/cjs/rsc/rsc-ssr-loader.js +60 -0
  19. package/dist/cjs/shared/devServer.js +87 -0
  20. package/dist/cjs/shared/getCssSupport.js +121 -0
  21. package/dist/cjs/shared/manifest.js +46 -0
  22. package/dist/cjs/shared/parseCommonConfig.js +209 -0
  23. package/dist/cjs/shared/rsc/rscClientBrowserFallback.js +64 -0
  24. package/dist/cjs/shared/rsc/rscEmptyModule.js +36 -0
  25. package/dist/cjs/shared/utils.js +115 -0
  26. package/dist/cjs/types.js +18 -0
  27. package/dist/compiled/postcss-load-config/index.d.ts +1 -0
  28. package/dist/compiled/postcss-load-config/index.js +608 -0
  29. package/dist/compiled/postcss-load-config/index.js.LICENSE.txt +13 -0
  30. package/dist/compiled/postcss-load-config/license +20 -0
  31. package/dist/compiled/postcss-load-config/package.json +1 -0
  32. package/dist/esm/createBuilder.mjs +59 -0
  33. package/dist/esm/index.mjs +4 -0
  34. package/dist/esm/plugins/devtools.mjs +13 -0
  35. package/dist/esm/plugins/emitRouteFile.mjs +28 -0
  36. package/dist/esm/plugins/environmentDefaults.mjs +66 -0
  37. package/dist/esm/plugins/globalVars.mjs +21 -0
  38. package/dist/esm/plugins/htmlMinify.mjs +81 -0
  39. package/dist/esm/plugins/manifest.mjs +18 -0
  40. package/dist/esm/plugins/postcss.mjs +81 -0
  41. package/dist/esm/plugins/runtimeChunk.mjs +20 -0
  42. package/dist/esm/rsc/common.mjs +87 -0
  43. package/dist/esm/rsc/plugins/rsbuild-rsc-plugin.mjs +124 -0
  44. package/dist/esm/rsc/plugins/rspack-rsc-client-plugin.mjs +143 -0
  45. package/dist/esm/rsc/plugins/rspack-rsc-server-plugin.mjs +211 -0
  46. package/dist/esm/rsc/rsc-client-loader.mjs +37 -0
  47. package/dist/esm/rsc/rsc-css-loader.mjs +8 -0
  48. package/dist/esm/rsc/rsc-server-loader.mjs +58 -0
  49. package/dist/esm/rsc/rsc-ssr-loader.mjs +26 -0
  50. package/dist/esm/shared/devServer.mjs +53 -0
  51. package/dist/esm/shared/getCssSupport.mjs +77 -0
  52. package/dist/esm/shared/manifest.mjs +12 -0
  53. package/dist/esm/shared/parseCommonConfig.mjs +172 -0
  54. package/dist/esm/shared/rsc/rscClientBrowserFallback.mjs +20 -0
  55. package/dist/esm/shared/rsc/rscEmptyModule.mjs +2 -0
  56. package/dist/esm/shared/utils.mjs +53 -0
  57. package/dist/esm/types.mjs +0 -0
  58. package/dist/esm-node/createBuilder.mjs +61 -0
  59. package/dist/esm-node/index.mjs +6 -0
  60. package/dist/esm-node/plugins/devtools.mjs +15 -0
  61. package/dist/esm-node/plugins/emitRouteFile.mjs +30 -0
  62. package/dist/esm-node/plugins/environmentDefaults.mjs +68 -0
  63. package/dist/esm-node/plugins/globalVars.mjs +23 -0
  64. package/dist/esm-node/plugins/htmlMinify.mjs +83 -0
  65. package/dist/esm-node/plugins/manifest.mjs +20 -0
  66. package/dist/esm-node/plugins/postcss.mjs +83 -0
  67. package/dist/esm-node/plugins/runtimeChunk.mjs +22 -0
  68. package/dist/esm-node/rsc/common.mjs +89 -0
  69. package/dist/esm-node/rsc/plugins/rsbuild-rsc-plugin.mjs +126 -0
  70. package/dist/esm-node/rsc/plugins/rspack-rsc-client-plugin.mjs +145 -0
  71. package/dist/esm-node/rsc/plugins/rspack-rsc-server-plugin.mjs +213 -0
  72. package/dist/esm-node/rsc/rsc-client-loader.mjs +39 -0
  73. package/dist/esm-node/rsc/rsc-css-loader.mjs +10 -0
  74. package/dist/esm-node/rsc/rsc-server-loader.mjs +60 -0
  75. package/dist/esm-node/rsc/rsc-ssr-loader.mjs +28 -0
  76. package/dist/esm-node/shared/devServer.mjs +55 -0
  77. package/dist/esm-node/shared/getCssSupport.mjs +79 -0
  78. package/dist/esm-node/shared/manifest.mjs +14 -0
  79. package/dist/esm-node/shared/parseCommonConfig.mjs +174 -0
  80. package/dist/esm-node/shared/rsc/rscClientBrowserFallback.mjs +25 -0
  81. package/dist/esm-node/shared/rsc/rscEmptyModule.mjs +4 -0
  82. package/dist/esm-node/shared/utils.mjs +55 -0
  83. package/dist/esm-node/types.mjs +2 -0
  84. package/dist/types/shared/rsc/rscClientBrowserFallback.d.ts +2 -0
  85. package/dist/types/shared/rsc/rscEmptyModule.d.ts +2 -0
  86. package/dist/{types.d.ts → types/types.d.ts} +2 -16
  87. package/package.json +36 -31
  88. package/dist/createBuilder.js +0 -153
  89. package/dist/index.js +0 -45
  90. package/dist/plugins/babel-post.d.ts +0 -5
  91. package/dist/plugins/babel-post.js +0 -57
  92. package/dist/plugins/devtools.js +0 -42
  93. package/dist/plugins/emitRouteFile.js +0 -70
  94. package/dist/plugins/environmentDefaults.js +0 -99
  95. package/dist/plugins/globalVars.js +0 -50
  96. package/dist/plugins/htmlMinify.js +0 -126
  97. package/dist/plugins/manifest.js +0 -54
  98. package/dist/plugins/postcss.js +0 -133
  99. package/dist/plugins/runtimeChunk.js +0 -55
  100. package/dist/rsc/common.js +0 -174
  101. package/dist/rsc/plugins/rsbuild-rsc-plugin.js +0 -166
  102. package/dist/rsc/plugins/rspack-rsc-client-plugin.js +0 -233
  103. package/dist/rsc/plugins/rspack-rsc-server-plugin.js +0 -290
  104. package/dist/rsc/rsc-client-loader.js +0 -70
  105. package/dist/rsc/rsc-css-loader.js +0 -30
  106. package/dist/rsc/rsc-server-loader.js +0 -95
  107. package/dist/rsc/rsc-ssr-loader.js +0 -58
  108. package/dist/shared/devServer.js +0 -83
  109. package/dist/shared/getCssSupport.js +0 -113
  110. package/dist/shared/manifest.js +0 -38
  111. package/dist/shared/parseCommonConfig.js +0 -232
  112. package/dist/shared/utils.js +0 -109
  113. package/dist/types.js +0 -16
  114. /package/dist/{createBuilder.d.ts → types/createBuilder.d.ts} +0 -0
  115. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  116. /package/dist/{plugins → types/plugins}/devtools.d.ts +0 -0
  117. /package/dist/{plugins → types/plugins}/emitRouteFile.d.ts +0 -0
  118. /package/dist/{plugins → types/plugins}/environmentDefaults.d.ts +0 -0
  119. /package/dist/{plugins → types/plugins}/globalVars.d.ts +0 -0
  120. /package/dist/{plugins → types/plugins}/htmlMinify.d.ts +0 -0
  121. /package/dist/{plugins → types/plugins}/manifest.d.ts +0 -0
  122. /package/dist/{plugins → types/plugins}/postcss.d.ts +0 -0
  123. /package/dist/{plugins → types/plugins}/runtimeChunk.d.ts +0 -0
  124. /package/dist/{rsc → types/rsc}/common.d.ts +0 -0
  125. /package/dist/{rsc → types/rsc}/plugins/rsbuild-rsc-plugin.d.ts +0 -0
  126. /package/dist/{rsc → types/rsc}/plugins/rspack-rsc-client-plugin.d.ts +0 -0
  127. /package/dist/{rsc → types/rsc}/plugins/rspack-rsc-server-plugin.d.ts +0 -0
  128. /package/dist/{rsc → types/rsc}/rsc-client-loader.d.ts +0 -0
  129. /package/dist/{rsc → types/rsc}/rsc-css-loader.d.ts +0 -0
  130. /package/dist/{rsc → types/rsc}/rsc-server-loader.d.ts +0 -0
  131. /package/dist/{rsc → types/rsc}/rsc-ssr-loader.d.ts +0 -0
  132. /package/dist/{shared → types/shared}/devServer.d.ts +0 -0
  133. /package/dist/{shared → types/shared}/getCssSupport.d.ts +0 -0
  134. /package/dist/{shared → types/shared}/manifest.d.ts +0 -0
  135. /package/dist/{shared → types/shared}/parseCommonConfig.d.ts +0 -0
  136. /package/dist/{shared → types/shared}/utils.d.ts +0 -0
@@ -0,0 +1,143 @@
1
+ import path from "path";
2
+ import { sharedData } from "../common.mjs";
3
+ const hasExtension = (filePath)=>'' !== path.extname(filePath);
4
+ class RspackRscClientPlugin {
5
+ apply(compiler) {
6
+ const { EntryPlugin, RuntimeGlobals, WebpackError, sources: { RawSource } } = compiler.rspack;
7
+ const ssrManifest = {
8
+ moduleMap: {},
9
+ moduleLoading: null,
10
+ styles: []
11
+ };
12
+ const getEntryModule = (compilation)=>{
13
+ const entryModules = [];
14
+ for (const [, entryValue] of compilation.entries.entries()){
15
+ const entryDependency = entryValue.dependencies[0];
16
+ if (!entryDependency) {
17
+ compilation.errors.push(new WebpackError("Could not find an entry dependency."));
18
+ continue;
19
+ }
20
+ const resolvedModule = compilation.moduleGraph.getModule(entryDependency);
21
+ if (resolvedModule) entryModules.push(resolvedModule);
22
+ }
23
+ if (0 === entryModules.length) {
24
+ compilation.errors.push(new WebpackError("Could not find any entries in the compilation."));
25
+ return [];
26
+ }
27
+ return entryModules;
28
+ };
29
+ const addClientReferencesChunks = (compilation, callback)=>{
30
+ const promises = [];
31
+ [
32
+ ...this.clientReferencesMap.keys()
33
+ ].forEach((resourcePath)=>{
34
+ const entries = compilation.entries.entries();
35
+ for (const [entryName, entry] of entries){
36
+ const runtimeName = entry.options.runtime || entryName;
37
+ if (hasExtension(entryName)) continue;
38
+ const dependency = EntryPlugin.createDependency(resourcePath);
39
+ promises.push(new Promise((resolve, reject)=>{
40
+ compilation.addInclude(compiler.context, dependency, {
41
+ name: entryName
42
+ }, (error, module)=>{
43
+ if (error) reject(error);
44
+ else {
45
+ compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(runtimeName);
46
+ this.dependencies.push(dependency);
47
+ resolve(void 0);
48
+ }
49
+ });
50
+ }));
51
+ }
52
+ });
53
+ if (this.styles && this.styles.size > 0) for (const style of this.styles){
54
+ const dependency = EntryPlugin.createDependency(style);
55
+ promises.push(new Promise((resolve, reject)=>{
56
+ compilation.addInclude(compiler.context, dependency, {
57
+ name: void 0
58
+ }, (error, module)=>{
59
+ if (error) reject(error);
60
+ else {
61
+ compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(void 0);
62
+ this.dependencies.push(dependency);
63
+ resolve(void 0);
64
+ }
65
+ });
66
+ }));
67
+ }
68
+ Promise.all(promises).then(()=>callback(null)).catch((error)=>callback(error));
69
+ };
70
+ compiler.hooks.finishMake.tapAsync(RspackRscClientPlugin.name, (compilation, callback)=>{
71
+ const entryModules = getEntryModule(compilation);
72
+ for (const entryModule of entryModules)if (entryModule) addClientReferencesChunks(compilation, callback);
73
+ });
74
+ compiler.hooks.thisCompilation.tap(RspackRscClientPlugin.name, (compilation)=>{
75
+ this.styles = sharedData.get('styles');
76
+ this.clientReferencesMap = sharedData.get('clientReferencesMap');
77
+ compilation.hooks.additionalTreeRuntimeRequirements.tap(RspackRscClientPlugin.name, (_chunk, runtimeRequirements)=>{
78
+ runtimeRequirements.add(RuntimeGlobals.ensureChunkHandlers);
79
+ runtimeRequirements.add(RuntimeGlobals.ensureChunk);
80
+ runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
81
+ });
82
+ compilation.hooks.processAssets.tap(RspackRscClientPlugin.name, ()=>{
83
+ const clientManifest = {};
84
+ const { chunkGraph, moduleGraph } = compilation;
85
+ for (const dependency of this.dependencies){
86
+ const module = moduleGraph.getModule(dependency);
87
+ if (!module) continue;
88
+ const resourcePath = module.nameForCondition();
89
+ const clientReferences = resourcePath ? this.clientReferencesMap.get(resourcePath) : void 0;
90
+ if (clientReferences) {
91
+ const moduleId = chunkGraph.getModuleId(module);
92
+ const ssrModuleMetaData = {};
93
+ for (const { id, exportName, ssrId } of clientReferences){
94
+ const clientExportName = exportName;
95
+ const ssrExportName = exportName;
96
+ const chunksSet = new Set();
97
+ for (const chunk of chunkGraph.getModuleChunksIterable(module))chunksSet.add(chunk);
98
+ for (const connection of moduleGraph.getOutgoingConnections(module))for (const chunk of chunkGraph.getModuleChunksIterable(connection.module))chunksSet.add(chunk);
99
+ const chunks = [];
100
+ const styles = [];
101
+ for (const chunk of chunksSet)if (chunk.id) {
102
+ for (const file of chunk.files)if (file.endsWith('.js')) chunks.push(chunk.id, file);
103
+ }
104
+ clientManifest[id] = {
105
+ id: moduleId,
106
+ name: clientExportName,
107
+ chunks,
108
+ styles
109
+ };
110
+ if (ssrId) ssrModuleMetaData[clientExportName] = {
111
+ id: ssrId,
112
+ name: ssrExportName,
113
+ chunks: []
114
+ };
115
+ }
116
+ ssrManifest.moduleMap[moduleId] = ssrModuleMetaData;
117
+ }
118
+ }
119
+ compilation.emitAsset(this.clientManifestFilename, new RawSource(JSON.stringify(clientManifest, null, 2), false));
120
+ const { crossOriginLoading, publicPath = "" } = compilation.outputOptions;
121
+ ssrManifest.moduleLoading = {
122
+ prefix: compilation.getPath(publicPath, {
123
+ hash: compilation.hash ?? "XXXX"
124
+ }),
125
+ crossOrigin: crossOriginLoading ? "use-credentials" === crossOriginLoading ? crossOriginLoading : "" : void 0
126
+ };
127
+ if (this.styles && this.styles.size > 0) {
128
+ const assets = compilation.getAssets();
129
+ const cssAsset = assets.find((asset)=>asset.name.endsWith('.css'));
130
+ if (cssAsset) ssrManifest.styles.push(cssAsset.name);
131
+ }
132
+ compilation.emitAsset(this.ssrManifestFilename, new RawSource(JSON.stringify(ssrManifest, null, 2), false));
133
+ });
134
+ });
135
+ }
136
+ constructor(options){
137
+ this.clientReferencesMap = new Map();
138
+ this.dependencies = [];
139
+ this.clientManifestFilename = options?.clientManifestFilename || "react-client-manifest.json";
140
+ this.ssrManifestFilename = options?.ssrManifestFilename || "react-ssr-manifest.json";
141
+ }
142
+ }
143
+ export { RspackRscClientPlugin };
@@ -0,0 +1,211 @@
1
+ import { findRootIssuer, getRscBuildInfo, isCssModule, rspackRscLayerName, setRscBuildInfo, sharedData } from "../common.mjs";
2
+ const rspack_rsc_server_plugin_resourcePath2Entries = new Map();
3
+ class RscServerPlugin {
4
+ isValidModule(module) {
5
+ return Boolean(module?.resource);
6
+ }
7
+ hasValidEntries(entries) {
8
+ return Boolean(entries && entries.length > 0);
9
+ }
10
+ getEntryNameFromIssuer(issuer) {
11
+ return issuer.resource ? this.entryPath2Name.get(issuer.resource) : void 0;
12
+ }
13
+ createEntryFromIssuer(issuer, entryName) {
14
+ return {
15
+ entryName,
16
+ entryPath: issuer.resource
17
+ };
18
+ }
19
+ buildModuleToEntriesMapping(compilation) {
20
+ this.moduleToEntries.clear();
21
+ for (const [entryName, entryDependency] of compilation.entries.entries()){
22
+ const entryModule = compilation.moduleGraph.getModule(entryDependency.dependencies[0]);
23
+ if (entryModule) this.traverseModulesFromEntry(entryModule, entryName, compilation.moduleGraph, new Set());
24
+ }
25
+ }
26
+ traverseModulesFromEntry(module, entryName, moduleGraph, visited) {
27
+ if (!module?.resource || visited.has(module.resource)) return;
28
+ visited.add(module.resource);
29
+ if (!this.moduleToEntries.has(module.resource)) this.moduleToEntries.set(module.resource, new Set());
30
+ this.moduleToEntries.get(module.resource).add(entryName);
31
+ for (const connection of moduleGraph.getOutgoingConnections(module))if (connection.module && 'resource' in connection.module) this.traverseModulesFromEntry(connection.module, entryName, moduleGraph, visited);
32
+ }
33
+ findModuleEntries(module, compilation, resourcePath2Entries, visited = new Set()) {
34
+ if (!this.isValidModule(module) || visited.has(module.resource)) return [];
35
+ visited.add(module.resource);
36
+ const currentEntries = resourcePath2Entries.get(module.resource);
37
+ if (this.hasValidEntries(currentEntries)) return currentEntries;
38
+ const entryNames = this.moduleToEntries.get(module.resource);
39
+ if (entryNames && entryNames.size > 0) {
40
+ const entries = [];
41
+ for (const entryName of entryNames){
42
+ const entryPath = this.getEntryPathByName(entryName, compilation);
43
+ if (entryPath) entries.push({
44
+ entryName,
45
+ entryPath
46
+ });
47
+ }
48
+ return entries;
49
+ }
50
+ const issuer = findRootIssuer(compilation.moduleGraph, module);
51
+ if (!issuer) return [];
52
+ const issuerEntries = this.findModuleEntries(issuer, compilation, resourcePath2Entries, visited);
53
+ if (issuerEntries.length > 0) return issuerEntries;
54
+ const entryName = this.getEntryNameFromIssuer(issuer);
55
+ if (entryName) return [
56
+ this.createEntryFromIssuer(issuer, entryName)
57
+ ];
58
+ return [];
59
+ }
60
+ getEntryPathByName(entryName, compilation) {
61
+ const entryDependency = compilation.entries.get(entryName);
62
+ if (entryDependency && entryDependency.dependencies.length > 0) {
63
+ const firstDep = entryDependency.dependencies[0];
64
+ if ('request' in firstDep && 'string' == typeof firstDep.request) return firstDep.request;
65
+ }
66
+ }
67
+ apply(compiler) {
68
+ const { EntryPlugin, WebpackError, sources: { RawSource } } = compiler.rspack;
69
+ const includeModule = async (compilation, resource, resourceEntryNames, layer)=>{
70
+ const entries = Array.from(compilation.entries.entries());
71
+ if (0 === entries.length) return void compilation.errors.push(new WebpackError("Could not find an entry in the compilation."));
72
+ const includePromises = entries.filter(([entryName])=>resourceEntryNames?.includes(entryName)).map(([entryName])=>{
73
+ const dependency = EntryPlugin.createDependency(resource);
74
+ return new Promise((resolve, reject)=>{
75
+ compilation.addInclude(compiler.context, dependency, {
76
+ name: entryName,
77
+ layer
78
+ }, (error, module)=>{
79
+ if (error) {
80
+ compilation.errors.push(error);
81
+ return reject(error);
82
+ }
83
+ if (!module) {
84
+ const noModuleError = new WebpackError("Module not added");
85
+ noModuleError.file = resource;
86
+ compilation.errors.push(noModuleError);
87
+ return reject(noModuleError);
88
+ }
89
+ setRscBuildInfo(module, {
90
+ __entryName: entryName
91
+ });
92
+ compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(entryName);
93
+ resolve();
94
+ });
95
+ });
96
+ });
97
+ await Promise.all(includePromises);
98
+ };
99
+ let needsAdditionalPass = false;
100
+ compiler.hooks.finishMake.tapPromise(RscServerPlugin.name, async (compilation)=>{
101
+ this.buildModuleToEntriesMapping(compilation);
102
+ const processModules = (modules)=>{
103
+ let hasChangeReference = false;
104
+ for (const module of modules){
105
+ if ('resource' in module && isCssModule(module)) this.styles.add(module.resource);
106
+ const buildInfo = getRscBuildInfo(module);
107
+ if (!buildInfo || !buildInfo.resourcePath) continue;
108
+ if (module.layer && 'server' === buildInfo.type) sharedData.set(buildInfo?.resourcePath, buildInfo);
109
+ if (!module.layer && 'client' === buildInfo.type) sharedData.set(buildInfo?.resourcePath, buildInfo);
110
+ const currentReference = buildInfo?.type === 'client' ? this.clientReferencesMap.get(buildInfo.resourcePath) : this.serverReferencesMap.get(buildInfo.resourcePath);
111
+ if (buildInfo?.type !== 'client' || currentReference) {
112
+ if (buildInfo?.type === 'server' && !currentReference) {
113
+ hasChangeReference = true;
114
+ this.serverReferencesMap.set(buildInfo.resourcePath, buildInfo.exportNames);
115
+ }
116
+ } else {
117
+ hasChangeReference = true;
118
+ this.clientReferencesMap.set(buildInfo.resourcePath, buildInfo.clientReferences);
119
+ }
120
+ const entries = this.findModuleEntries(module, compilation, rspack_rsc_server_plugin_resourcePath2Entries);
121
+ if (entries.length > 0) rspack_rsc_server_plugin_resourcePath2Entries.set(module.resource, entries);
122
+ }
123
+ return hasChangeReference;
124
+ };
125
+ this.serverManifest = {};
126
+ const clientReferences = [
127
+ ...this.clientReferencesMap.keys()
128
+ ];
129
+ const serverReferences = [
130
+ ...this.serverReferencesMap.keys()
131
+ ];
132
+ const referencesBefore = [
133
+ ...clientReferences,
134
+ ...serverReferences
135
+ ];
136
+ let hasChangeReference = false;
137
+ await Promise.all([
138
+ ...clientReferences.map(async (resource)=>{
139
+ try {
140
+ await includeModule(compilation, resource, rspack_rsc_server_plugin_resourcePath2Entries.get(resource)?.map((entry)=>entry.entryName) || []);
141
+ } catch (error) {
142
+ console.error(error);
143
+ hasChangeReference = true;
144
+ this.clientReferencesMap.delete(resource);
145
+ }
146
+ }),
147
+ ...serverReferences.map(async (resource)=>{
148
+ try {
149
+ await includeModule(compilation, resource, rspack_rsc_server_plugin_resourcePath2Entries.get(resource)?.map((entry)=>entry.entryName) || [], rspackRscLayerName);
150
+ } catch (error) {
151
+ console.error(error);
152
+ hasChangeReference = true;
153
+ this.serverReferencesMap.delete(resource);
154
+ }
155
+ })
156
+ ]);
157
+ hasChangeReference = processModules(compilation.modules);
158
+ const referencesAfter = [
159
+ ...this.clientReferencesMap.keys(),
160
+ ...this.serverReferencesMap.keys()
161
+ ];
162
+ if (referencesBefore.length !== referencesAfter.length || !referencesAfter.every((reference)=>referencesBefore.includes(reference)) && hasChangeReference) needsAdditionalPass = true;
163
+ });
164
+ compiler.hooks.done.tap(RscServerPlugin.name, ()=>{
165
+ sharedData.set('serverReferencesMap', this.serverReferencesMap);
166
+ sharedData.set('clientReferencesMap', this.clientReferencesMap);
167
+ sharedData.set('styles', this.styles);
168
+ });
169
+ compiler.hooks.afterCompile.tap(RscServerPlugin.name, (compilation)=>{
170
+ for (const module of compilation.modules){
171
+ const resource = module.nameForCondition();
172
+ if (!resource) continue;
173
+ const moduleId = compilation.chunkGraph.getModuleId(module);
174
+ if (null !== moduleId) {
175
+ if (module.layer !== rspackRscLayerName && this.clientReferencesMap.has(resource)) {
176
+ const clientReferences = this.clientReferencesMap.get(resource);
177
+ if (clientReferences) for (const clientReference of clientReferences)clientReference.ssrId = moduleId;
178
+ else compilation.errors.push(new WebpackError(`Could not find client references info in \`clientReferencesMap\` for ${resource}.`));
179
+ } else if (module.layer === rspackRscLayerName && getRscBuildInfo(module)?.type === 'server') {
180
+ const serverReferencesModuleInfo = getRscBuildInfo(module);
181
+ if (serverReferencesModuleInfo) {
182
+ serverReferencesModuleInfo.moduleId = moduleId;
183
+ for (const exportName of serverReferencesModuleInfo.exportNames)this.serverManifest[`${moduleId}#${exportName}`] = {
184
+ id: moduleId,
185
+ chunks: [],
186
+ name: exportName
187
+ };
188
+ } else compilation.errors.push(new WebpackError(`Could not find server references module info in \`serverReferencesMap\` for ${resource}.`));
189
+ }
190
+ }
191
+ }
192
+ });
193
+ compiler.hooks.thisCompilation.tap(RscServerPlugin.name, (compilation)=>{
194
+ compilation.hooks.needAdditionalPass.tap(RscServerPlugin.name, ()=>!(needsAdditionalPass = !needsAdditionalPass));
195
+ compilation.hooks.processAssets.tap(RscServerPlugin.name, ()=>{
196
+ compilation.emitAsset(this.serverManifestFilename, new RawSource(JSON.stringify(this.serverManifest, null, 2), false));
197
+ });
198
+ });
199
+ }
200
+ constructor(options){
201
+ this.clientReferencesMap = new Map();
202
+ this.serverReferencesMap = new Map();
203
+ this.serverManifest = {};
204
+ this.entryPath2Name = new Map();
205
+ this.moduleToEntries = new Map();
206
+ this.styles = new Set();
207
+ this.serverManifestFilename = options?.serverManifestFilename || "react-server-manifest.json";
208
+ this.entryPath2Name = options?.entryPath2Name || new Map();
209
+ }
210
+ }
211
+ export { RscServerPlugin };
@@ -0,0 +1,37 @@
1
+ import { isServerModule, parseSource, sharedData } from "./common.mjs";
2
+ async function rscClientLoader(source, sourceMap) {
3
+ this.cacheable(true);
4
+ const callback = this.async();
5
+ const ast = await parseSource(source);
6
+ const hasUseServerDirective = await isServerModule(ast);
7
+ if (!hasUseServerDirective) return void callback(null, source, sourceMap);
8
+ const { callServerImport = "@modern-js/runtime/rsc/client", registerImport = "@modern-js/runtime/rsc/client" } = this.getOptions();
9
+ const buildInfo = sharedData.get(this.resourcePath);
10
+ const moduleInfo = buildInfo ? {
11
+ moduleId: buildInfo?.moduleId,
12
+ exportNames: buildInfo?.exportNames
13
+ } : null;
14
+ if (!moduleInfo) {
15
+ this.emitError(new Error(`Could not find server module info in \`serverReferencesMap\` for ${this.resourcePath}.`));
16
+ callback(null, '');
17
+ return;
18
+ }
19
+ const { moduleId, exportNames } = moduleInfo;
20
+ if (!moduleId) {
21
+ this.emitError(new Error(`Could not find server module ID in \`serverReferencesMap\` for ${this.resourcePath}.`));
22
+ callback(null, '');
23
+ return;
24
+ }
25
+ if (!exportNames) return void callback(null, '');
26
+ const importsCode = `
27
+ import { createServerReference } from "${registerImport}";
28
+ import { callServer } from "${callServerImport}";
29
+ `;
30
+ const exportsCode = exportNames.map((item)=>{
31
+ const name = item;
32
+ if ('default' === name) return `export default createServerReference("${moduleId}", callServer);`;
33
+ return `export var ${name} = createServerReference("${moduleId}#${name}", callServer);`;
34
+ }).join('\n');
35
+ callback(null, `${importsCode}\n${exportsCode}`);
36
+ }
37
+ export { rscClientLoader as default };
@@ -0,0 +1,8 @@
1
+ import { setRscBuildInfo } from "./common.mjs";
2
+ function rscCssLoader(source) {
3
+ this._module && setRscBuildInfo(this._module, {
4
+ isCssModule: true
5
+ });
6
+ return source;
7
+ }
8
+ export { rscCssLoader as default };
@@ -0,0 +1,58 @@
1
+ import path from "path";
2
+ import { transform } from "@swc/core";
3
+ import { setRscBuildInfo } from "./common.mjs";
4
+ function extractMetadata(code) {
5
+ const metadataRegex = /\/\* @modern-js-rsc-metadata\n([\s\S]*?)\*\//;
6
+ const match = code.match(metadataRegex);
7
+ if (!match) return null;
8
+ try {
9
+ const metadata = JSON.parse(match[1]);
10
+ return metadata;
11
+ } catch (e) {
12
+ console.error('Failed to parse metadata:', e);
13
+ return null;
14
+ }
15
+ }
16
+ async function rscServerLoader(source) {
17
+ this.cacheable(true);
18
+ const callback = this.async();
19
+ const { appDir, runtimePath = '@modern-js/runtime/rsc/server' } = this.getOptions();
20
+ const result = await transform(source, {
21
+ filename: this.resourcePath,
22
+ jsc: {
23
+ target: 'es2020',
24
+ experimental: {
25
+ cacheRoot: path.resolve(appDir, 'node_modules/.swc'),
26
+ plugins: [
27
+ [
28
+ require.resolve('@modern-js/flight-server-transform-plugin'),
29
+ {
30
+ appDir: appDir,
31
+ runtimePath: runtimePath
32
+ }
33
+ ]
34
+ ]
35
+ }
36
+ },
37
+ isModule: true
38
+ });
39
+ const { code, map } = result;
40
+ const metadata = extractMetadata(code);
41
+ if (metadata?.directive && 'client' === metadata.directive) {
42
+ const { exportNames } = metadata;
43
+ if (exportNames.length > 0) setRscBuildInfo(this._module, {
44
+ type: 'client',
45
+ resourcePath: this.resourcePath,
46
+ clientReferences: exportNames
47
+ });
48
+ } else if (metadata) {
49
+ const { exportNames } = metadata;
50
+ if (exportNames.length > 0) setRscBuildInfo(this._module, {
51
+ type: 'server',
52
+ resourcePath: this.resourcePath,
53
+ exportNames: exportNames.map((item)=>item.exportName)
54
+ });
55
+ }
56
+ return callback(null, code, map);
57
+ }
58
+ export { rscServerLoader as default };
@@ -0,0 +1,26 @@
1
+ import { getExportNames, isServerModule, parseSource, setRscBuildInfo } from "./common.mjs";
2
+ async function rscSsrLoader(source, sourceMap) {
3
+ this.cacheable(true);
4
+ const callback = this.async();
5
+ const ast = await parseSource(source);
6
+ const hasDeclareServerDirective = await isServerModule(ast);
7
+ const resourcePath = this.resourcePath;
8
+ if (!hasDeclareServerDirective) return void callback(null, source, sourceMap);
9
+ const exportedNames = await getExportNames(ast, true);
10
+ const importsCode = `
11
+ 'use server';
12
+ `;
13
+ const exportsCode = exportedNames.map((name)=>{
14
+ if ('default' === name) return 'export default () => {throw new Error("Server actions must not be called during server-side rendering.")}';
15
+ return `export const ${name} = () => {
16
+ throw new Error("Server actions must not be called during server-side rendering.")
17
+ }`;
18
+ }).join('\n');
19
+ if (exportedNames.length > 0) setRscBuildInfo(this._module, {
20
+ type: 'server',
21
+ resourcePath,
22
+ exportNames: exportedNames
23
+ });
24
+ callback(null, `${importsCode}\n${exportsCode}`, sourceMap);
25
+ }
26
+ export { rscSsrLoader as default };
@@ -0,0 +1,53 @@
1
+ import { applyOptionsChain, isProd } from "@modern-js/utils";
2
+ import { merge } from "ts-deepmerge";
3
+ const defaultDevConfig = {
4
+ writeToDisk: (file)=>!file.includes('.hot-update.'),
5
+ hmr: true,
6
+ liveReload: true,
7
+ progressBar: true,
8
+ client: {
9
+ path: '/webpack-hmr',
10
+ overlay: false,
11
+ port: '<port>'
12
+ }
13
+ };
14
+ const transformToRsbuildServerOptions = (dev, devServer, server)=>{
15
+ const { host = '0.0.0.0', https, startUrl, beforeStartUrl, server: devServerConfig, ...devConfig } = dev;
16
+ const port = process.env.PORT ? Number(process.env.PORT) : server?.port ?? 8080;
17
+ const rsbuildDev = merge(defaultDevConfig, devConfig);
18
+ delete rsbuildDev.setupMiddlewares;
19
+ const legacyServerConfig = applyOptionsChain({}, devServer, {}, merge);
20
+ const serverCofig = {
21
+ compress: devServerConfig?.compress ?? legacyServerConfig.compress,
22
+ headers: devServerConfig?.headers ?? legacyServerConfig.headers,
23
+ historyApiFallback: devServerConfig?.historyApiFallback ?? legacyServerConfig.historyApiFallback,
24
+ proxy: devServerConfig?.proxy ?? legacyServerConfig.proxy
25
+ };
26
+ const rsbuildServer = isProd() ? {
27
+ publicDir: false,
28
+ htmlFallback: false,
29
+ printUrls: false
30
+ } : {
31
+ publicDir: false,
32
+ htmlFallback: false,
33
+ printUrls: false,
34
+ compress: serverCofig.compress,
35
+ headers: serverCofig.headers,
36
+ historyApiFallback: serverCofig.historyApiFallback,
37
+ proxy: serverCofig.proxy,
38
+ host,
39
+ port,
40
+ https: https ? https : void 0,
41
+ middlewareMode: true,
42
+ cors: server?.cors
43
+ };
44
+ if (!isProd() && startUrl) rsbuildServer.open = beforeStartUrl ? {
45
+ target: true === startUrl ? '//localhost:<port>' : startUrl,
46
+ before: beforeStartUrl
47
+ } : startUrl;
48
+ return {
49
+ rsbuildDev,
50
+ rsbuildServer
51
+ };
52
+ };
53
+ export { transformToRsbuildServerOptions };
@@ -0,0 +1,77 @@
1
+ import browserslist from "browserslist";
2
+ const CSS_FEATURES_BROWSER = {
3
+ customProperties: {
4
+ and_chr: '49',
5
+ and_ff: '31',
6
+ android: '50',
7
+ chrome: '49',
8
+ edge: '15',
9
+ firefox: '31',
10
+ ios_saf: '9.3',
11
+ op_mob: '36',
12
+ opera: '36',
13
+ safari: '9.1',
14
+ samsung: '5.0'
15
+ },
16
+ initial: {
17
+ and_chr: '37',
18
+ and_ff: '27',
19
+ android: '37',
20
+ chrome: '37',
21
+ edge: '79',
22
+ firefox: '27',
23
+ ios_saf: '9.3',
24
+ op_mob: '24',
25
+ opera: '24',
26
+ safari: '9.1',
27
+ samsung: '3.0'
28
+ },
29
+ pageBreak: {
30
+ and_chr: '51',
31
+ and_ff: '92',
32
+ android: '51',
33
+ chrome: '51',
34
+ edge: '12',
35
+ firefox: '92',
36
+ ios_saf: '10',
37
+ op_mob: '37',
38
+ opera: '11.1',
39
+ safari: '10',
40
+ samsung: '5.0'
41
+ },
42
+ fontVariant: {
43
+ and_chr: '18',
44
+ and_ff: '34',
45
+ android: '4.4',
46
+ chrome: '1',
47
+ edge: '12',
48
+ firefox: '34',
49
+ ios_saf: '9.3',
50
+ op_mob: '12',
51
+ opera: '10',
52
+ safari: '9.1',
53
+ samsung: '1.0'
54
+ },
55
+ mediaMinmax: {
56
+ and_chr: '104',
57
+ and_ff: '109',
58
+ android: '104',
59
+ chrome: '104',
60
+ edge: '104',
61
+ firefox: '63',
62
+ opera: '91'
63
+ }
64
+ };
65
+ const getCssFeatureBrowsers = (feature)=>{
66
+ const featureBrowsers = CSS_FEATURES_BROWSER[feature];
67
+ return browserslist(Object.entries(featureBrowsers).map(([key, value])=>`${key} >= ${value}`));
68
+ };
69
+ const isFeatureSupported = (projectBrowsers, featureBrowsers)=>projectBrowsers.every((item)=>featureBrowsers.includes(item));
70
+ function getCssSupport(projectBrowserslist) {
71
+ const projectBrowsers = browserslist(projectBrowserslist);
72
+ return Object.keys(CSS_FEATURES_BROWSER).reduce((acc, key)=>{
73
+ acc[key] = isFeatureSupported(projectBrowsers, getCssFeatureBrowsers(key));
74
+ return acc;
75
+ }, {});
76
+ }
77
+ export { getCssSupport };
@@ -0,0 +1,12 @@
1
+ const generateManifest = (seed, files, entries)=>{
2
+ const manifestFiles = files.reduce((manifest, file)=>{
3
+ manifest[file.name] = file.path;
4
+ return manifest;
5
+ }, seed);
6
+ const entrypointFiles = Object.keys(entries).reduce((previous, name)=>previous.concat(entries[name].filter((fileName)=>!fileName.endsWith('.map'))), []);
7
+ return {
8
+ files: manifestFiles,
9
+ entrypoints: entrypointFiles
10
+ };
11
+ };
12
+ export { generateManifest };