react-cosmos-plugin-rspack 2.0.0 → 2.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/README.md +0 -16
- package/dist/client/errorOverlay/reactErrorOverlay.js +1719 -1458
- package/dist/client/index.js +15 -1837
- package/dist/client/reactDevtoolsHook.js +0 -8
- package/dist/client/userImports.js +10 -12
- package/dist/server/rspackServerPlugin.js +474 -621
- package/dist/ui/build.js +32 -45
- package/package.json +17 -17
|
@@ -1,701 +1,554 @@
|
|
|
1
|
-
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
|
|
4
|
-
// src/server/rspackConfig/constants.ts
|
|
5
|
-
var RENDERER_FILENAME = "renderer.html";
|
|
6
|
-
|
|
7
|
-
// src/server/rspackConfigPlugin.ts
|
|
8
|
-
async function rspackConfigPlugin({
|
|
9
|
-
config
|
|
10
|
-
}) {
|
|
11
|
-
if (config.rendererUrl) {
|
|
12
|
-
return config;
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
...config,
|
|
16
|
-
rendererUrl: path.join(config.publicUrl, RENDERER_FILENAME)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// src/server/rspackDevServerPlugin.ts
|
|
21
|
-
import * as path8 from "path";
|
|
22
|
-
import { serveStaticDir } from "react-cosmos";
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { fileExists, getCliArgs, importModule, moduleExists, serveStaticDir } from "react-cosmos";
|
|
23
4
|
import webpackHotMiddleware from "webpack-hot-middleware";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
5
|
+
import rspack from "@rspack/core";
|
|
6
|
+
//#region src/server/rspackConfig/constants.ts
|
|
7
|
+
const RENDERER_FILENAME = "renderer.html";
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/server/rspackConfigPlugin.ts
|
|
10
|
+
async function rspackConfigPlugin({ config }) {
|
|
11
|
+
if (config.rendererUrl) return config;
|
|
12
|
+
return {
|
|
13
|
+
...config,
|
|
14
|
+
rendererUrl: path.join(config.publicUrl, RENDERER_FILENAME)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/server/utils/isNodeError.ts
|
|
33
19
|
function isNodeError(err) {
|
|
34
|
-
|
|
20
|
+
return err && err.stack && err.message;
|
|
35
21
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
import { createRequire } from "module";
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/server/utils/resolve.ts
|
|
39
24
|
function resolve(moduleId) {
|
|
40
|
-
|
|
41
|
-
return require2.resolve(moduleId);
|
|
25
|
+
return createRequire(import.meta.url).resolve(moduleId);
|
|
42
26
|
}
|
|
43
27
|
function resolveFrom(fromDirectory, moduleId) {
|
|
44
|
-
|
|
45
|
-
return require2.resolve(moduleId, { paths: [fromDirectory] });
|
|
28
|
+
return createRequire(import.meta.url).resolve(moduleId, { paths: [fromDirectory] });
|
|
46
29
|
}
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/server/utils/resolveSilent.ts
|
|
49
32
|
function resolveFromSilent(fromDirectory, moduleId) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
33
|
+
try {
|
|
34
|
+
return resolveFrom(fromDirectory, moduleId);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (!isNodeError(err) || err.code !== "MODULE_NOT_FOUND") console.log(err);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/server/utils/resolveLoose.ts
|
|
59
42
|
function resolveLoose(fromDirectory, moduleId) {
|
|
60
|
-
|
|
61
|
-
// 1. Don't start with ./
|
|
62
|
-
// 2. Don't point to an existing file
|
|
63
|
-
path2.join(fromDirectory, moduleId);
|
|
43
|
+
return path.isAbsolute(moduleId) ? moduleId : resolveFromSilent(fromDirectory, moduleId) || path.join(fromDirectory, moduleId);
|
|
64
44
|
}
|
|
65
|
-
|
|
66
|
-
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/server/cosmosConfig/createRspackCosmosConfig.ts
|
|
67
47
|
function createRspackCosmosConfig(cosmosConfig) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
48
|
+
const { rootDir } = cosmosConfig;
|
|
49
|
+
const configInput = cosmosConfig.rspack || {};
|
|
50
|
+
return {
|
|
51
|
+
configPath: getRspackConfigPath(configInput, rootDir),
|
|
52
|
+
overridePath: getRspackOverridePath(configInput, rootDir),
|
|
53
|
+
includeHashInOutputFilename: getIncludeHashInOutputFilename(configInput),
|
|
54
|
+
hotReload: getHotReload(configInput),
|
|
55
|
+
reloadOnFail: getReloadOnFail(configInput)
|
|
56
|
+
};
|
|
77
57
|
}
|
|
78
58
|
function getRspackConfigPath({ configPath }, rootDir) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const relPath = path3.relative(process.cwd(), absPath);
|
|
88
|
-
throw new Error(`rspack config not found at path: ${relPath}`);
|
|
89
|
-
}
|
|
90
|
-
return absPath;
|
|
59
|
+
if (typeof configPath === "undefined") return resolveLoose(rootDir, "rspack.config.js");
|
|
60
|
+
if (!configPath) return null;
|
|
61
|
+
const absPath = resolveLoose(rootDir, configPath);
|
|
62
|
+
if (!fileExists(absPath)) {
|
|
63
|
+
const relPath = path.relative(process.cwd(), absPath);
|
|
64
|
+
throw new Error(`rspack config not found at path: ${relPath}`);
|
|
65
|
+
}
|
|
66
|
+
return absPath;
|
|
91
67
|
}
|
|
92
68
|
function getRspackOverridePath({ overridePath }, rootDir) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return absPath;
|
|
105
|
-
}
|
|
106
|
-
function getIncludeHashInOutputFilename({
|
|
107
|
-
includeHashInOutputFilename = false
|
|
108
|
-
}) {
|
|
109
|
-
return includeHashInOutputFilename;
|
|
69
|
+
if (typeof overridePath === "undefined") return resolveLoose(rootDir, "rspack.override.js");
|
|
70
|
+
if (!overridePath) return null;
|
|
71
|
+
const absPath = resolveLoose(rootDir, overridePath);
|
|
72
|
+
if (!fileExists(absPath)) {
|
|
73
|
+
const relPath = path.relative(process.cwd(), absPath);
|
|
74
|
+
throw new Error(`rspack override module not found at path: ${relPath}`);
|
|
75
|
+
}
|
|
76
|
+
return absPath;
|
|
77
|
+
}
|
|
78
|
+
function getIncludeHashInOutputFilename({ includeHashInOutputFilename = false }) {
|
|
79
|
+
return includeHashInOutputFilename;
|
|
110
80
|
}
|
|
111
81
|
function getHotReload({ hotReload = true }) {
|
|
112
|
-
|
|
82
|
+
return hotReload;
|
|
113
83
|
}
|
|
114
84
|
function getReloadOnFail({ reloadOnFail = false }) {
|
|
115
|
-
|
|
85
|
+
return reloadOnFail;
|
|
116
86
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
import { createRequire as createRequire2 } from "module";
|
|
120
|
-
import * as path4 from "path";
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/server/utils/requireModule.ts
|
|
121
89
|
function requireFrom(fromDirectory, moduleId) {
|
|
122
|
-
|
|
123
|
-
return require2(moduleId);
|
|
90
|
+
return createRequire(path.resolve(fromDirectory, "noop.js"))(moduleId);
|
|
124
91
|
}
|
|
125
|
-
|
|
126
|
-
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/server/utils/requireSilent.ts
|
|
127
94
|
function requireFromSilent(fromDirectory, moduleId) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
// src/server/getRspack.ts
|
|
95
|
+
try {
|
|
96
|
+
return requireFrom(fromDirectory, moduleId);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
if (!isNodeError(err) || err.code !== "MODULE_NOT_FOUND") console.log(err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/server/getRspack.ts
|
|
138
103
|
function getRspack(rootDir) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
return userRspack.rspack;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// src/server/rspackConfig/getDevRspackConfig.ts
|
|
154
|
-
import * as path7 from "path";
|
|
155
|
-
|
|
156
|
-
// src/server/rspackConfig/getUserRspackConfig.ts
|
|
157
|
-
import * as path5 from "path";
|
|
158
|
-
import {
|
|
159
|
-
getCliArgs,
|
|
160
|
-
importModule,
|
|
161
|
-
moduleExists
|
|
162
|
-
} from "react-cosmos";
|
|
163
|
-
|
|
164
|
-
// src/server/rspackConfig/getDefaultRspackConfig.ts
|
|
165
|
-
import rspack from "@rspack/core";
|
|
166
|
-
|
|
167
|
-
// src/server/rspackConfig/getRspackNodeEnv.ts
|
|
104
|
+
const userRspack = requireFromSilent(rootDir, "@rspack/core");
|
|
105
|
+
if (!userRspack) {
|
|
106
|
+
console.warn("[Cosmos] rspack dependency missing!");
|
|
107
|
+
console.log("Install using \"yarn add --dev @rspack/core\" or \"npm install --save-dev @rspack/core\"");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
return userRspack.rspack;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/server/rspackConfig/getRspackNodeEnv.ts
|
|
168
114
|
function getRspackNodeEnv() {
|
|
169
|
-
|
|
115
|
+
return process.env.NODE_ENV === "production" ? "production" : "development";
|
|
170
116
|
}
|
|
171
|
-
|
|
172
|
-
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/server/rspackConfig/getDefaultRspackConfig.ts
|
|
173
119
|
function getDefaultRspackConfig(rootDir) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
mode: getRspackNodeEnv(),
|
|
248
|
-
optimization: {
|
|
249
|
-
// Cosmos reads component names at run-time, so it is crucial to not
|
|
250
|
-
// minify even when building with production env (ie. when exporting)
|
|
251
|
-
// https://github.com/react-cosmos/react-cosmos/issues/701
|
|
252
|
-
minimize: false
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// src/server/rspackConfig/getUserRspackConfig.ts
|
|
120
|
+
const postcssLoaderPath = resolveFromSilent(rootDir, "postcss-loader");
|
|
121
|
+
const mdxLoaderPath = resolveFromSilent(rootDir, "@mdx-js/loader");
|
|
122
|
+
const rules = [];
|
|
123
|
+
const plugins = [];
|
|
124
|
+
rules.push({
|
|
125
|
+
test: /\.(j|t)s$/,
|
|
126
|
+
exclude: [/[\\/]node_modules[\\/]/],
|
|
127
|
+
loader: "builtin:swc-loader",
|
|
128
|
+
options: {
|
|
129
|
+
sourceMaps: true,
|
|
130
|
+
jsc: { parser: { syntax: "typescript" } }
|
|
131
|
+
},
|
|
132
|
+
type: "javascript/auto"
|
|
133
|
+
});
|
|
134
|
+
rules.push({
|
|
135
|
+
test: /\.(j|t)sx$/,
|
|
136
|
+
loader: "builtin:swc-loader",
|
|
137
|
+
exclude: [/[\\/]node_modules[\\/]/],
|
|
138
|
+
options: {
|
|
139
|
+
sourceMaps: true,
|
|
140
|
+
jsc: {
|
|
141
|
+
parser: {
|
|
142
|
+
syntax: "typescript",
|
|
143
|
+
tsx: true
|
|
144
|
+
},
|
|
145
|
+
transform: { react: { runtime: "automatic" } }
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
type: "javascript/auto"
|
|
149
|
+
});
|
|
150
|
+
rules.push({
|
|
151
|
+
test: /\.css$/,
|
|
152
|
+
use: postcssLoaderPath ? [{ loader: postcssLoaderPath }] : [{ loader: "builtin:lightningcss-loader" }],
|
|
153
|
+
type: "css/auto"
|
|
154
|
+
});
|
|
155
|
+
if (mdxLoaderPath) rules.push({
|
|
156
|
+
test: /\.mdx$/,
|
|
157
|
+
loader: mdxLoaderPath,
|
|
158
|
+
exclude: [/[\\/]node_modules[\\/]/]
|
|
159
|
+
});
|
|
160
|
+
plugins.push(new rspack.HtmlRspackPlugin({
|
|
161
|
+
title: "React Cosmos",
|
|
162
|
+
filename: RENDERER_FILENAME
|
|
163
|
+
}));
|
|
164
|
+
return {
|
|
165
|
+
devtool: "cheap-module-source-map",
|
|
166
|
+
resolve: {
|
|
167
|
+
extensionAlias: { ".js": [
|
|
168
|
+
".ts",
|
|
169
|
+
".tsx",
|
|
170
|
+
".js"
|
|
171
|
+
] },
|
|
172
|
+
extensions: [
|
|
173
|
+
".js",
|
|
174
|
+
".jsx",
|
|
175
|
+
".ts",
|
|
176
|
+
".tsx"
|
|
177
|
+
]
|
|
178
|
+
},
|
|
179
|
+
module: { rules },
|
|
180
|
+
plugins,
|
|
181
|
+
stats: {
|
|
182
|
+
preset: "errors-only",
|
|
183
|
+
builtAt: true
|
|
184
|
+
},
|
|
185
|
+
infrastructureLogging: { level: "warn" },
|
|
186
|
+
experiments: { topLevelAwait: true },
|
|
187
|
+
mode: getRspackNodeEnv(),
|
|
188
|
+
optimization: { minimize: false }
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/server/rspackConfig/getUserRspackConfig.ts
|
|
258
193
|
async function getUserRspackConfig(config) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const module = await importModule(overridePath);
|
|
270
|
-
const rspackOverride = module.default;
|
|
271
|
-
return rspackOverride(baseRspackConfig, getRspackNodeEnv());
|
|
194
|
+
const baseRspackConfig = await getBaseRspackConfig(config);
|
|
195
|
+
const { overridePath } = createRspackCosmosConfig(config);
|
|
196
|
+
if (!overridePath || !moduleExists(overridePath)) {
|
|
197
|
+
console.log(`[Cosmos] Learn how to override rspack config for cosmos: https://github.com/react-cosmos/react-cosmos/tree/main/docs#webpack-config-override`);
|
|
198
|
+
return baseRspackConfig;
|
|
199
|
+
}
|
|
200
|
+
const relPath = path.relative(process.cwd(), overridePath);
|
|
201
|
+
console.log(`[Cosmos] Overriding rspack config at ${relPath}`);
|
|
202
|
+
const rspackOverride = (await importModule(overridePath)).default;
|
|
203
|
+
return rspackOverride(baseRspackConfig, getRspackNodeEnv());
|
|
272
204
|
}
|
|
273
205
|
async function getBaseRspackConfig(confg) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
// but that doesn't seem to be what webpack does:
|
|
289
|
-
//
|
|
290
|
-
// https://github.com/webpack/webpack-cli/blob/79a969fb02c870667d8a3b7035405566d2b4d088/packages/webpack-cli/src/webpack-cli.ts#L1903C35-L1903C43
|
|
291
|
-
await rspackConfig(cliArgs.env || {}, cliArgs)
|
|
292
|
-
) : rspackConfig;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// src/server/rspackConfig/resolveRspackClientPath.ts
|
|
296
|
-
import { createRequire as createRequire3 } from "module";
|
|
206
|
+
const { rootDir } = confg;
|
|
207
|
+
const { configPath } = createRspackCosmosConfig(confg);
|
|
208
|
+
if (!configPath || !moduleExists(configPath)) {
|
|
209
|
+
console.log("[Cosmos] Using default rspack config");
|
|
210
|
+
return getDefaultRspackConfig(rootDir);
|
|
211
|
+
}
|
|
212
|
+
const relPath = path.relative(process.cwd(), configPath);
|
|
213
|
+
console.log(`[Cosmos] Using rspack config found at ${relPath}`);
|
|
214
|
+
const rspackConfig = (await importModule(configPath)).default;
|
|
215
|
+
const cliArgs = getCliArgs();
|
|
216
|
+
return typeof rspackConfig === "function" ? await rspackConfig(cliArgs.env || {}, cliArgs) : rspackConfig;
|
|
217
|
+
}
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/server/rspackConfig/resolveRspackClientPath.ts
|
|
297
220
|
function resolveRspackClientPath(relPath) {
|
|
298
|
-
|
|
299
|
-
return require2.resolve(`../client/${relPath}`);
|
|
221
|
+
return createRequire(import.meta.url).resolve(`../client/${relPath}`);
|
|
300
222
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
import { createRequire as createRequire4 } from "module";
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/server/rspackConfig/resolveRspackLoaderPath.ts
|
|
304
225
|
function resolveRspackLoaderPath() {
|
|
305
|
-
|
|
306
|
-
return require2.resolve("./userImportsLoader.cjs");
|
|
226
|
+
return createRequire(import.meta.url).resolve("./userImportsLoader.cjs");
|
|
307
227
|
}
|
|
308
|
-
|
|
309
|
-
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/server/rspackConfig/getRspackConfigModule.ts
|
|
310
230
|
function getRspackConfigModule(config, rspackConfig, mode) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
231
|
+
return {
|
|
232
|
+
...rspackConfig.module,
|
|
233
|
+
rules: getRules(config, rspackConfig, mode)
|
|
234
|
+
};
|
|
315
235
|
}
|
|
316
236
|
function getRules(config, { module }, mode) {
|
|
317
|
-
|
|
318
|
-
return [...existingRules, getUserImportsLoaderRule(config, mode)];
|
|
237
|
+
return [...module && module.rules || [], getUserImportsLoaderRule(config, mode)];
|
|
319
238
|
}
|
|
320
239
|
function getUserImportsLoaderRule(config, mode) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
240
|
+
return {
|
|
241
|
+
include: resolveRspackClientPath("userImports"),
|
|
242
|
+
use: {
|
|
243
|
+
loader: resolveRspackLoaderPath(),
|
|
244
|
+
options: {
|
|
245
|
+
config,
|
|
246
|
+
mode
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/server/rspackConfig/getRspackConfigResolve.ts
|
|
332
253
|
function getRspackConfigResolve(config, rspackConfig) {
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
function resolveLocalReactDeps(config,
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
return { ...resolve6, alias };
|
|
254
|
+
return resolveLocalReactDeps(config, rspackConfig.resolve);
|
|
255
|
+
}
|
|
256
|
+
function resolveLocalReactDeps(config, resolve = {}) {
|
|
257
|
+
const { rootDir } = config;
|
|
258
|
+
let alias = resolve.alias || {};
|
|
259
|
+
let reactAlias = hasAlias(alias, "react");
|
|
260
|
+
let reactDomAlias = hasAlias(alias, "react-dom");
|
|
261
|
+
if (reactAlias && reactDomAlias) {
|
|
262
|
+
console.log("[Cosmos] React and React DOM aliases found in rspack config");
|
|
263
|
+
return resolve;
|
|
264
|
+
}
|
|
265
|
+
if (reactAlias) console.log("[Cosmos] React alias found in rspack config");
|
|
266
|
+
else {
|
|
267
|
+
const reactPath = resolveFromSilent(rootDir, "react");
|
|
268
|
+
if (!reactPath) throw new Error(`[Cosmos] Local dependency not found: react`);
|
|
269
|
+
alias = addAlias(alias, "react", path.dirname(reactPath));
|
|
270
|
+
}
|
|
271
|
+
if (reactDomAlias) console.log("[Cosmos] React DOM alias found in rspack config");
|
|
272
|
+
else {
|
|
273
|
+
const reactDomPath = resolveFromSilent(rootDir, "react-dom");
|
|
274
|
+
if (!reactDomPath) throw new Error(`[Cosmos] Local dependency not found: react-dom`);
|
|
275
|
+
alias = addAlias(alias, "react-dom", path.dirname(reactDomPath));
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
...resolve,
|
|
279
|
+
alias
|
|
280
|
+
};
|
|
361
281
|
}
|
|
362
282
|
function hasAlias(alias, name) {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
283
|
+
if (!alias) return false;
|
|
284
|
+
const exactName = `${name}$`;
|
|
285
|
+
const keys = Object.keys(alias);
|
|
286
|
+
return keys.includes(name) || keys.includes(exactName);
|
|
367
287
|
}
|
|
368
288
|
function addAlias(alias, name, value) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
289
|
+
return {
|
|
290
|
+
...alias,
|
|
291
|
+
[name]: value
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/server/utils/omit.ts
|
|
296
|
+
/**
|
|
297
|
+
* A helper to strip certain fields from an object.
|
|
298
|
+
*/
|
|
376
299
|
function omit(o, fields) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
// src/server/rspackConfig/plugins.ts
|
|
300
|
+
const result = { ...o };
|
|
301
|
+
for (const field of fields) delete result[field];
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/server/rspackConfig/plugins.ts
|
|
385
306
|
function getGlobalsPlugin({ publicUrl }, userRspack, devServerOn) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
});
|
|
307
|
+
const cleanPublicUrl = removeTrailingSlash(publicUrl);
|
|
308
|
+
return new userRspack.DefinePlugin({
|
|
309
|
+
__DEV__: JSON.stringify(devServerOn),
|
|
310
|
+
"process.env.NODE_ENV": JSON.stringify(getRspackNodeEnv()),
|
|
311
|
+
"process.env.PUBLIC_URL": JSON.stringify(cleanPublicUrl)
|
|
312
|
+
});
|
|
393
313
|
}
|
|
394
314
|
function hasPlugin(plugins, pluginName) {
|
|
395
|
-
|
|
315
|
+
return plugins && plugins.filter((p) => isInstanceOfRspackPlugin(p, pluginName)).length > 0;
|
|
396
316
|
}
|
|
397
317
|
function isInstanceOfRspackPlugin(plugin, constructorName) {
|
|
398
|
-
|
|
318
|
+
return plugin.constructor && plugin.constructor.name === constructorName;
|
|
399
319
|
}
|
|
400
320
|
function ignoreEmptyRspackPlugins(plugins = []) {
|
|
401
|
-
|
|
321
|
+
return plugins.filter(Boolean);
|
|
402
322
|
}
|
|
403
323
|
function removeTrailingSlash(url) {
|
|
404
|
-
|
|
324
|
+
return url.replace(/\/$/, "");
|
|
405
325
|
}
|
|
406
|
-
|
|
407
|
-
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/server/rspackConfig/htmlPlugin.ts
|
|
408
328
|
function ensureHtmlPlugin(plugins) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
})
|
|
424
|
-
];
|
|
425
|
-
}
|
|
426
|
-
return [
|
|
427
|
-
...plugins,
|
|
428
|
-
new rspack2.HtmlRspackPlugin({
|
|
429
|
-
title: "React Cosmos",
|
|
430
|
-
filename: RENDERER_FILENAME
|
|
431
|
-
})
|
|
432
|
-
];
|
|
329
|
+
if (hasPlugin(plugins, "HtmlWebpackPlugin")) return plugins.map((plugin) => isHtmlWebpackPlugin(plugin) ? changeHtmlPluginFilename(plugin) : plugin);
|
|
330
|
+
const htmlRspackPlugin = plugins.find(isHtmlRspackPlugin);
|
|
331
|
+
if (htmlRspackPlugin) {
|
|
332
|
+
const options = htmlRspackPlugin._args[0];
|
|
333
|
+
const safeOptions = omit(options, ["chunks"]);
|
|
334
|
+
return [...plugins.filter((plugin) => plugin !== htmlRspackPlugin), new rspack.HtmlRspackPlugin({
|
|
335
|
+
...safeOptions,
|
|
336
|
+
filename: RENDERER_FILENAME
|
|
337
|
+
})];
|
|
338
|
+
}
|
|
339
|
+
return [...plugins, new rspack.HtmlRspackPlugin({
|
|
340
|
+
title: "React Cosmos",
|
|
341
|
+
filename: RENDERER_FILENAME
|
|
342
|
+
})];
|
|
433
343
|
}
|
|
434
344
|
function isHtmlWebpackPlugin(plugin) {
|
|
435
|
-
|
|
345
|
+
return isInstanceOfRspackPlugin(plugin, "HtmlWebpackPlugin");
|
|
436
346
|
}
|
|
437
347
|
function isHtmlRspackPlugin(plugin) {
|
|
438
|
-
|
|
348
|
+
return isInstanceOfRspackPlugin(plugin, "HtmlRspackPlugin");
|
|
439
349
|
}
|
|
440
350
|
function changeHtmlPluginFilename(htmlPlugin) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
...safeOptions,
|
|
448
|
-
filename: RENDERER_FILENAME
|
|
449
|
-
});
|
|
351
|
+
if (!isIndexHtmlWebpackPlugin(htmlPlugin)) return htmlPlugin;
|
|
352
|
+
const safeOptions = omit(htmlPlugin.userOptions || htmlPlugin.options, ["chunks"]);
|
|
353
|
+
return new htmlPlugin.constructor({
|
|
354
|
+
...safeOptions,
|
|
355
|
+
filename: RENDERER_FILENAME
|
|
356
|
+
});
|
|
450
357
|
}
|
|
451
358
|
function isIndexHtmlWebpackPlugin(htmlPlugin) {
|
|
452
|
-
|
|
453
|
-
|
|
359
|
+
const { filename } = htmlPlugin.userOptions || htmlPlugin.options;
|
|
360
|
+
return filename === "index.html" || typeof filename !== "string" || filename.endsWith("/index.html");
|
|
454
361
|
}
|
|
455
|
-
|
|
456
|
-
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/server/rspackConfig/rspackConfigTopLevelAwait.ts
|
|
457
364
|
function ensureRspackConfigTopLevelAwait(baseWebpackConfig) {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
return experiments;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
// src/server/rspackConfig/getDevRspackConfig.ts
|
|
466
|
-
async function getDevRspackConfig(config, userRspack) {
|
|
467
|
-
const baseRspackConfig = await getUserRspackConfig(config);
|
|
468
|
-
const rspackConfig = {
|
|
469
|
-
...baseRspackConfig,
|
|
470
|
-
entry: getEntry(config),
|
|
471
|
-
output: getOutput(config),
|
|
472
|
-
module: getRspackConfigModule(config, baseRspackConfig, "dev"),
|
|
473
|
-
resolve: getRspackConfigResolve(config, baseRspackConfig),
|
|
474
|
-
plugins: getPlugins(config, baseRspackConfig, userRspack),
|
|
475
|
-
experiments: getExperiments(baseRspackConfig)
|
|
476
|
-
};
|
|
477
|
-
if (rspackConfig.optimization?.splitChunks) {
|
|
478
|
-
const { name } = rspackConfig.optimization.splitChunks;
|
|
479
|
-
if (name === false) delete rspackConfig.optimization.splitChunks.name;
|
|
480
|
-
}
|
|
481
|
-
return rspackConfig;
|
|
482
|
-
}
|
|
483
|
-
function getEntry(config) {
|
|
484
|
-
const { hotReload, reloadOnFail } = createRspackCosmosConfig(config);
|
|
485
|
-
const devtoolsHook = resolveRspackClientPath("reactDevtoolsHook");
|
|
486
|
-
const clientIndex = resolveRspackClientPath("index");
|
|
487
|
-
return hotReload ? [devtoolsHook, getHotMiddlewareEntry(reloadOnFail), clientIndex] : [devtoolsHook, clientIndex];
|
|
488
|
-
}
|
|
489
|
-
function getOutput({ publicUrl }) {
|
|
490
|
-
const filename = "[name].js";
|
|
491
|
-
return {
|
|
492
|
-
filename,
|
|
493
|
-
publicPath: publicUrl,
|
|
494
|
-
// Enable click-to-open source in react-error-overlay
|
|
495
|
-
devtoolModuleFilenameTemplate: (info) => path7.resolve(info.absoluteResourcePath).replace(/\\/g, "/")
|
|
496
|
-
};
|
|
365
|
+
const experiments = baseWebpackConfig.experiments || {};
|
|
366
|
+
if (!experiments.topLevelAwait) experiments.topLevelAwait = true;
|
|
367
|
+
return experiments;
|
|
497
368
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region src/server/rspackConfig/getDevRspackConfig.ts
|
|
371
|
+
async function getDevRspackConfig(config, userRspack) {
|
|
372
|
+
const baseRspackConfig = await getUserRspackConfig(config);
|
|
373
|
+
const rspackConfig = {
|
|
374
|
+
...baseRspackConfig,
|
|
375
|
+
entry: getEntry$1(config),
|
|
376
|
+
output: getOutput$1(config),
|
|
377
|
+
module: getRspackConfigModule(config, baseRspackConfig, "dev"),
|
|
378
|
+
resolve: getRspackConfigResolve(config, baseRspackConfig),
|
|
379
|
+
plugins: getPlugins$1(config, baseRspackConfig, userRspack),
|
|
380
|
+
experiments: getExperiments$1(baseRspackConfig)
|
|
381
|
+
};
|
|
382
|
+
if (rspackConfig.optimization?.splitChunks) {
|
|
383
|
+
const { name } = rspackConfig.optimization.splitChunks;
|
|
384
|
+
if (name === false) delete rspackConfig.optimization.splitChunks.name;
|
|
385
|
+
}
|
|
386
|
+
return rspackConfig;
|
|
387
|
+
}
|
|
388
|
+
function getEntry$1(config) {
|
|
389
|
+
const { hotReload, reloadOnFail } = createRspackCosmosConfig(config);
|
|
390
|
+
const devtoolsHook = resolveRspackClientPath("reactDevtoolsHook");
|
|
391
|
+
const clientIndex = resolveRspackClientPath("index");
|
|
392
|
+
return hotReload ? [
|
|
393
|
+
devtoolsHook,
|
|
394
|
+
getHotMiddlewareEntry(reloadOnFail),
|
|
395
|
+
clientIndex
|
|
396
|
+
] : [devtoolsHook, clientIndex];
|
|
397
|
+
}
|
|
398
|
+
function getOutput$1({ publicUrl }) {
|
|
399
|
+
return {
|
|
400
|
+
filename: "[name].js",
|
|
401
|
+
publicPath: publicUrl,
|
|
402
|
+
devtoolModuleFilenameTemplate: (info) => path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function getPlugins$1(config, baseRspackConfig, userRspack) {
|
|
406
|
+
const existingPlugins = ignoreEmptyRspackPlugins(baseRspackConfig.plugins);
|
|
407
|
+
const globalsPlugin = getGlobalsPlugin(config, userRspack, true);
|
|
408
|
+
const noEmitErrorsPlugin = new userRspack.NoEmitOnErrorsPlugin();
|
|
409
|
+
let plugins = [
|
|
410
|
+
...existingPlugins,
|
|
411
|
+
globalsPlugin,
|
|
412
|
+
noEmitErrorsPlugin
|
|
413
|
+
];
|
|
414
|
+
const { hotReload } = createRspackCosmosConfig(config);
|
|
415
|
+
if (hotReload && !hasPlugin(plugins, "HotModuleReplacementPlugin")) {
|
|
416
|
+
const hmrPlugin = new userRspack.HotModuleReplacementPlugin();
|
|
417
|
+
plugins = [...plugins, hmrPlugin];
|
|
418
|
+
}
|
|
419
|
+
return ensureHtmlPlugin(plugins);
|
|
509
420
|
}
|
|
510
421
|
function getHotMiddlewareEntry(reloadOnFail) {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
sendBuildMessage({ type: "buildStart" });
|
|
558
|
-
});
|
|
559
|
-
rspackCompiler.hooks.failed.tap("Cosmos", () => {
|
|
560
|
-
sendBuildMessage({ type: "buildError" });
|
|
561
|
-
});
|
|
562
|
-
const onCompilationDone = new Promise((resolve6) => {
|
|
563
|
-
rspackCompiler.hooks.done.tap("Cosmos", (stats) => {
|
|
564
|
-
resolve6();
|
|
565
|
-
if (stats.hasErrors()) {
|
|
566
|
-
sendBuildMessage({ type: "buildError" });
|
|
567
|
-
} else {
|
|
568
|
-
sendBuildMessage({ type: "buildDone" });
|
|
569
|
-
}
|
|
570
|
-
});
|
|
571
|
-
});
|
|
572
|
-
console.log("[Cosmos] Building rspack...");
|
|
573
|
-
const wdmModule = await import("webpack-dev-middleware");
|
|
574
|
-
const wdmInst = wdmModule.default(
|
|
575
|
-
// Rspack's Compiler type is pretty close to webpack's Compiler type, but
|
|
576
|
-
// is missing a few members. Hopefully WDM doesn't need them, however.
|
|
577
|
-
// @rspack/dev-server itself uses webpack-dev-middleware so it should be
|
|
578
|
-
// fine.
|
|
579
|
-
rspackCompiler,
|
|
580
|
-
{
|
|
581
|
-
// publicPath is the base path for the rspack assets and has to match
|
|
582
|
-
// rspack.output.publicPath
|
|
583
|
-
publicPath: config.publicUrl
|
|
584
|
-
}
|
|
585
|
-
);
|
|
586
|
-
app.use(wdmInst);
|
|
587
|
-
const { hotReload } = createRspackCosmosConfig(config);
|
|
588
|
-
if (hotReload) {
|
|
589
|
-
app.use(
|
|
590
|
-
webpackHotMiddleware(
|
|
591
|
-
// As above, rspack's Compiler type is pretty close to webpack's
|
|
592
|
-
// Compiler type so this should be fine.
|
|
593
|
-
rspackCompiler
|
|
594
|
-
)
|
|
595
|
-
);
|
|
596
|
-
}
|
|
597
|
-
await onCompilationDone;
|
|
598
|
-
return async () => {
|
|
599
|
-
await new Promise((res) => wdmInst.close(res));
|
|
600
|
-
};
|
|
422
|
+
return `${resolve("webpack-hot-middleware/client")}?reload=${reloadOnFail}&overlay=false`;
|
|
423
|
+
}
|
|
424
|
+
function getExperiments$1(baseWebpackConfig) {
|
|
425
|
+
return ensureRspackConfigTopLevelAwait(baseWebpackConfig);
|
|
426
|
+
}
|
|
427
|
+
//#endregion
|
|
428
|
+
//#region src/server/rspackDevServerPlugin.ts
|
|
429
|
+
async function rspackDevServerPlugin({ config, platform, app, sendMessage }) {
|
|
430
|
+
if (platform !== "web") return;
|
|
431
|
+
const userRspack = getRspack(config.rootDir);
|
|
432
|
+
if (!userRspack) return;
|
|
433
|
+
const rspackConfig = await getDevRspackConfig(config, userRspack);
|
|
434
|
+
if (config.staticPath === null) {
|
|
435
|
+
const rspackDerivedStaticPath = getRspackStaticPath(rspackConfig);
|
|
436
|
+
if (rspackDerivedStaticPath !== null) serveStaticDir(app, path.resolve(config.rootDir, rspackDerivedStaticPath), config.publicUrl);
|
|
437
|
+
}
|
|
438
|
+
function sendBuildMessage(msg) {
|
|
439
|
+
sendMessage(msg);
|
|
440
|
+
}
|
|
441
|
+
const rspackCompiler = userRspack(rspackConfig);
|
|
442
|
+
rspackCompiler.hooks.invalid.tap("Cosmos", (filePath) => {
|
|
443
|
+
if (typeof filePath === "string") {
|
|
444
|
+
const relFilePath = path.relative(process.cwd(), filePath);
|
|
445
|
+
console.log("[Cosmos] rspack build invalidated by", relFilePath);
|
|
446
|
+
} else console.log("[Cosmos] rspack build invalidated by unknown file");
|
|
447
|
+
sendBuildMessage({ type: "buildStart" });
|
|
448
|
+
});
|
|
449
|
+
rspackCompiler.hooks.failed.tap("Cosmos", () => {
|
|
450
|
+
sendBuildMessage({ type: "buildError" });
|
|
451
|
+
});
|
|
452
|
+
const onCompilationDone = new Promise((resolve) => {
|
|
453
|
+
rspackCompiler.hooks.done.tap("Cosmos", (stats) => {
|
|
454
|
+
resolve();
|
|
455
|
+
if (stats.hasErrors()) sendBuildMessage({ type: "buildError" });
|
|
456
|
+
else sendBuildMessage({ type: "buildDone" });
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
console.log("[Cosmos] Building rspack...");
|
|
460
|
+
const wdmInst = (await import("webpack-dev-middleware")).default(rspackCompiler, { publicPath: config.publicUrl });
|
|
461
|
+
app.use(wdmInst);
|
|
462
|
+
const { hotReload } = createRspackCosmosConfig(config);
|
|
463
|
+
if (hotReload) app.use(webpackHotMiddleware(rspackCompiler));
|
|
464
|
+
await onCompilationDone;
|
|
465
|
+
return async () => {
|
|
466
|
+
await new Promise((res) => wdmInst.close(res));
|
|
467
|
+
};
|
|
601
468
|
}
|
|
602
469
|
function getRspackStaticPath({ devServer }) {
|
|
603
|
-
|
|
470
|
+
return devServer && typeof devServer.contentBase === "string" ? devServer.contentBase : null;
|
|
604
471
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
import * as path9 from "path";
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/server/rspackConfig/getExportRspackConfig.ts
|
|
608
474
|
async function getExportRspackConfig(config, userRspack) {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
620
|
-
function
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
// src/server/rspackExportPlugin.ts
|
|
475
|
+
const baseRspackConfig = await getUserRspackConfig(config);
|
|
476
|
+
return {
|
|
477
|
+
...baseRspackConfig,
|
|
478
|
+
entry: getEntry(),
|
|
479
|
+
output: getOutput(config),
|
|
480
|
+
module: getRspackConfigModule(config, baseRspackConfig, "export"),
|
|
481
|
+
resolve: getRspackConfigResolve(config, baseRspackConfig),
|
|
482
|
+
plugins: getPlugins(config, baseRspackConfig, userRspack),
|
|
483
|
+
experiments: getExperiments(baseRspackConfig)
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
function getEntry() {
|
|
487
|
+
return [resolveRspackClientPath("reactDevtoolsHook"), resolveRspackClientPath("index")];
|
|
488
|
+
}
|
|
489
|
+
function getOutput(config) {
|
|
490
|
+
const { exportPath, publicUrl } = config;
|
|
491
|
+
const { includeHashInOutputFilename } = createRspackCosmosConfig(config);
|
|
492
|
+
return {
|
|
493
|
+
path: path.join(exportPath, publicUrl),
|
|
494
|
+
filename: includeHashInOutputFilename ? "[name].[contenthash].js" : "[name].js",
|
|
495
|
+
publicPath: publicUrl
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function getPlugins(config, baseRspackConfig, userRspack) {
|
|
499
|
+
const existingPlugins = ignoreEmptyRspackPlugins(baseRspackConfig.plugins);
|
|
500
|
+
const globalsPlugin = getGlobalsPlugin(config, userRspack, false);
|
|
501
|
+
const noEmitErrorsPlugin = new userRspack.NoEmitOnErrorsPlugin();
|
|
502
|
+
return ensureHtmlPlugin([
|
|
503
|
+
...existingPlugins,
|
|
504
|
+
globalsPlugin,
|
|
505
|
+
noEmitErrorsPlugin
|
|
506
|
+
]);
|
|
507
|
+
}
|
|
508
|
+
function getExperiments(baseWebpackConfig) {
|
|
509
|
+
return ensureRspackConfigTopLevelAwait(baseWebpackConfig);
|
|
510
|
+
}
|
|
511
|
+
//#endregion
|
|
512
|
+
//#region src/server/rspackExportPlugin.ts
|
|
649
513
|
async function rspackExportPlugin({ config }) {
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
`);
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
throw rspackError;
|
|
666
|
-
}
|
|
514
|
+
const userRspack = getRspack(config.rootDir);
|
|
515
|
+
if (!userRspack) return;
|
|
516
|
+
const rspackConfig = await getExportRspackConfig(config, userRspack);
|
|
517
|
+
try {
|
|
518
|
+
await runRspackCompiler(userRspack, rspackConfig);
|
|
519
|
+
} catch (err) {
|
|
520
|
+
const rspackError = err;
|
|
521
|
+
if (rspackError.rspackErrors) rspackError.rspackErrors.forEach((error) => {
|
|
522
|
+
console.error(`${error}\n`);
|
|
523
|
+
});
|
|
524
|
+
throw rspackError;
|
|
525
|
+
}
|
|
667
526
|
}
|
|
668
527
|
function runRspackCompiler(userRspack, rspackConfig) {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
resolve6(stats);
|
|
680
|
-
}
|
|
681
|
-
});
|
|
682
|
-
});
|
|
528
|
+
return new Promise((resolve, reject) => {
|
|
529
|
+
userRspack(rspackConfig).run((err, stats) => {
|
|
530
|
+
if (err) reject(err);
|
|
531
|
+
else if (stats?.hasErrors()) {
|
|
532
|
+
const error = new RspackCompilationError();
|
|
533
|
+
error.rspackErrors = stats.toJson().errors;
|
|
534
|
+
reject(error);
|
|
535
|
+
} else resolve(stats);
|
|
536
|
+
});
|
|
537
|
+
});
|
|
683
538
|
}
|
|
684
539
|
var RspackCompilationError = class extends Error {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
};
|
|
690
|
-
|
|
691
|
-
// src/server/rspackServerPlugin.ts
|
|
692
|
-
var rspackServerPlugin = {
|
|
693
|
-
name: "rspack",
|
|
694
|
-
config: rspackConfigPlugin,
|
|
695
|
-
devServer: rspackDevServerPlugin,
|
|
696
|
-
export: rspackExportPlugin
|
|
540
|
+
rspackErrors;
|
|
541
|
+
constructor() {
|
|
542
|
+
super("Rspack errors occurred");
|
|
543
|
+
}
|
|
697
544
|
};
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/server/rspackServerPlugin.ts
|
|
547
|
+
const rspackServerPlugin = {
|
|
548
|
+
name: "rspack",
|
|
549
|
+
config: rspackConfigPlugin,
|
|
550
|
+
devServer: rspackDevServerPlugin,
|
|
551
|
+
export: rspackExportPlugin
|
|
701
552
|
};
|
|
553
|
+
//#endregion
|
|
554
|
+
export { rspackServerPlugin as default };
|