@modern-js/uni-builder 2.64.1 → 2.64.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.
- package/dist/index.d.ts +2 -2
- package/dist/rspack/index.js +14 -2
- package/dist/shared/plugins/frameworkConfig.js +10 -1
- package/dist/shared/rsc/common.d.ts +22 -0
- package/dist/shared/rsc/common.js +170 -0
- package/dist/shared/rsc/index.d.ts +2 -0
- package/dist/shared/rsc/index.js +31 -0
- package/dist/shared/rsc/plugins/rsbuild-rsc-plugin.d.ts +7 -0
- package/dist/shared/rsc/plugins/rsbuild-rsc-plugin.js +138 -0
- package/dist/shared/rsc/plugins/rsc-client-plugin.d.ts +13 -0
- package/dist/shared/rsc/plugins/rsc-client-plugin.js +205 -0
- package/dist/shared/rsc/plugins/rsc-server-plugin.d.ts +20 -0
- package/dist/shared/rsc/plugins/rsc-server-plugin.js +272 -0
- package/dist/shared/rsc/plugins/rspack-rsc-client-plugin.d.ts +14 -0
- package/dist/shared/rsc/plugins/rspack-rsc-client-plugin.js +238 -0
- package/dist/shared/rsc/plugins/rspack-rsc-server-plugin.d.ts +23 -0
- package/dist/shared/rsc/plugins/rspack-rsc-server-plugin.js +240 -0
- package/dist/shared/rsc/rsc-client-loader.d.ts +7 -0
- package/dist/shared/rsc/rsc-client-loader.js +70 -0
- package/dist/shared/rsc/rsc-css-loader.d.ts +2 -0
- package/dist/shared/rsc/rsc-css-loader.js +30 -0
- package/dist/shared/rsc/rsc-server-loader.d.ts +6 -0
- package/dist/shared/rsc/rsc-server-loader.js +94 -0
- package/dist/shared/rsc/rsc-ssr-loader.d.ts +6 -0
- package/dist/shared/rsc/rsc-ssr-loader.js +59 -0
- package/dist/types.d.ts +8 -0
- package/dist/webpack/index.js +13 -1
- package/package.json +25 -15
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type Webpack from 'webpack';
|
|
2
|
+
export interface RscServerPluginOptions {
|
|
3
|
+
readonly serverManifestFilename?: string;
|
|
4
|
+
readonly entryPath2Name: Map<string, string>;
|
|
5
|
+
}
|
|
6
|
+
export interface ModuleExportsInfo {
|
|
7
|
+
readonly moduleResource: string;
|
|
8
|
+
readonly exportName: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class RscServerPlugin {
|
|
11
|
+
private clientReferencesMap;
|
|
12
|
+
private serverReferencesMap;
|
|
13
|
+
private serverManifest;
|
|
14
|
+
private serverManifestFilename;
|
|
15
|
+
private entryPath2Name;
|
|
16
|
+
private styles;
|
|
17
|
+
constructor(options: RscServerPluginOptions);
|
|
18
|
+
private findModuleEntries;
|
|
19
|
+
apply(compiler: Webpack.Compiler): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var rsc_server_plugin_exports = {};
|
|
20
|
+
__export(rsc_server_plugin_exports, {
|
|
21
|
+
RscServerPlugin: () => RscServerPlugin
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(rsc_server_plugin_exports);
|
|
24
|
+
var import_webpack = require("webpack");
|
|
25
|
+
var import_common = require("../common");
|
|
26
|
+
const resourcePath2Entries = /* @__PURE__ */ new Map();
|
|
27
|
+
class RscServerPlugin {
|
|
28
|
+
findModuleEntries(module2, compilation, resourcePath2Entries2, visited = /* @__PURE__ */ new Set()) {
|
|
29
|
+
if (!(module2 === null || module2 === void 0 ? void 0 : module2.resource) || visited.has(module2.resource)) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
visited.add(module2.resource);
|
|
33
|
+
const currentEntries = resourcePath2Entries2.get(module2.resource);
|
|
34
|
+
if (currentEntries && (currentEntries === null || currentEntries === void 0 ? void 0 : currentEntries.length) > 0) {
|
|
35
|
+
return currentEntries;
|
|
36
|
+
}
|
|
37
|
+
const issuer = (0, import_common.findRootIssuer)(compilation.moduleGraph, module2);
|
|
38
|
+
if (!issuer) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const issuerEntries = this.findModuleEntries(issuer, compilation, resourcePath2Entries2, visited);
|
|
42
|
+
if (issuerEntries.length > 0) {
|
|
43
|
+
return issuerEntries;
|
|
44
|
+
}
|
|
45
|
+
if (issuer.resource) {
|
|
46
|
+
const entryName = this.entryPath2Name.get(issuer.resource);
|
|
47
|
+
if (entryName) {
|
|
48
|
+
return [
|
|
49
|
+
{
|
|
50
|
+
entryName,
|
|
51
|
+
entryPath: issuer.resource
|
|
52
|
+
}
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
apply(compiler) {
|
|
59
|
+
const { EntryPlugin, WebpackError, dependencies: { NullDependency }, sources: { RawSource }, RuntimeGlobals } = compiler.webpack;
|
|
60
|
+
class ServerReferenceDependency extends NullDependency {
|
|
61
|
+
get type() {
|
|
62
|
+
return `server-reference`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
ServerReferenceDependency.Template = class ServerReferenceDependencyTemplate extends NullDependency.Template {
|
|
66
|
+
apply(_dependency, _source, { runtimeRequirements }) {
|
|
67
|
+
runtimeRequirements.add(RuntimeGlobals.moduleId);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
function hasServerReferenceDependency(module2) {
|
|
71
|
+
return module2.dependencies.some((dependency) => dependency instanceof ServerReferenceDependency);
|
|
72
|
+
}
|
|
73
|
+
const includeModule = async (compilation, resource, resourceEntryNames, layer) => {
|
|
74
|
+
const entries = Array.from(compilation.entries.entries());
|
|
75
|
+
if (entries.length === 0) {
|
|
76
|
+
compilation.errors.push(new WebpackError(`Could not find an entry in the compilation.`));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const includePromises = entries.filter(([entryName]) => resourceEntryNames === null || resourceEntryNames === void 0 ? void 0 : resourceEntryNames.includes(entryName)).map(([entryName]) => {
|
|
80
|
+
const dependency = EntryPlugin.createDependency(resource, {
|
|
81
|
+
name: resource
|
|
82
|
+
});
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
compilation.addInclude(compiler.context, dependency, {
|
|
85
|
+
name: entryName,
|
|
86
|
+
layer
|
|
87
|
+
}, (error, module2) => {
|
|
88
|
+
if (error) {
|
|
89
|
+
compilation.errors.push(error);
|
|
90
|
+
return reject(error);
|
|
91
|
+
}
|
|
92
|
+
if (!module2) {
|
|
93
|
+
const noModuleError = new WebpackError(`Module not added`);
|
|
94
|
+
noModuleError.file = resource;
|
|
95
|
+
compilation.errors.push(noModuleError);
|
|
96
|
+
return reject(noModuleError);
|
|
97
|
+
}
|
|
98
|
+
(0, import_common.setRscBuildInfo)(module2, {
|
|
99
|
+
__entryName: entryName
|
|
100
|
+
});
|
|
101
|
+
compilation.moduleGraph.getExportsInfo(module2).setUsedInUnknownWay(entryName);
|
|
102
|
+
resolve();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
await Promise.all(includePromises);
|
|
107
|
+
};
|
|
108
|
+
let needsAdditionalPass = false;
|
|
109
|
+
compiler.hooks.finishMake.tapPromise(RscServerPlugin.name, async (compilation) => {
|
|
110
|
+
const processModules = (modules) => {
|
|
111
|
+
let hasChangeReference2 = false;
|
|
112
|
+
for (const module2 of modules) {
|
|
113
|
+
if ("resource" in module2 && (0, import_common.isCssModule)(module2)) {
|
|
114
|
+
this.styles.add(module2.resource);
|
|
115
|
+
}
|
|
116
|
+
const buildInfo = (0, import_common.getRscBuildInfo)(module2);
|
|
117
|
+
if (!buildInfo || !buildInfo.resourcePath) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (module2.layer && buildInfo.type === "server") {
|
|
121
|
+
import_common.sharedData.set(buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.resourcePath, buildInfo);
|
|
122
|
+
}
|
|
123
|
+
if (!module2.layer && buildInfo.type === "client") {
|
|
124
|
+
import_common.sharedData.set(buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.resourcePath, buildInfo);
|
|
125
|
+
}
|
|
126
|
+
const currentReference = (buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.type) === "client" ? this.clientReferencesMap.get(buildInfo.resourcePath) : this.serverReferencesMap.get(buildInfo.resourcePath);
|
|
127
|
+
if ((buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.type) === "client" && !currentReference) {
|
|
128
|
+
hasChangeReference2 = true;
|
|
129
|
+
this.clientReferencesMap.set(buildInfo.resourcePath, buildInfo.clientReferences);
|
|
130
|
+
} else if ((buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.type) === "server" && !currentReference) {
|
|
131
|
+
hasChangeReference2 = true;
|
|
132
|
+
this.serverReferencesMap.set(buildInfo.resourcePath, buildInfo.exportNames);
|
|
133
|
+
}
|
|
134
|
+
if (module2 instanceof import_webpack.NormalModule) {
|
|
135
|
+
const entries = this.findModuleEntries(module2, compilation, resourcePath2Entries);
|
|
136
|
+
if (entries.length > 0) {
|
|
137
|
+
resourcePath2Entries.set(module2.resource, entries);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return hasChangeReference2;
|
|
142
|
+
};
|
|
143
|
+
this.serverManifest = {};
|
|
144
|
+
const clientReferences = [
|
|
145
|
+
...this.clientReferencesMap.keys()
|
|
146
|
+
];
|
|
147
|
+
const serverReferences = [
|
|
148
|
+
...this.serverReferencesMap.keys()
|
|
149
|
+
];
|
|
150
|
+
const referencesBefore = [
|
|
151
|
+
...clientReferences,
|
|
152
|
+
...serverReferences
|
|
153
|
+
];
|
|
154
|
+
let hasChangeReference = false;
|
|
155
|
+
await Promise.all([
|
|
156
|
+
...clientReferences.map(async (resource) => {
|
|
157
|
+
try {
|
|
158
|
+
var _resourcePath2Entries_get;
|
|
159
|
+
await includeModule(compilation, resource, ((_resourcePath2Entries_get = resourcePath2Entries.get(resource)) === null || _resourcePath2Entries_get === void 0 ? void 0 : _resourcePath2Entries_get.map((entry) => entry.entryName)) || []);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error(error);
|
|
162
|
+
hasChangeReference = true;
|
|
163
|
+
this.clientReferencesMap.delete(resource);
|
|
164
|
+
}
|
|
165
|
+
}),
|
|
166
|
+
...serverReferences.map(async (resource) => {
|
|
167
|
+
try {
|
|
168
|
+
var _resourcePath2Entries_get;
|
|
169
|
+
await includeModule(compilation, resource, ((_resourcePath2Entries_get = resourcePath2Entries.get(resource)) === null || _resourcePath2Entries_get === void 0 ? void 0 : _resourcePath2Entries_get.map((entry) => entry.entryName)) || [], import_common.webpackRscLayerName);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
console.error(error);
|
|
172
|
+
hasChangeReference = true;
|
|
173
|
+
this.serverReferencesMap.delete(resource);
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
]);
|
|
177
|
+
hasChangeReference = processModules(compilation.modules);
|
|
178
|
+
const referencesAfter = [
|
|
179
|
+
...this.clientReferencesMap.keys(),
|
|
180
|
+
...this.serverReferencesMap.keys()
|
|
181
|
+
];
|
|
182
|
+
if (referencesBefore.length !== referencesAfter.length || !referencesAfter.every((reference) => referencesBefore.includes(reference)) && hasChangeReference) {
|
|
183
|
+
needsAdditionalPass = true;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
compiler.hooks.done.tap(RscServerPlugin.name, () => {
|
|
187
|
+
import_common.sharedData.set("serverReferencesMap", this.serverReferencesMap);
|
|
188
|
+
import_common.sharedData.set("clientReferencesMap", this.clientReferencesMap);
|
|
189
|
+
import_common.sharedData.set("styles", this.styles);
|
|
190
|
+
});
|
|
191
|
+
compiler.hooks.thisCompilation.tap(RscServerPlugin.name, (compilation, { normalModuleFactory }) => {
|
|
192
|
+
compilation.dependencyFactories.set(ServerReferenceDependency, normalModuleFactory);
|
|
193
|
+
compilation.dependencyTemplates.set(ServerReferenceDependency, new ServerReferenceDependency.Template());
|
|
194
|
+
const onNormalModuleFactoryParser = (parser) => {
|
|
195
|
+
parser.hooks.program.tap(RscServerPlugin.name, () => {
|
|
196
|
+
const { module: module2 } = parser.state;
|
|
197
|
+
const { resource } = module2;
|
|
198
|
+
const buildInfo = (0, import_common.getRscBuildInfo)(module2);
|
|
199
|
+
const isClientModule = (buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.type) === "client";
|
|
200
|
+
const isServerModule = (buildInfo === null || buildInfo === void 0 ? void 0 : buildInfo.type) === "server";
|
|
201
|
+
if (isServerModule && isClientModule) {
|
|
202
|
+
compilation.errors.push(new WebpackError(`Cannot use both 'use server' and 'use client' in the same module ${resource}.`));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (module2.layer === import_common.webpackRscLayerName && isServerModule && !hasServerReferenceDependency(module2)) {
|
|
206
|
+
module2.addDependency(new ServerReferenceDependency());
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
parser.hooks.expression.for(RuntimeGlobals.moduleId).tap(RscServerPlugin.name, () => {
|
|
210
|
+
parser.state.module.buildInfo.moduleArgument = RuntimeGlobals.module;
|
|
211
|
+
return true;
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
normalModuleFactory.hooks.parser.for(`javascript/auto`).tap(`HarmonyModulesPlugin`, onNormalModuleFactoryParser);
|
|
215
|
+
normalModuleFactory.hooks.parser.for(`javascript/dynamic`).tap(`HarmonyModulesPlugin`, onNormalModuleFactoryParser);
|
|
216
|
+
normalModuleFactory.hooks.parser.for(`javascript/esm`).tap(`HarmonyModulesPlugin`, onNormalModuleFactoryParser);
|
|
217
|
+
compilation.hooks.needAdditionalPass.tap(RscServerPlugin.name, () => !(needsAdditionalPass = !needsAdditionalPass));
|
|
218
|
+
compilation.hooks.afterOptimizeModuleIds.tap(RscServerPlugin.name, (modules) => {
|
|
219
|
+
for (const module2 of modules) {
|
|
220
|
+
const resource = module2.nameForCondition();
|
|
221
|
+
if (!resource) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const moduleId = compilation.chunkGraph.getModuleId(module2);
|
|
225
|
+
if (moduleId === null) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (module2.layer !== import_common.webpackRscLayerName && this.clientReferencesMap.has(resource)) {
|
|
229
|
+
const clientReferences = this.clientReferencesMap.get(resource);
|
|
230
|
+
if (clientReferences) {
|
|
231
|
+
for (const clientReference of clientReferences) {
|
|
232
|
+
clientReference.ssrId = moduleId;
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
compilation.errors.push(new WebpackError(`Could not find client references info in \`clientReferencesMap\` for ${resource}.`));
|
|
236
|
+
}
|
|
237
|
+
} else if (hasServerReferenceDependency(module2)) {
|
|
238
|
+
const serverReferencesModuleInfo = (0, import_common.getRscBuildInfo)(module2);
|
|
239
|
+
if (serverReferencesModuleInfo) {
|
|
240
|
+
serverReferencesModuleInfo.moduleId = moduleId;
|
|
241
|
+
for (const exportName of serverReferencesModuleInfo.exportNames) {
|
|
242
|
+
this.serverManifest[`${moduleId}#${exportName}`] = {
|
|
243
|
+
id: moduleId,
|
|
244
|
+
chunks: [],
|
|
245
|
+
name: exportName
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
} else {
|
|
249
|
+
compilation.errors.push(new WebpackError(`Could not find server references module info in \`serverReferencesMap\` for ${resource}.`));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
compilation.hooks.processAssets.tap(RscServerPlugin.name, () => {
|
|
255
|
+
compilation.emitAsset(this.serverManifestFilename, new RawSource(JSON.stringify(this.serverManifest, null, 2), false));
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
constructor(options) {
|
|
260
|
+
this.clientReferencesMap = /* @__PURE__ */ new Map();
|
|
261
|
+
this.serverReferencesMap = /* @__PURE__ */ new Map();
|
|
262
|
+
this.serverManifest = {};
|
|
263
|
+
this.entryPath2Name = /* @__PURE__ */ new Map();
|
|
264
|
+
this.styles = /* @__PURE__ */ new Set();
|
|
265
|
+
this.serverManifestFilename = (options === null || options === void 0 ? void 0 : options.serverManifestFilename) || `react-server-manifest.json`;
|
|
266
|
+
this.entryPath2Name = (options === null || options === void 0 ? void 0 : options.entryPath2Name) || /* @__PURE__ */ new Map();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
270
|
+
0 && (module.exports = {
|
|
271
|
+
RscServerPlugin
|
|
272
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type Webpack from 'webpack';
|
|
2
|
+
export interface RscClientPluginOptions {
|
|
3
|
+
readonly clientManifestFilename?: string;
|
|
4
|
+
readonly ssrManifestFilename?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class RspackRscClientPlugin {
|
|
7
|
+
private clientReferencesMap;
|
|
8
|
+
private clientManifestFilename;
|
|
9
|
+
private ssrManifestFilename;
|
|
10
|
+
private styles?;
|
|
11
|
+
private dependencies;
|
|
12
|
+
constructor(options?: RscClientPluginOptions);
|
|
13
|
+
apply(compiler: Webpack.Compiler): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var rspack_rsc_client_plugin_exports = {};
|
|
30
|
+
__export(rspack_rsc_client_plugin_exports, {
|
|
31
|
+
RspackRscClientPlugin: () => RspackRscClientPlugin
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(rspack_rsc_client_plugin_exports);
|
|
34
|
+
var import_path = __toESM(require("path"));
|
|
35
|
+
var import_common = require("../common");
|
|
36
|
+
const hasExtension = (filePath) => {
|
|
37
|
+
return import_path.default.extname(filePath) !== "";
|
|
38
|
+
};
|
|
39
|
+
class RspackRscClientPlugin {
|
|
40
|
+
apply(compiler) {
|
|
41
|
+
const {
|
|
42
|
+
EntryPlugin,
|
|
43
|
+
// AsyncDependenciesBlock,
|
|
44
|
+
RuntimeGlobals,
|
|
45
|
+
RuntimeModule,
|
|
46
|
+
WebpackError,
|
|
47
|
+
// dependencies: { ModuleDependency, NullDependency },
|
|
48
|
+
sources: { RawSource }
|
|
49
|
+
} = compiler.webpack;
|
|
50
|
+
const ssrManifest = {
|
|
51
|
+
moduleMap: {},
|
|
52
|
+
moduleLoading: null,
|
|
53
|
+
styles: []
|
|
54
|
+
};
|
|
55
|
+
const getEntryModule = (compilation) => {
|
|
56
|
+
const entryModules = [];
|
|
57
|
+
for (const [, entryValue] of compilation.entries.entries()) {
|
|
58
|
+
const entryDependency = entryValue.dependencies[0];
|
|
59
|
+
if (!entryDependency) {
|
|
60
|
+
compilation.errors.push(new WebpackError(`Could not find an entry dependency.`));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const resolvedModule = compilation.moduleGraph.getModule(entryDependency);
|
|
64
|
+
if (resolvedModule) {
|
|
65
|
+
entryModules.push(resolvedModule);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (entryModules.length === 0) {
|
|
69
|
+
compilation.errors.push(new WebpackError(`Could not find any entries in the compilation.`));
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
return entryModules;
|
|
73
|
+
};
|
|
74
|
+
const addClientReferencesChunks = (compilation, entryModule, callback) => {
|
|
75
|
+
const promises = [];
|
|
76
|
+
[
|
|
77
|
+
...this.clientReferencesMap.keys()
|
|
78
|
+
].forEach((resourcePath, index) => {
|
|
79
|
+
const dependency = EntryPlugin.createDependency(resourcePath, {
|
|
80
|
+
name: resourcePath
|
|
81
|
+
});
|
|
82
|
+
const entries = compilation.entries.entries();
|
|
83
|
+
for (const [entryName, entry] of entries) {
|
|
84
|
+
if (hasExtension(entryName)) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
promises.push(new Promise((resolve, reject) => {
|
|
88
|
+
compilation.addInclude(compiler.context, dependency, {
|
|
89
|
+
name: entryName
|
|
90
|
+
}, (error, module2) => {
|
|
91
|
+
if (error) {
|
|
92
|
+
reject(error);
|
|
93
|
+
} else {
|
|
94
|
+
this.dependencies.push(dependency);
|
|
95
|
+
resolve(void 0);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
if (this.styles && this.styles.size > 0) {
|
|
102
|
+
for (const style of this.styles) {
|
|
103
|
+
const dependency = EntryPlugin.createDependency(style, {
|
|
104
|
+
name: style
|
|
105
|
+
});
|
|
106
|
+
promises.push(new Promise((resolve, reject) => {
|
|
107
|
+
compilation.addInclude(compiler.context, dependency, {
|
|
108
|
+
name: void 0
|
|
109
|
+
}, (error, module2) => {
|
|
110
|
+
if (error) {
|
|
111
|
+
reject(error);
|
|
112
|
+
} else {
|
|
113
|
+
this.dependencies.push(dependency);
|
|
114
|
+
resolve(void 0);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
Promise.all(promises).then(() => callback(null)).catch((error) => callback(error));
|
|
121
|
+
};
|
|
122
|
+
compiler.hooks.finishMake.tapAsync(RspackRscClientPlugin.name, (compilation, callback) => {
|
|
123
|
+
const entryModules = getEntryModule(compilation);
|
|
124
|
+
for (const entryModule of entryModules) {
|
|
125
|
+
if (entryModule) {
|
|
126
|
+
addClientReferencesChunks(compilation, entryModule, callback);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
compiler.hooks.compilation.tap(RspackRscClientPlugin.name, (compilation, { normalModuleFactory }) => {
|
|
131
|
+
class EntryNameRuntimeModule extends RuntimeModule {
|
|
132
|
+
generate() {
|
|
133
|
+
return `window.__MODERN_JS_ENTRY_NAME="${this.entryName}";`;
|
|
134
|
+
}
|
|
135
|
+
constructor(entryName) {
|
|
136
|
+
super("entry-name", 10);
|
|
137
|
+
this.entryName = entryName;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.ensureChunk).tap(RspackRscClientPlugin.name, (chunk, set) => {
|
|
141
|
+
Array.from(compilation.entrypoints.entries()).forEach(([entryName, entrypoint]) => {
|
|
142
|
+
if (entrypoint.chunks.includes(chunk)) {
|
|
143
|
+
compilation.addRuntimeModule(chunk, new EntryNameRuntimeModule(entryName));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
compiler.hooks.thisCompilation.tap(RspackRscClientPlugin.name, (compilation, { normalModuleFactory }) => {
|
|
149
|
+
this.styles = import_common.sharedData.get("styles");
|
|
150
|
+
this.clientReferencesMap = import_common.sharedData.get("clientReferencesMap");
|
|
151
|
+
compilation.hooks.additionalTreeRuntimeRequirements.tap(RspackRscClientPlugin.name, (_chunk, runtimeRequirements) => {
|
|
152
|
+
runtimeRequirements.add(RuntimeGlobals.ensureChunkHandlers);
|
|
153
|
+
runtimeRequirements.add(RuntimeGlobals.ensureChunk);
|
|
154
|
+
runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
|
|
155
|
+
});
|
|
156
|
+
compilation.hooks.processAssets.tap(RspackRscClientPlugin.name, () => {
|
|
157
|
+
const clientManifest = {};
|
|
158
|
+
const { chunkGraph, moduleGraph, modules } = compilation;
|
|
159
|
+
for (const module2 of modules) {
|
|
160
|
+
const resourcePath = module2.nameForCondition();
|
|
161
|
+
const clientReferences = resourcePath ? this.clientReferencesMap.get(resourcePath) : void 0;
|
|
162
|
+
if (clientReferences) {
|
|
163
|
+
const moduleId = chunkGraph.getModuleId(module2);
|
|
164
|
+
const ssrModuleMetaData = {};
|
|
165
|
+
for (const { id, exportName, ssrId } of clientReferences) {
|
|
166
|
+
const clientExportName = exportName;
|
|
167
|
+
const ssrExportName = exportName;
|
|
168
|
+
const chunksSet = /* @__PURE__ */ new Set();
|
|
169
|
+
for (const chunk of chunkGraph.getModuleChunksIterable(module2)) {
|
|
170
|
+
chunksSet.add(chunk);
|
|
171
|
+
}
|
|
172
|
+
for (const connection of moduleGraph.getOutgoingConnections(module2)) {
|
|
173
|
+
for (const chunk of chunkGraph.getModuleChunksIterable(connection.module)) {
|
|
174
|
+
chunksSet.add(chunk);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const chunks = [];
|
|
178
|
+
const styles = [];
|
|
179
|
+
for (const chunk of chunksSet) {
|
|
180
|
+
if (chunk.id) {
|
|
181
|
+
for (const file of chunk.files) {
|
|
182
|
+
if (file.endsWith(".js")) {
|
|
183
|
+
chunks.push(chunk.id, file);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
clientManifest[id] = {
|
|
189
|
+
id: moduleId,
|
|
190
|
+
name: clientExportName,
|
|
191
|
+
chunks,
|
|
192
|
+
styles
|
|
193
|
+
};
|
|
194
|
+
if (ssrId) {
|
|
195
|
+
ssrModuleMetaData[clientExportName] = {
|
|
196
|
+
id: ssrId,
|
|
197
|
+
name: ssrExportName,
|
|
198
|
+
chunks: []
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
ssrManifest.moduleMap[moduleId] = ssrModuleMetaData;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
compilation.emitAsset(this.clientManifestFilename, new RawSource(JSON.stringify(clientManifest, null, 2), false));
|
|
206
|
+
const { crossOriginLoading, publicPath = `` } = compilation.outputOptions;
|
|
207
|
+
var _compilation_hash;
|
|
208
|
+
ssrManifest.moduleLoading = {
|
|
209
|
+
// https://github.com/webpack/webpack/blob/87660921808566ef3b8796f8df61bd79fc026108/lib/runtime/PublicPathRuntimeModule.js#L30-L32
|
|
210
|
+
prefix: compilation.getPath(publicPath, {
|
|
211
|
+
hash: (_compilation_hash = compilation.hash) !== null && _compilation_hash !== void 0 ? _compilation_hash : `XXXX`
|
|
212
|
+
}),
|
|
213
|
+
crossOrigin: crossOriginLoading ? crossOriginLoading === `use-credentials` ? crossOriginLoading : `` : void 0
|
|
214
|
+
};
|
|
215
|
+
if (this.styles && this.styles.size > 0) {
|
|
216
|
+
const assets = compilation.getAssets();
|
|
217
|
+
const cssAsset = assets.find((asset) => {
|
|
218
|
+
return asset.name.endsWith(".css");
|
|
219
|
+
});
|
|
220
|
+
if (cssAsset) {
|
|
221
|
+
ssrManifest.styles.push(cssAsset.name);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
compilation.emitAsset(this.ssrManifestFilename, new RawSource(JSON.stringify(ssrManifest, null, 2), false));
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
constructor(options) {
|
|
229
|
+
this.clientReferencesMap = /* @__PURE__ */ new Map();
|
|
230
|
+
this.dependencies = [];
|
|
231
|
+
this.clientManifestFilename = (options === null || options === void 0 ? void 0 : options.clientManifestFilename) || `react-client-manifest.json`;
|
|
232
|
+
this.ssrManifestFilename = (options === null || options === void 0 ? void 0 : options.ssrManifestFilename) || `react-ssr-manifest.json`;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
236
|
+
0 && (module.exports = {
|
|
237
|
+
RspackRscClientPlugin
|
|
238
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin is developing, ignore it now
|
|
3
|
+
*/
|
|
4
|
+
import type Webpack from 'webpack';
|
|
5
|
+
export interface RscServerPluginOptions {
|
|
6
|
+
readonly serverManifestFilename?: string;
|
|
7
|
+
readonly entryPath2Name: Map<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface ModuleExportsInfo {
|
|
10
|
+
readonly moduleResource: string;
|
|
11
|
+
readonly exportName: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class RscServerPlugin {
|
|
14
|
+
private clientReferencesMap;
|
|
15
|
+
private serverReferencesMap;
|
|
16
|
+
private serverManifest;
|
|
17
|
+
private serverManifestFilename;
|
|
18
|
+
private entryPath2Name;
|
|
19
|
+
private styles;
|
|
20
|
+
constructor(options: RscServerPluginOptions);
|
|
21
|
+
private findModuleEntries;
|
|
22
|
+
apply(compiler: Webpack.Compiler): void;
|
|
23
|
+
}
|