react-cosmos-plugin-rspack 1.0.3 → 2.0.1
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 -2169
- package/dist/client/reactDevtoolsHook.js +0 -8
- package/dist/client/userImports.js +10 -11
- package/dist/server/rspackServerPlugin.mjs +554 -0
- package/dist/server/userImportsLoader.cjs +7 -6
- package/dist/ui/build.mjs +42 -0
- package/package.json +17 -17
- package/dist/server/rspackServerPlugin.js +0 -701
- package/dist/ui/build.js +0 -55
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// src/client/reactDevtoolsHook.ts
|
|
2
|
-
if (process.env.NODE_ENV === "development") {
|
|
3
|
-
try {
|
|
4
|
-
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.parent.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
5
|
-
} catch (err) {
|
|
6
|
-
console.warn("Could not access parent React devtools hook");
|
|
7
|
-
}
|
|
8
|
-
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region src/client/userImports.ts
|
|
2
|
+
const rendererConfig = {
|
|
3
|
+
webSocketUrl: "ws://localhost:5000",
|
|
4
|
+
rendererUrl: null
|
|
4
5
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
export {
|
|
11
|
-
moduleWrappers,
|
|
12
|
-
rendererConfig
|
|
6
|
+
const moduleWrappers = {
|
|
7
|
+
lazy: false,
|
|
8
|
+
fixtures: {},
|
|
9
|
+
decorators: {}
|
|
13
10
|
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { moduleWrappers, rendererConfig };
|
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { fileExists, getCliArgs, importModule, moduleExists, serveStaticDir } from "react-cosmos";
|
|
4
|
+
import webpackHotMiddleware from "webpack-hot-middleware";
|
|
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
|
|
19
|
+
function isNodeError(err) {
|
|
20
|
+
return err && err.stack && err.message;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/server/utils/resolve.ts
|
|
24
|
+
function resolve(moduleId) {
|
|
25
|
+
return createRequire(import.meta.url).resolve(moduleId);
|
|
26
|
+
}
|
|
27
|
+
function resolveFrom(fromDirectory, moduleId) {
|
|
28
|
+
return createRequire(import.meta.url).resolve(moduleId, { paths: [fromDirectory] });
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/server/utils/resolveSilent.ts
|
|
32
|
+
function resolveFromSilent(fromDirectory, moduleId) {
|
|
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
|
|
42
|
+
function resolveLoose(fromDirectory, moduleId) {
|
|
43
|
+
return path.isAbsolute(moduleId) ? moduleId : resolveFromSilent(fromDirectory, moduleId) || path.join(fromDirectory, moduleId);
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/server/cosmosConfig/createRspackCosmosConfig.ts
|
|
47
|
+
function createRspackCosmosConfig(cosmosConfig) {
|
|
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
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function getRspackConfigPath({ configPath }, rootDir) {
|
|
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;
|
|
67
|
+
}
|
|
68
|
+
function getRspackOverridePath({ overridePath }, rootDir) {
|
|
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;
|
|
80
|
+
}
|
|
81
|
+
function getHotReload({ hotReload = true }) {
|
|
82
|
+
return hotReload;
|
|
83
|
+
}
|
|
84
|
+
function getReloadOnFail({ reloadOnFail = false }) {
|
|
85
|
+
return reloadOnFail;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/server/utils/requireModule.ts
|
|
89
|
+
function requireFrom(fromDirectory, moduleId) {
|
|
90
|
+
return createRequire(path.resolve(fromDirectory, "noop.js"))(moduleId);
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/server/utils/requireSilent.ts
|
|
94
|
+
function requireFromSilent(fromDirectory, moduleId) {
|
|
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
|
|
103
|
+
function getRspack(rootDir) {
|
|
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
|
|
114
|
+
function getRspackNodeEnv() {
|
|
115
|
+
return process.env.NODE_ENV === "production" ? "production" : "development";
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/server/rspackConfig/getDefaultRspackConfig.ts
|
|
119
|
+
function getDefaultRspackConfig(rootDir) {
|
|
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
|
|
193
|
+
async function getUserRspackConfig(config) {
|
|
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());
|
|
204
|
+
}
|
|
205
|
+
async function getBaseRspackConfig(confg) {
|
|
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
|
|
220
|
+
function resolveRspackClientPath(relPath) {
|
|
221
|
+
return createRequire(import.meta.url).resolve(`../client/${relPath}`);
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/server/rspackConfig/resolveRspackLoaderPath.ts
|
|
225
|
+
function resolveRspackLoaderPath() {
|
|
226
|
+
return createRequire(import.meta.url).resolve("./userImportsLoader.cjs");
|
|
227
|
+
}
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/server/rspackConfig/getRspackConfigModule.ts
|
|
230
|
+
function getRspackConfigModule(config, rspackConfig, mode) {
|
|
231
|
+
return {
|
|
232
|
+
...rspackConfig.module,
|
|
233
|
+
rules: getRules(config, rspackConfig, mode)
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function getRules(config, { module }, mode) {
|
|
237
|
+
return [...module && module.rules || [], getUserImportsLoaderRule(config, mode)];
|
|
238
|
+
}
|
|
239
|
+
function getUserImportsLoaderRule(config, mode) {
|
|
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
|
|
253
|
+
function getRspackConfigResolve(config, rspackConfig) {
|
|
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
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function hasAlias(alias, name) {
|
|
283
|
+
if (!alias) return false;
|
|
284
|
+
const exactName = `${name}$`;
|
|
285
|
+
const keys = Object.keys(alias);
|
|
286
|
+
return keys.includes(name) || keys.includes(exactName);
|
|
287
|
+
}
|
|
288
|
+
function addAlias(alias, name, value) {
|
|
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
|
+
*/
|
|
299
|
+
function omit(o, fields) {
|
|
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
|
|
306
|
+
function getGlobalsPlugin({ publicUrl }, userRspack, devServerOn) {
|
|
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
|
+
});
|
|
313
|
+
}
|
|
314
|
+
function hasPlugin(plugins, pluginName) {
|
|
315
|
+
return plugins && plugins.filter((p) => isInstanceOfRspackPlugin(p, pluginName)).length > 0;
|
|
316
|
+
}
|
|
317
|
+
function isInstanceOfRspackPlugin(plugin, constructorName) {
|
|
318
|
+
return plugin.constructor && plugin.constructor.name === constructorName;
|
|
319
|
+
}
|
|
320
|
+
function ignoreEmptyRspackPlugins(plugins = []) {
|
|
321
|
+
return plugins.filter(Boolean);
|
|
322
|
+
}
|
|
323
|
+
function removeTrailingSlash(url) {
|
|
324
|
+
return url.replace(/\/$/, "");
|
|
325
|
+
}
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/server/rspackConfig/htmlPlugin.ts
|
|
328
|
+
function ensureHtmlPlugin(plugins) {
|
|
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
|
+
})];
|
|
343
|
+
}
|
|
344
|
+
function isHtmlWebpackPlugin(plugin) {
|
|
345
|
+
return isInstanceOfRspackPlugin(plugin, "HtmlWebpackPlugin");
|
|
346
|
+
}
|
|
347
|
+
function isHtmlRspackPlugin(plugin) {
|
|
348
|
+
return isInstanceOfRspackPlugin(plugin, "HtmlRspackPlugin");
|
|
349
|
+
}
|
|
350
|
+
function changeHtmlPluginFilename(htmlPlugin) {
|
|
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
|
+
});
|
|
357
|
+
}
|
|
358
|
+
function isIndexHtmlWebpackPlugin(htmlPlugin) {
|
|
359
|
+
const { filename } = htmlPlugin.userOptions || htmlPlugin.options;
|
|
360
|
+
return filename === "index.html" || typeof filename !== "string" || filename.endsWith("/index.html");
|
|
361
|
+
}
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/server/rspackConfig/rspackConfigTopLevelAwait.ts
|
|
364
|
+
function ensureRspackConfigTopLevelAwait(baseWebpackConfig) {
|
|
365
|
+
const experiments = baseWebpackConfig.experiments || {};
|
|
366
|
+
if (!experiments.topLevelAwait) experiments.topLevelAwait = true;
|
|
367
|
+
return experiments;
|
|
368
|
+
}
|
|
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);
|
|
420
|
+
}
|
|
421
|
+
function getHotMiddlewareEntry(reloadOnFail) {
|
|
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
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function getRspackStaticPath({ devServer }) {
|
|
470
|
+
return devServer && typeof devServer.contentBase === "string" ? devServer.contentBase : null;
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/server/rspackConfig/getExportRspackConfig.ts
|
|
474
|
+
async function getExportRspackConfig(config, userRspack) {
|
|
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
|
|
513
|
+
async function rspackExportPlugin({ config }) {
|
|
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
|
+
}
|
|
526
|
+
}
|
|
527
|
+
function runRspackCompiler(userRspack, rspackConfig) {
|
|
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
|
+
});
|
|
538
|
+
}
|
|
539
|
+
var RspackCompilationError = class extends Error {
|
|
540
|
+
rspackErrors;
|
|
541
|
+
constructor() {
|
|
542
|
+
super("Rspack errors occurred");
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/server/rspackServerPlugin.ts
|
|
547
|
+
const rspackServerPlugin = {
|
|
548
|
+
name: "rspack",
|
|
549
|
+
config: rspackConfigPlugin,
|
|
550
|
+
devServer: rspackDevServerPlugin,
|
|
551
|
+
export: rspackExportPlugin
|
|
552
|
+
};
|
|
553
|
+
//#endregion
|
|
554
|
+
export { rspackServerPlugin as default };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// dist folder as part of the build process
|
|
5
5
|
module.exports = async function injectUserImports() {
|
|
6
6
|
const cosmos = await import('react-cosmos');
|
|
7
|
-
const {
|
|
7
|
+
const { config, mode } = this.getOptions();
|
|
8
8
|
|
|
9
9
|
// This ensures this loader is invalidated whenever a new file is added to or
|
|
10
10
|
// removed from user's project, which in turn triggers findUserModulePaths
|
|
@@ -13,17 +13,18 @@ module.exports = async function injectUserImports() {
|
|
|
13
13
|
// of require.context, which not only watches for file changes but also
|
|
14
14
|
// automatically bundles new files that match the watcher's query.
|
|
15
15
|
// https://github.com/webpack/webpack/issues/222#issuecomment-40691546
|
|
16
|
-
const watchDirs =
|
|
16
|
+
const watchDirs = config.watchDirs;
|
|
17
17
|
watchDirs.forEach((watchDir) => this.addContextDependency(watchDir));
|
|
18
18
|
|
|
19
|
-
const { containerQuerySelector } =
|
|
20
|
-
const modulePaths = await cosmos.findUserModulePaths(
|
|
19
|
+
const { containerQuerySelector } = config.dom;
|
|
20
|
+
const modulePaths = await cosmos.findUserModulePaths(config);
|
|
21
21
|
const rendererConfig = {
|
|
22
|
-
|
|
22
|
+
webSocketUrl: mode === 'dev' ? cosmos.getWebSocketUrl(config) : null,
|
|
23
|
+
rendererUrl: null,
|
|
23
24
|
containerQuerySelector,
|
|
24
25
|
};
|
|
25
26
|
return cosmos.generateUserImports({
|
|
26
|
-
|
|
27
|
+
config,
|
|
27
28
|
modulePaths,
|
|
28
29
|
rendererConfig,
|
|
29
30
|
relativeToDir: null,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
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 __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region \0virtual:react
|
|
25
|
+
var require__virtual_react = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
26
|
+
module.exports = React;
|
|
27
|
+
}));
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region \0virtual:react-plugin
|
|
30
|
+
var require__virtual_react_plugin = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
31
|
+
module.exports = ReactPlugin;
|
|
32
|
+
}));
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/ui/WebpackRendererError.tsx
|
|
35
|
+
var import__virtual_react = /* @__PURE__ */ __toESM(require__virtual_react());
|
|
36
|
+
const { register, plug } = (0, require__virtual_react_plugin().createPlugin)({ name: "webpackRendererError" });
|
|
37
|
+
plug("rendererError", () => {
|
|
38
|
+
return /* @__PURE__ */ import__virtual_react.default.createElement(import__virtual_react.default.Fragment, null, "If you use a custom webpack config,", " ", /* @__PURE__ */ import__virtual_react.default.createElement("strong", null, "make sure", /* @__PURE__ */ import__virtual_react.default.createElement("br", null), "your build is generating an index.html page."));
|
|
39
|
+
});
|
|
40
|
+
register();
|
|
41
|
+
//#endregion
|
|
42
|
+
export { register };
|