@modern-js/builder 3.0.1 → 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.
- package/dist/cjs/createBuilder.js +3 -8
- package/dist/cjs/plugins/environmentDefaults.js +4 -4
- package/dist/cjs/plugins/rscConfig.js +159 -0
- package/dist/cjs/{rsc/rsc-css-loader.js → shared/rsc/rsc-server-entry-loader.js} +6 -7
- package/dist/esm/createBuilder.mjs +3 -8
- package/dist/esm/plugins/environmentDefaults.mjs +4 -4
- package/dist/esm/plugins/rscConfig.mjs +112 -0
- package/dist/esm/shared/rsc/rsc-server-entry-loader.mjs +7 -0
- package/dist/esm-node/createBuilder.mjs +3 -8
- package/dist/esm-node/plugins/environmentDefaults.mjs +4 -4
- package/dist/esm-node/plugins/rscConfig.mjs +117 -0
- package/dist/esm-node/shared/rsc/rsc-server-entry-loader.mjs +8 -0
- package/dist/types/plugins/rscConfig.d.ts +18 -0
- package/dist/types/shared/rsc/rsc-server-entry-loader.d.ts +5 -0
- package/package.json +7 -7
- package/dist/cjs/rsc/common.js +0 -157
- package/dist/cjs/rsc/plugins/rsbuild-rsc-plugin.js +0 -169
- package/dist/cjs/rsc/plugins/rspack-rsc-client-plugin.js +0 -187
- package/dist/cjs/rsc/plugins/rspack-rsc-server-plugin.js +0 -245
- package/dist/cjs/rsc/rsc-client-loader.js +0 -71
- package/dist/cjs/rsc/rsc-server-loader.js +0 -102
- package/dist/cjs/rsc/rsc-ssr-loader.js +0 -60
- package/dist/esm/rsc/common.mjs +0 -87
- package/dist/esm/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -124
- package/dist/esm/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -143
- package/dist/esm/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -211
- package/dist/esm/rsc/rsc-client-loader.mjs +0 -37
- package/dist/esm/rsc/rsc-css-loader.mjs +0 -8
- package/dist/esm/rsc/rsc-server-loader.mjs +0 -58
- package/dist/esm/rsc/rsc-ssr-loader.mjs +0 -26
- package/dist/esm-node/rsc/common.mjs +0 -88
- package/dist/esm-node/rsc/plugins/rsbuild-rsc-plugin.mjs +0 -126
- package/dist/esm-node/rsc/plugins/rspack-rsc-client-plugin.mjs +0 -144
- package/dist/esm-node/rsc/plugins/rspack-rsc-server-plugin.mjs +0 -212
- package/dist/esm-node/rsc/rsc-client-loader.mjs +0 -38
- package/dist/esm-node/rsc/rsc-css-loader.mjs +0 -9
- package/dist/esm-node/rsc/rsc-server-loader.mjs +0 -60
- package/dist/esm-node/rsc/rsc-ssr-loader.mjs +0 -27
- package/dist/types/rsc/common.d.ts +0 -22
- package/dist/types/rsc/plugins/rsbuild-rsc-plugin.d.ts +0 -7
- package/dist/types/rsc/plugins/rspack-rsc-client-plugin.d.ts +0 -14
- package/dist/types/rsc/plugins/rspack-rsc-server-plugin.d.ts +0 -32
- package/dist/types/rsc/rsc-client-loader.d.ts +0 -7
- package/dist/types/rsc/rsc-css-loader.d.ts +0 -2
- package/dist/types/rsc/rsc-server-loader.d.ts +0 -6
- package/dist/types/rsc/rsc-ssr-loader.d.ts +0 -6
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { logger } from "@modern-js/utils";
|
|
3
|
-
import { parse } from "@swc/core";
|
|
4
|
-
const rspackRscLayerName = "react-server";
|
|
5
|
-
const MODERN_RSC_INFO = 'modernRscInfo';
|
|
6
|
-
const sharedData = {
|
|
7
|
-
store: new Map(),
|
|
8
|
-
get (key) {
|
|
9
|
-
return this.store.get(key);
|
|
10
|
-
},
|
|
11
|
-
set (key, value) {
|
|
12
|
-
this.store.set(key, value);
|
|
13
|
-
},
|
|
14
|
-
clear () {
|
|
15
|
-
this.store.clear();
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
function setBuildInfo(mod, property) {
|
|
19
|
-
if (!mod.buildInfo) mod.buildInfo = {};
|
|
20
|
-
Object.assign(mod.buildInfo, property);
|
|
21
|
-
}
|
|
22
|
-
function setRscBuildInfo(mod, property) {
|
|
23
|
-
if (!mod.buildInfo) mod.buildInfo = {};
|
|
24
|
-
const rscBuildInfo = mod.buildInfo[MODERN_RSC_INFO] || {};
|
|
25
|
-
Object.assign(rscBuildInfo, property);
|
|
26
|
-
setBuildInfo(mod, {
|
|
27
|
-
[MODERN_RSC_INFO]: rscBuildInfo
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
function removeRscBuildInfo(mod) {
|
|
31
|
-
delete mod.buildInfo?.[MODERN_RSC_INFO];
|
|
32
|
-
}
|
|
33
|
-
function getRscBuildInfo(mod) {
|
|
34
|
-
return mod.buildInfo?.[MODERN_RSC_INFO];
|
|
35
|
-
}
|
|
36
|
-
function isCssModule(mod) {
|
|
37
|
-
if (!mod) return false;
|
|
38
|
-
return getRscBuildInfo(mod)?.isCssModule;
|
|
39
|
-
}
|
|
40
|
-
const parseSource = async (source)=>await parse(source, {
|
|
41
|
-
syntax: "typescript",
|
|
42
|
-
tsx: true,
|
|
43
|
-
dynamicImport: true
|
|
44
|
-
});
|
|
45
|
-
const getExportNames = async (ast, collectFuncOnly = false)=>{
|
|
46
|
-
const exportNames = [];
|
|
47
|
-
ast.body.forEach((node)=>{
|
|
48
|
-
if ('ExportNamedDeclaration' === node.type) {
|
|
49
|
-
const namedExport = node;
|
|
50
|
-
namedExport.specifiers.forEach((specifier)=>{
|
|
51
|
-
if ('ExportSpecifier' === specifier.type) exportNames.push(specifier.exported?.value || specifier.orig.value);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
if ('ExportDeclaration' === node.type) {
|
|
55
|
-
if ('VariableDeclaration' === node.declaration.type) node.declaration.declarations.forEach((decl)=>{
|
|
56
|
-
if ('Identifier' === decl.id.type) if (collectFuncOnly) {
|
|
57
|
-
if (decl.init?.type === 'FunctionExpression' || decl.init?.type === 'ArrowFunctionExpression') exportNames.push(decl.id.value);
|
|
58
|
-
} else exportNames.push(decl.id.value);
|
|
59
|
-
});
|
|
60
|
-
if ('ClassDeclaration' === node.declaration.type || 'FunctionDeclaration' === node.declaration.type) {
|
|
61
|
-
if (node.declaration.identifier) exportNames.push(node.declaration.identifier.value);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if ('ExportDefaultExpression' === node.type || 'ExportDefaultDeclaration' === node.type) exportNames.push('default');
|
|
65
|
-
});
|
|
66
|
-
return exportNames;
|
|
67
|
-
};
|
|
68
|
-
const checkDirective = async (ast, directive)=>{
|
|
69
|
-
try {
|
|
70
|
-
for(let i = 0; i < ast.body.length; i++){
|
|
71
|
-
const node = ast.body[i];
|
|
72
|
-
if ('ExpressionStatement' !== node.type) break;
|
|
73
|
-
if ('StringLiteral' === node.expression.type && node.expression.value === directive) return true;
|
|
74
|
-
}
|
|
75
|
-
} catch (e) {
|
|
76
|
-
logger.error(e);
|
|
77
|
-
}
|
|
78
|
-
return false;
|
|
79
|
-
};
|
|
80
|
-
const isServerModule = async (ast)=>checkDirective(ast, 'use server');
|
|
81
|
-
const isClientModule = async (ast)=>checkDirective(ast, 'use client');
|
|
82
|
-
function findRootIssuer(modulegraph, module) {
|
|
83
|
-
const currentModule = module;
|
|
84
|
-
const issuer = modulegraph.getIssuer(currentModule);
|
|
85
|
-
if (!issuer) return currentModule;
|
|
86
|
-
return findRootIssuer(modulegraph, issuer);
|
|
87
|
-
}
|
|
88
|
-
export { MODERN_RSC_INFO, findRootIssuer, getExportNames, getRscBuildInfo, isClientModule, isCssModule, isServerModule, parseSource, removeRscBuildInfo, rspackRscLayerName, setBuildInfo, setRscBuildInfo, sharedData };
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import __rslib_shim_module__ from "node:module";
|
|
2
|
-
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import node_path from "node:path";
|
|
4
|
-
import fs_extra from "@modern-js/utils/fs-extra";
|
|
5
|
-
import { logger } from "@rsbuild/core";
|
|
6
|
-
import { rspackRscLayerName } from "../common.mjs";
|
|
7
|
-
import { RspackRscClientPlugin } from "./rspack-rsc-client-plugin.mjs";
|
|
8
|
-
import { RscServerPlugin } from "./rspack-rsc-server-plugin.mjs";
|
|
9
|
-
const CSS_RULE_NAMES = [
|
|
10
|
-
'less',
|
|
11
|
-
'css',
|
|
12
|
-
'scss',
|
|
13
|
-
'sass'
|
|
14
|
-
];
|
|
15
|
-
const createVirtualModule = (content)=>`data:text/javascript,${encodeURIComponent(content)}`;
|
|
16
|
-
const checkReactVersionAtLeast19 = async (appDir)=>{
|
|
17
|
-
const packageJsonPath = node_path.resolve(appDir, 'package.json');
|
|
18
|
-
const packageJson = await fs_extra.readJSON(packageJsonPath);
|
|
19
|
-
if (!packageJson.dependencies) return false;
|
|
20
|
-
const { dependencies } = packageJson;
|
|
21
|
-
const reactVersion = dependencies.react;
|
|
22
|
-
const reactDomVersion = dependencies['react-dom'];
|
|
23
|
-
if (!reactVersion || !reactDomVersion) return false;
|
|
24
|
-
const cleanVersion = (version)=>version.replace(/[\^~]/g, '');
|
|
25
|
-
const reactVersionParts = cleanVersion(reactVersion).split('.');
|
|
26
|
-
const reactDomVersionParts = cleanVersion(reactDomVersion).split('.');
|
|
27
|
-
const reactMajor = parseInt(reactVersionParts[0], 10);
|
|
28
|
-
const reactDomMajor = parseInt(reactDomVersionParts[0], 10);
|
|
29
|
-
if (Number.isNaN(reactMajor) || Number.isNaN(reactDomMajor)) return false;
|
|
30
|
-
return reactMajor >= 19 && reactDomMajor >= 19;
|
|
31
|
-
};
|
|
32
|
-
const rsbuildRscPlugin = ({ appDir, rscClientRuntimePath, rscServerRuntimePath, internalDirectory })=>({
|
|
33
|
-
name: 'builder:rsc-rsbuild-plugin',
|
|
34
|
-
setup (api) {
|
|
35
|
-
api.modifyBundlerChain({
|
|
36
|
-
handler: async (chain, { isServer, CHAIN_ID, isWebWorker })=>{
|
|
37
|
-
if (!await checkReactVersionAtLeast19(appDir)) {
|
|
38
|
-
logger.error('Enable react server component, please make sure the react and react-dom versions are greater than or equal to 19.0.0');
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
const entryPath2Name = new Map();
|
|
42
|
-
for (const [name, entry] of Object.entries(chain.entryPoints.entries()))entry.values().forEach((value)=>{
|
|
43
|
-
entryPath2Name.set(value, name);
|
|
44
|
-
});
|
|
45
|
-
const jsHandler = ()=>{
|
|
46
|
-
const originalJsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
|
|
47
|
-
if (!originalJsRule) throw new Error('Original JS rule not found when setup RSC plugin.');
|
|
48
|
-
const originalJsMainRule = originalJsRule.oneOfs.get(CHAIN_ID.ONE_OF.JS_MAIN);
|
|
49
|
-
if (!originalJsMainRule) throw new Error('Original JS main rule not found when setup RSC plugin.');
|
|
50
|
-
const useBabel = originalJsMainRule.uses.has(CHAIN_ID.USE.BABEL);
|
|
51
|
-
const jsLoader = useBabel ? CHAIN_ID.USE.BABEL : CHAIN_ID.USE.SWC;
|
|
52
|
-
const jsLoaderOptions = originalJsMainRule.use(jsLoader).get('options');
|
|
53
|
-
const jsLoaderPath = originalJsMainRule.use(jsLoader).get('loader');
|
|
54
|
-
originalJsRule.oneOfs.delete(CHAIN_ID.ONE_OF.JS_MAIN);
|
|
55
|
-
chain.module.rule(CHAIN_ID.RULE.JS).oneOf('rsc-server').issuerLayer(rspackRscLayerName).exclude.add(/universal[/\\]async_storage/).end().use('rsc-server-loader').loader(require.resolve('../rsc-server-loader')).options({
|
|
56
|
-
entryPath2Name,
|
|
57
|
-
appDir,
|
|
58
|
-
runtimePath: rscServerRuntimePath,
|
|
59
|
-
internalDirectory
|
|
60
|
-
}).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({
|
|
61
|
-
entryPath2Name,
|
|
62
|
-
internalDirectory
|
|
63
|
-
}).end().use(jsLoader).loader(jsLoaderPath).options(jsLoaderOptions).end().end();
|
|
64
|
-
};
|
|
65
|
-
const layerHandler = ()=>{
|
|
66
|
-
const routesFileReg = new RegExp(`${internalDirectory.replace(/[/\\]/g, '[/\\\\]')}[/\\\\][^/\\\\]*[/\\\\]routes`);
|
|
67
|
-
chain.module.rule('server-module').resource([
|
|
68
|
-
/render[/\\].*[/\\]server[/\\]rsc/,
|
|
69
|
-
/AppProxy/,
|
|
70
|
-
routesFileReg
|
|
71
|
-
]).layer(rspackRscLayerName).end();
|
|
72
|
-
chain.module.rule(rspackRscLayerName).issuerLayer(rspackRscLayerName).resolve.conditionNames.add(rspackRscLayerName).add('...');
|
|
73
|
-
chain.module.rule('rsc-common').resource([
|
|
74
|
-
/universal[/\\]async_storage/
|
|
75
|
-
]).layer('rsc-common');
|
|
76
|
-
};
|
|
77
|
-
const flightCssHandler = ()=>{
|
|
78
|
-
CSS_RULE_NAMES.forEach((ruleName)=>{
|
|
79
|
-
const rule = chain.module.rules.get(ruleName);
|
|
80
|
-
if (rule) chain.module.rule(ruleName).use('custom-loader').before('ignore-css').loader(require.resolve('../rsc-css-loader'));
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
const addServerRscPlugin = ()=>{
|
|
84
|
-
const ServerPlugin = RscServerPlugin;
|
|
85
|
-
chain.plugin('rsc-server-plugin').use(ServerPlugin, [
|
|
86
|
-
{
|
|
87
|
-
entryPath2Name
|
|
88
|
-
}
|
|
89
|
-
]);
|
|
90
|
-
};
|
|
91
|
-
const addRscClientLoader = ()=>{
|
|
92
|
-
chain.module.rule('js').use('rsc-client-loader').loader(require.resolve('../rsc-client-loader')).before('babel').options({
|
|
93
|
-
callServerImport: rscClientRuntimePath,
|
|
94
|
-
registerImport: rscClientRuntimePath
|
|
95
|
-
}).end();
|
|
96
|
-
};
|
|
97
|
-
const addRscClientPlugin = ()=>{
|
|
98
|
-
const ClientPlugin = RspackRscClientPlugin;
|
|
99
|
-
chain.plugin('rsc-client-plugin').use(ClientPlugin);
|
|
100
|
-
};
|
|
101
|
-
if (isServer) {
|
|
102
|
-
chain.name('server');
|
|
103
|
-
layerHandler();
|
|
104
|
-
flightCssHandler();
|
|
105
|
-
jsHandler();
|
|
106
|
-
addServerRscPlugin();
|
|
107
|
-
} else if (!isWebWorker) {
|
|
108
|
-
chain.name('client');
|
|
109
|
-
chain.dependencies([
|
|
110
|
-
'server'
|
|
111
|
-
]);
|
|
112
|
-
const entries = chain.entryPoints.entries();
|
|
113
|
-
for (const entryName of Object.keys(entries)){
|
|
114
|
-
const entryPoint = chain.entry(entryName);
|
|
115
|
-
const code = `window.__MODERN_JS_ENTRY_NAME="${entryName}";`;
|
|
116
|
-
entryPoint.add(createVirtualModule(code));
|
|
117
|
-
}
|
|
118
|
-
addRscClientLoader();
|
|
119
|
-
addRscClientPlugin();
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
order: 'post'
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
export { rsbuildRscPlugin };
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { sharedData } from "../common.mjs";
|
|
4
|
-
const hasExtension = (filePath)=>'' !== path.extname(filePath);
|
|
5
|
-
class RspackRscClientPlugin {
|
|
6
|
-
apply(compiler) {
|
|
7
|
-
const { EntryPlugin, RuntimeGlobals, WebpackError, sources: { RawSource } } = compiler.rspack;
|
|
8
|
-
const ssrManifest = {
|
|
9
|
-
moduleMap: {},
|
|
10
|
-
moduleLoading: null,
|
|
11
|
-
styles: []
|
|
12
|
-
};
|
|
13
|
-
const getEntryModule = (compilation)=>{
|
|
14
|
-
const entryModules = [];
|
|
15
|
-
for (const [, entryValue] of compilation.entries.entries()){
|
|
16
|
-
const entryDependency = entryValue.dependencies[0];
|
|
17
|
-
if (!entryDependency) {
|
|
18
|
-
compilation.errors.push(new WebpackError("Could not find an entry dependency."));
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
const resolvedModule = compilation.moduleGraph.getModule(entryDependency);
|
|
22
|
-
if (resolvedModule) entryModules.push(resolvedModule);
|
|
23
|
-
}
|
|
24
|
-
if (0 === entryModules.length) {
|
|
25
|
-
compilation.errors.push(new WebpackError("Could not find any entries in the compilation."));
|
|
26
|
-
return [];
|
|
27
|
-
}
|
|
28
|
-
return entryModules;
|
|
29
|
-
};
|
|
30
|
-
const addClientReferencesChunks = (compilation, callback)=>{
|
|
31
|
-
const promises = [];
|
|
32
|
-
[
|
|
33
|
-
...this.clientReferencesMap.keys()
|
|
34
|
-
].forEach((resourcePath)=>{
|
|
35
|
-
const entries = compilation.entries.entries();
|
|
36
|
-
for (const [entryName, entry] of entries){
|
|
37
|
-
const runtimeName = entry.options.runtime || entryName;
|
|
38
|
-
if (hasExtension(entryName)) continue;
|
|
39
|
-
const dependency = EntryPlugin.createDependency(resourcePath);
|
|
40
|
-
promises.push(new Promise((resolve, reject)=>{
|
|
41
|
-
compilation.addInclude(compiler.context, dependency, {
|
|
42
|
-
name: entryName
|
|
43
|
-
}, (error, module)=>{
|
|
44
|
-
if (error) reject(error);
|
|
45
|
-
else {
|
|
46
|
-
compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(runtimeName);
|
|
47
|
-
this.dependencies.push(dependency);
|
|
48
|
-
resolve(void 0);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}));
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
if (this.styles && this.styles.size > 0) for (const style of this.styles){
|
|
55
|
-
const dependency = EntryPlugin.createDependency(style);
|
|
56
|
-
promises.push(new Promise((resolve, reject)=>{
|
|
57
|
-
compilation.addInclude(compiler.context, dependency, {
|
|
58
|
-
name: void 0
|
|
59
|
-
}, (error, module)=>{
|
|
60
|
-
if (error) reject(error);
|
|
61
|
-
else {
|
|
62
|
-
compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(void 0);
|
|
63
|
-
this.dependencies.push(dependency);
|
|
64
|
-
resolve(void 0);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}));
|
|
68
|
-
}
|
|
69
|
-
Promise.all(promises).then(()=>callback(null)).catch((error)=>callback(error));
|
|
70
|
-
};
|
|
71
|
-
compiler.hooks.finishMake.tapAsync(RspackRscClientPlugin.name, (compilation, callback)=>{
|
|
72
|
-
const entryModules = getEntryModule(compilation);
|
|
73
|
-
for (const entryModule of entryModules)if (entryModule) addClientReferencesChunks(compilation, callback);
|
|
74
|
-
});
|
|
75
|
-
compiler.hooks.thisCompilation.tap(RspackRscClientPlugin.name, (compilation)=>{
|
|
76
|
-
this.styles = sharedData.get('styles');
|
|
77
|
-
this.clientReferencesMap = sharedData.get('clientReferencesMap');
|
|
78
|
-
compilation.hooks.additionalTreeRuntimeRequirements.tap(RspackRscClientPlugin.name, (_chunk, runtimeRequirements)=>{
|
|
79
|
-
runtimeRequirements.add(RuntimeGlobals.ensureChunkHandlers);
|
|
80
|
-
runtimeRequirements.add(RuntimeGlobals.ensureChunk);
|
|
81
|
-
runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
|
|
82
|
-
});
|
|
83
|
-
compilation.hooks.processAssets.tap(RspackRscClientPlugin.name, ()=>{
|
|
84
|
-
const clientManifest = {};
|
|
85
|
-
const { chunkGraph, moduleGraph } = compilation;
|
|
86
|
-
for (const dependency of this.dependencies){
|
|
87
|
-
const module = moduleGraph.getModule(dependency);
|
|
88
|
-
if (!module) continue;
|
|
89
|
-
const resourcePath = module.nameForCondition();
|
|
90
|
-
const clientReferences = resourcePath ? this.clientReferencesMap.get(resourcePath) : void 0;
|
|
91
|
-
if (clientReferences) {
|
|
92
|
-
const moduleId = chunkGraph.getModuleId(module);
|
|
93
|
-
const ssrModuleMetaData = {};
|
|
94
|
-
for (const { id, exportName, ssrId } of clientReferences){
|
|
95
|
-
const clientExportName = exportName;
|
|
96
|
-
const ssrExportName = exportName;
|
|
97
|
-
const chunksSet = new Set();
|
|
98
|
-
for (const chunk of chunkGraph.getModuleChunksIterable(module))chunksSet.add(chunk);
|
|
99
|
-
for (const connection of moduleGraph.getOutgoingConnections(module))for (const chunk of chunkGraph.getModuleChunksIterable(connection.module))chunksSet.add(chunk);
|
|
100
|
-
const chunks = [];
|
|
101
|
-
const styles = [];
|
|
102
|
-
for (const chunk of chunksSet)if (chunk.id) {
|
|
103
|
-
for (const file of chunk.files)if (file.endsWith('.js')) chunks.push(chunk.id, file);
|
|
104
|
-
}
|
|
105
|
-
clientManifest[id] = {
|
|
106
|
-
id: moduleId,
|
|
107
|
-
name: clientExportName,
|
|
108
|
-
chunks,
|
|
109
|
-
styles
|
|
110
|
-
};
|
|
111
|
-
if (ssrId) ssrModuleMetaData[clientExportName] = {
|
|
112
|
-
id: ssrId,
|
|
113
|
-
name: ssrExportName,
|
|
114
|
-
chunks: []
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
ssrManifest.moduleMap[moduleId] = ssrModuleMetaData;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
compilation.emitAsset(this.clientManifestFilename, new RawSource(JSON.stringify(clientManifest, null, 2), false));
|
|
121
|
-
const { crossOriginLoading, publicPath = "" } = compilation.outputOptions;
|
|
122
|
-
ssrManifest.moduleLoading = {
|
|
123
|
-
prefix: compilation.getPath(publicPath, {
|
|
124
|
-
hash: compilation.hash ?? "XXXX"
|
|
125
|
-
}),
|
|
126
|
-
crossOrigin: crossOriginLoading ? "use-credentials" === crossOriginLoading ? crossOriginLoading : "" : void 0
|
|
127
|
-
};
|
|
128
|
-
if (this.styles && this.styles.size > 0) {
|
|
129
|
-
const assets = compilation.getAssets();
|
|
130
|
-
const cssAsset = assets.find((asset)=>asset.name.endsWith('.css'));
|
|
131
|
-
if (cssAsset) ssrManifest.styles.push(cssAsset.name);
|
|
132
|
-
}
|
|
133
|
-
compilation.emitAsset(this.ssrManifestFilename, new RawSource(JSON.stringify(ssrManifest, null, 2), false));
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
constructor(options){
|
|
138
|
-
this.clientReferencesMap = new Map();
|
|
139
|
-
this.dependencies = [];
|
|
140
|
-
this.clientManifestFilename = options?.clientManifestFilename || "react-client-manifest.json";
|
|
141
|
-
this.ssrManifestFilename = options?.ssrManifestFilename || "react-ssr-manifest.json";
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
export { RspackRscClientPlugin };
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { findRootIssuer, getRscBuildInfo, isCssModule, rspackRscLayerName, setRscBuildInfo, sharedData } from "../common.mjs";
|
|
3
|
-
const rspack_rsc_server_plugin_resourcePath2Entries = new Map();
|
|
4
|
-
class RscServerPlugin {
|
|
5
|
-
isValidModule(module) {
|
|
6
|
-
return Boolean(module?.resource);
|
|
7
|
-
}
|
|
8
|
-
hasValidEntries(entries) {
|
|
9
|
-
return Boolean(entries && entries.length > 0);
|
|
10
|
-
}
|
|
11
|
-
getEntryNameFromIssuer(issuer) {
|
|
12
|
-
return issuer.resource ? this.entryPath2Name.get(issuer.resource) : void 0;
|
|
13
|
-
}
|
|
14
|
-
createEntryFromIssuer(issuer, entryName) {
|
|
15
|
-
return {
|
|
16
|
-
entryName,
|
|
17
|
-
entryPath: issuer.resource
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
buildModuleToEntriesMapping(compilation) {
|
|
21
|
-
this.moduleToEntries.clear();
|
|
22
|
-
for (const [entryName, entryDependency] of compilation.entries.entries()){
|
|
23
|
-
const entryModule = compilation.moduleGraph.getModule(entryDependency.dependencies[0]);
|
|
24
|
-
if (entryModule) this.traverseModulesFromEntry(entryModule, entryName, compilation.moduleGraph, new Set());
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
traverseModulesFromEntry(module, entryName, moduleGraph, visited) {
|
|
28
|
-
if (!module?.resource || visited.has(module.resource)) return;
|
|
29
|
-
visited.add(module.resource);
|
|
30
|
-
if (!this.moduleToEntries.has(module.resource)) this.moduleToEntries.set(module.resource, new Set());
|
|
31
|
-
this.moduleToEntries.get(module.resource).add(entryName);
|
|
32
|
-
for (const connection of moduleGraph.getOutgoingConnections(module))if (connection.module && 'resource' in connection.module) this.traverseModulesFromEntry(connection.module, entryName, moduleGraph, visited);
|
|
33
|
-
}
|
|
34
|
-
findModuleEntries(module, compilation, resourcePath2Entries, visited = new Set()) {
|
|
35
|
-
if (!this.isValidModule(module) || visited.has(module.resource)) return [];
|
|
36
|
-
visited.add(module.resource);
|
|
37
|
-
const currentEntries = resourcePath2Entries.get(module.resource);
|
|
38
|
-
if (this.hasValidEntries(currentEntries)) return currentEntries;
|
|
39
|
-
const entryNames = this.moduleToEntries.get(module.resource);
|
|
40
|
-
if (entryNames && entryNames.size > 0) {
|
|
41
|
-
const entries = [];
|
|
42
|
-
for (const entryName of entryNames){
|
|
43
|
-
const entryPath = this.getEntryPathByName(entryName, compilation);
|
|
44
|
-
if (entryPath) entries.push({
|
|
45
|
-
entryName,
|
|
46
|
-
entryPath
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
return entries;
|
|
50
|
-
}
|
|
51
|
-
const issuer = findRootIssuer(compilation.moduleGraph, module);
|
|
52
|
-
if (!issuer) return [];
|
|
53
|
-
const issuerEntries = this.findModuleEntries(issuer, compilation, resourcePath2Entries, visited);
|
|
54
|
-
if (issuerEntries.length > 0) return issuerEntries;
|
|
55
|
-
const entryName = this.getEntryNameFromIssuer(issuer);
|
|
56
|
-
if (entryName) return [
|
|
57
|
-
this.createEntryFromIssuer(issuer, entryName)
|
|
58
|
-
];
|
|
59
|
-
return [];
|
|
60
|
-
}
|
|
61
|
-
getEntryPathByName(entryName, compilation) {
|
|
62
|
-
const entryDependency = compilation.entries.get(entryName);
|
|
63
|
-
if (entryDependency && entryDependency.dependencies.length > 0) {
|
|
64
|
-
const firstDep = entryDependency.dependencies[0];
|
|
65
|
-
if ('request' in firstDep && 'string' == typeof firstDep.request) return firstDep.request;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
apply(compiler) {
|
|
69
|
-
const { EntryPlugin, WebpackError, sources: { RawSource } } = compiler.rspack;
|
|
70
|
-
const includeModule = async (compilation, resource, resourceEntryNames, layer)=>{
|
|
71
|
-
const entries = Array.from(compilation.entries.entries());
|
|
72
|
-
if (0 === entries.length) return void compilation.errors.push(new WebpackError("Could not find an entry in the compilation."));
|
|
73
|
-
const includePromises = entries.filter(([entryName])=>resourceEntryNames?.includes(entryName)).map(([entryName])=>{
|
|
74
|
-
const dependency = EntryPlugin.createDependency(resource);
|
|
75
|
-
return new Promise((resolve, reject)=>{
|
|
76
|
-
compilation.addInclude(compiler.context, dependency, {
|
|
77
|
-
name: entryName,
|
|
78
|
-
layer
|
|
79
|
-
}, (error, module)=>{
|
|
80
|
-
if (error) {
|
|
81
|
-
compilation.errors.push(error);
|
|
82
|
-
return reject(error);
|
|
83
|
-
}
|
|
84
|
-
if (!module) {
|
|
85
|
-
const noModuleError = new WebpackError("Module not added");
|
|
86
|
-
noModuleError.file = resource;
|
|
87
|
-
compilation.errors.push(noModuleError);
|
|
88
|
-
return reject(noModuleError);
|
|
89
|
-
}
|
|
90
|
-
setRscBuildInfo(module, {
|
|
91
|
-
__entryName: entryName
|
|
92
|
-
});
|
|
93
|
-
compilation.moduleGraph.getExportsInfo(module).setUsedInUnknownWay(entryName);
|
|
94
|
-
resolve();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
await Promise.all(includePromises);
|
|
99
|
-
};
|
|
100
|
-
let needsAdditionalPass = false;
|
|
101
|
-
compiler.hooks.finishMake.tapPromise(RscServerPlugin.name, async (compilation)=>{
|
|
102
|
-
this.buildModuleToEntriesMapping(compilation);
|
|
103
|
-
const processModules = (modules)=>{
|
|
104
|
-
let hasChangeReference = false;
|
|
105
|
-
for (const module of modules){
|
|
106
|
-
if ('resource' in module && isCssModule(module)) this.styles.add(module.resource);
|
|
107
|
-
const buildInfo = getRscBuildInfo(module);
|
|
108
|
-
if (!buildInfo || !buildInfo.resourcePath) continue;
|
|
109
|
-
if (module.layer && 'server' === buildInfo.type) sharedData.set(buildInfo?.resourcePath, buildInfo);
|
|
110
|
-
if (!module.layer && 'client' === buildInfo.type) sharedData.set(buildInfo?.resourcePath, buildInfo);
|
|
111
|
-
const currentReference = buildInfo?.type === 'client' ? this.clientReferencesMap.get(buildInfo.resourcePath) : this.serverReferencesMap.get(buildInfo.resourcePath);
|
|
112
|
-
if (buildInfo?.type !== 'client' || currentReference) {
|
|
113
|
-
if (buildInfo?.type === 'server' && !currentReference) {
|
|
114
|
-
hasChangeReference = true;
|
|
115
|
-
this.serverReferencesMap.set(buildInfo.resourcePath, buildInfo.exportNames);
|
|
116
|
-
}
|
|
117
|
-
} else {
|
|
118
|
-
hasChangeReference = true;
|
|
119
|
-
this.clientReferencesMap.set(buildInfo.resourcePath, buildInfo.clientReferences);
|
|
120
|
-
}
|
|
121
|
-
const entries = this.findModuleEntries(module, compilation, rspack_rsc_server_plugin_resourcePath2Entries);
|
|
122
|
-
if (entries.length > 0) rspack_rsc_server_plugin_resourcePath2Entries.set(module.resource, entries);
|
|
123
|
-
}
|
|
124
|
-
return hasChangeReference;
|
|
125
|
-
};
|
|
126
|
-
this.serverManifest = {};
|
|
127
|
-
const clientReferences = [
|
|
128
|
-
...this.clientReferencesMap.keys()
|
|
129
|
-
];
|
|
130
|
-
const serverReferences = [
|
|
131
|
-
...this.serverReferencesMap.keys()
|
|
132
|
-
];
|
|
133
|
-
const referencesBefore = [
|
|
134
|
-
...clientReferences,
|
|
135
|
-
...serverReferences
|
|
136
|
-
];
|
|
137
|
-
let hasChangeReference = false;
|
|
138
|
-
await Promise.all([
|
|
139
|
-
...clientReferences.map(async (resource)=>{
|
|
140
|
-
try {
|
|
141
|
-
await includeModule(compilation, resource, rspack_rsc_server_plugin_resourcePath2Entries.get(resource)?.map((entry)=>entry.entryName) || []);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
console.error(error);
|
|
144
|
-
hasChangeReference = true;
|
|
145
|
-
this.clientReferencesMap.delete(resource);
|
|
146
|
-
}
|
|
147
|
-
}),
|
|
148
|
-
...serverReferences.map(async (resource)=>{
|
|
149
|
-
try {
|
|
150
|
-
await includeModule(compilation, resource, rspack_rsc_server_plugin_resourcePath2Entries.get(resource)?.map((entry)=>entry.entryName) || [], rspackRscLayerName);
|
|
151
|
-
} catch (error) {
|
|
152
|
-
console.error(error);
|
|
153
|
-
hasChangeReference = true;
|
|
154
|
-
this.serverReferencesMap.delete(resource);
|
|
155
|
-
}
|
|
156
|
-
})
|
|
157
|
-
]);
|
|
158
|
-
hasChangeReference = processModules(compilation.modules);
|
|
159
|
-
const referencesAfter = [
|
|
160
|
-
...this.clientReferencesMap.keys(),
|
|
161
|
-
...this.serverReferencesMap.keys()
|
|
162
|
-
];
|
|
163
|
-
if (referencesBefore.length !== referencesAfter.length || !referencesAfter.every((reference)=>referencesBefore.includes(reference)) && hasChangeReference) needsAdditionalPass = true;
|
|
164
|
-
});
|
|
165
|
-
compiler.hooks.done.tap(RscServerPlugin.name, ()=>{
|
|
166
|
-
sharedData.set('serverReferencesMap', this.serverReferencesMap);
|
|
167
|
-
sharedData.set('clientReferencesMap', this.clientReferencesMap);
|
|
168
|
-
sharedData.set('styles', this.styles);
|
|
169
|
-
});
|
|
170
|
-
compiler.hooks.afterCompile.tap(RscServerPlugin.name, (compilation)=>{
|
|
171
|
-
for (const module of compilation.modules){
|
|
172
|
-
const resource = module.nameForCondition();
|
|
173
|
-
if (!resource) continue;
|
|
174
|
-
const moduleId = compilation.chunkGraph.getModuleId(module);
|
|
175
|
-
if (null !== moduleId) {
|
|
176
|
-
if (module.layer !== rspackRscLayerName && this.clientReferencesMap.has(resource)) {
|
|
177
|
-
const clientReferences = this.clientReferencesMap.get(resource);
|
|
178
|
-
if (clientReferences) for (const clientReference of clientReferences)clientReference.ssrId = moduleId;
|
|
179
|
-
else compilation.errors.push(new WebpackError(`Could not find client references info in \`clientReferencesMap\` for ${resource}.`));
|
|
180
|
-
} else if (module.layer === rspackRscLayerName && getRscBuildInfo(module)?.type === 'server') {
|
|
181
|
-
const serverReferencesModuleInfo = getRscBuildInfo(module);
|
|
182
|
-
if (serverReferencesModuleInfo) {
|
|
183
|
-
serverReferencesModuleInfo.moduleId = moduleId;
|
|
184
|
-
for (const exportName of serverReferencesModuleInfo.exportNames)this.serverManifest[`${moduleId}#${exportName}`] = {
|
|
185
|
-
id: moduleId,
|
|
186
|
-
chunks: [],
|
|
187
|
-
name: exportName
|
|
188
|
-
};
|
|
189
|
-
} else compilation.errors.push(new WebpackError(`Could not find server references module info in \`serverReferencesMap\` for ${resource}.`));
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
compiler.hooks.thisCompilation.tap(RscServerPlugin.name, (compilation)=>{
|
|
195
|
-
compilation.hooks.needAdditionalPass.tap(RscServerPlugin.name, ()=>!(needsAdditionalPass = !needsAdditionalPass));
|
|
196
|
-
compilation.hooks.processAssets.tap(RscServerPlugin.name, ()=>{
|
|
197
|
-
compilation.emitAsset(this.serverManifestFilename, new RawSource(JSON.stringify(this.serverManifest, null, 2), false));
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
constructor(options){
|
|
202
|
-
this.clientReferencesMap = new Map();
|
|
203
|
-
this.serverReferencesMap = new Map();
|
|
204
|
-
this.serverManifest = {};
|
|
205
|
-
this.entryPath2Name = new Map();
|
|
206
|
-
this.moduleToEntries = new Map();
|
|
207
|
-
this.styles = new Set();
|
|
208
|
-
this.serverManifestFilename = options?.serverManifestFilename || "react-server-manifest.json";
|
|
209
|
-
this.entryPath2Name = options?.entryPath2Name || new Map();
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
export { RscServerPlugin };
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { isServerModule, parseSource, sharedData } from "./common.mjs";
|
|
3
|
-
async function rscClientLoader(source, sourceMap) {
|
|
4
|
-
this.cacheable(true);
|
|
5
|
-
const callback = this.async();
|
|
6
|
-
const ast = await parseSource(source);
|
|
7
|
-
const hasUseServerDirective = await isServerModule(ast);
|
|
8
|
-
if (!hasUseServerDirective) return void callback(null, source, sourceMap);
|
|
9
|
-
const { callServerImport = "@modern-js/runtime/rsc/client", registerImport = "@modern-js/runtime/rsc/client" } = this.getOptions();
|
|
10
|
-
const buildInfo = sharedData.get(this.resourcePath);
|
|
11
|
-
const moduleInfo = buildInfo ? {
|
|
12
|
-
moduleId: buildInfo?.moduleId,
|
|
13
|
-
exportNames: buildInfo?.exportNames
|
|
14
|
-
} : null;
|
|
15
|
-
if (!moduleInfo) {
|
|
16
|
-
this.emitError(new Error(`Could not find server module info in \`serverReferencesMap\` for ${this.resourcePath}.`));
|
|
17
|
-
callback(null, '');
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
const { moduleId, exportNames } = moduleInfo;
|
|
21
|
-
if (!moduleId) {
|
|
22
|
-
this.emitError(new Error(`Could not find server module ID in \`serverReferencesMap\` for ${this.resourcePath}.`));
|
|
23
|
-
callback(null, '');
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (!exportNames) return void callback(null, '');
|
|
27
|
-
const importsCode = `
|
|
28
|
-
import { createServerReference } from "${registerImport}";
|
|
29
|
-
import { callServer } from "${callServerImport}";
|
|
30
|
-
`;
|
|
31
|
-
const exportsCode = exportNames.map((item)=>{
|
|
32
|
-
const name = item;
|
|
33
|
-
if ('default' === name) return `export default createServerReference("${moduleId}", callServer);`;
|
|
34
|
-
return `export var ${name} = createServerReference("${moduleId}#${name}", callServer);`;
|
|
35
|
-
}).join('\n');
|
|
36
|
-
callback(null, `${importsCode}\n${exportsCode}`);
|
|
37
|
-
}
|
|
38
|
-
export { rscClientLoader as default };
|