@rsbuild/plugin-react 0.0.0-next-20240513013836 → 0.0.0-next-20240528072128
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.cjs +191 -0
- package/dist/index.js +57 -57
- package/package.json +10 -10
- package/dist/index.mjs +0 -157
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
PLUGIN_REACT_NAME: () => PLUGIN_REACT_NAME,
|
|
34
|
+
pluginReact: () => pluginReact
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
var import_shared3 = require("@rsbuild/shared");
|
|
38
|
+
|
|
39
|
+
// src/react.ts
|
|
40
|
+
var import_node_path = __toESM(require("path"));
|
|
41
|
+
var import_shared = require("@rsbuild/shared");
|
|
42
|
+
var modifySwcLoaderOptions = ({
|
|
43
|
+
chain,
|
|
44
|
+
CHAIN_ID,
|
|
45
|
+
modifier
|
|
46
|
+
}) => {
|
|
47
|
+
const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI];
|
|
48
|
+
for (const ruleId of ruleIds) {
|
|
49
|
+
if (chain.module.rules.has(ruleId)) {
|
|
50
|
+
const rule = chain.module.rule(ruleId);
|
|
51
|
+
if (rule.uses.has(CHAIN_ID.USE.SWC)) {
|
|
52
|
+
rule.use(CHAIN_ID.USE.SWC).tap(modifier);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var applyBasicReactSupport = (api, options) => {
|
|
58
|
+
const REACT_REFRESH_PATH = require.resolve("react-refresh");
|
|
59
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd: isProd2, target }) => {
|
|
60
|
+
const config = api.getNormalizedConfig();
|
|
61
|
+
const usingHMR = (0, import_shared.isUsingHMR)(config, { isProd: isProd2, target });
|
|
62
|
+
const reactOptions = {
|
|
63
|
+
development: isDev,
|
|
64
|
+
refresh: usingHMR,
|
|
65
|
+
runtime: "automatic",
|
|
66
|
+
...options.swcReactOptions
|
|
67
|
+
};
|
|
68
|
+
modifySwcLoaderOptions({
|
|
69
|
+
chain,
|
|
70
|
+
CHAIN_ID,
|
|
71
|
+
modifier: (opts) => {
|
|
72
|
+
const extraOptions = {
|
|
73
|
+
jsc: {
|
|
74
|
+
parser: {
|
|
75
|
+
syntax: "typescript",
|
|
76
|
+
// enable supports for React JSX/TSX compilation
|
|
77
|
+
tsx: true
|
|
78
|
+
},
|
|
79
|
+
transform: {
|
|
80
|
+
react: reactOptions
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return (0, import_shared.deepmerge)(opts, extraOptions);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (!usingHMR) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
chain.resolve.alias.set("react-refresh", import_node_path.default.dirname(REACT_REFRESH_PATH));
|
|
91
|
+
const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
|
|
92
|
+
chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
|
|
93
|
+
{
|
|
94
|
+
include: [import_shared.SCRIPT_REGEX],
|
|
95
|
+
...options.reactRefreshOptions
|
|
96
|
+
}
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
var applyReactProfiler = (api) => {
|
|
101
|
+
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
|
|
102
|
+
const enableProfilerConfig = {
|
|
103
|
+
output: {
|
|
104
|
+
minify: {
|
|
105
|
+
jsOptions: {
|
|
106
|
+
// Need to keep classnames and function names like Components for debugging purposes.
|
|
107
|
+
mangle: {
|
|
108
|
+
keep_classnames: true,
|
|
109
|
+
keep_fnames: true
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
return mergeRsbuildConfig(config, enableProfilerConfig);
|
|
116
|
+
});
|
|
117
|
+
api.modifyBundlerChain((chain) => {
|
|
118
|
+
chain.resolve.alias.set("react-dom$", "react-dom/profiling");
|
|
119
|
+
chain.resolve.alias.set("scheduler/tracing", "scheduler/tracing-profiling");
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/splitChunks.ts
|
|
124
|
+
var import_shared2 = require("@rsbuild/shared");
|
|
125
|
+
var applySplitChunksRule = (api, options = {
|
|
126
|
+
react: true,
|
|
127
|
+
router: true
|
|
128
|
+
}) => {
|
|
129
|
+
api.modifyBundlerChain((chain) => {
|
|
130
|
+
const config = api.getNormalizedConfig();
|
|
131
|
+
if (config.performance.chunkSplit.strategy !== "split-by-experience") {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const currentConfig = chain.optimization.splitChunks.values();
|
|
135
|
+
if (!(0, import_shared2.isPlainObject)(currentConfig)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const extraGroups = {};
|
|
139
|
+
if (options.react) {
|
|
140
|
+
extraGroups.react = [
|
|
141
|
+
"react",
|
|
142
|
+
"react-dom",
|
|
143
|
+
"scheduler",
|
|
144
|
+
...(0, import_shared2.isProd)() ? [] : ["react-refresh", /@rspack[\\/]plugin-react-refresh/]
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
if (options.router) {
|
|
148
|
+
extraGroups.router = [
|
|
149
|
+
"react-router",
|
|
150
|
+
"react-router-dom",
|
|
151
|
+
"history",
|
|
152
|
+
/@remix-run[\\/]router/
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
if (!Object.keys(extraGroups).length) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
chain.optimization.splitChunks({
|
|
159
|
+
...currentConfig,
|
|
160
|
+
// @ts-expect-error Rspack and Webpack uses different cacheGroups type
|
|
161
|
+
cacheGroups: {
|
|
162
|
+
...currentConfig.cacheGroups,
|
|
163
|
+
...(0, import_shared2.createCacheGroups)(extraGroups)
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/index.ts
|
|
170
|
+
var PLUGIN_REACT_NAME = "rsbuild:react";
|
|
171
|
+
var pluginReact = ({
|
|
172
|
+
enableProfiler = false,
|
|
173
|
+
...options
|
|
174
|
+
} = {}) => ({
|
|
175
|
+
name: PLUGIN_REACT_NAME,
|
|
176
|
+
setup(api) {
|
|
177
|
+
if (api.context.bundlerType === "rspack") {
|
|
178
|
+
applyBasicReactSupport(api, options);
|
|
179
|
+
const isProdProfile = enableProfiler && (0, import_shared3.getNodeEnv)() === "production";
|
|
180
|
+
if (isProdProfile) {
|
|
181
|
+
applyReactProfiler(api);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
applySplitChunksRule(api, options?.splitChunks);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
188
|
+
0 && (module.exports = {
|
|
189
|
+
PLUGIN_REACT_NAME,
|
|
190
|
+
pluginReact
|
|
191
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1,76 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
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);
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
var require = createRequire(import.meta['url']);
|
|
29
3
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined")
|
|
8
|
+
return require.apply(this, arguments);
|
|
9
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
35
10
|
});
|
|
36
|
-
|
|
37
|
-
|
|
11
|
+
|
|
12
|
+
// ../../node_modules/.pnpm/@modern-js+module-tools@2.50.0_eslint@9.3.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
|
|
13
|
+
import { fileURLToPath } from "url";
|
|
14
|
+
import path from "path";
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
17
|
+
import { getNodeEnv } from "@rsbuild/shared";
|
|
38
18
|
|
|
39
19
|
// src/react.ts
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
import path2 from "path";
|
|
21
|
+
import { SCRIPT_REGEX, deepmerge, isUsingHMR } from "@rsbuild/shared";
|
|
22
|
+
var modifySwcLoaderOptions = ({
|
|
23
|
+
chain,
|
|
24
|
+
CHAIN_ID,
|
|
25
|
+
modifier
|
|
26
|
+
}) => {
|
|
27
|
+
const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI];
|
|
28
|
+
for (const ruleId of ruleIds) {
|
|
29
|
+
if (chain.module.rules.has(ruleId)) {
|
|
30
|
+
const rule = chain.module.rule(ruleId);
|
|
31
|
+
if (rule.uses.has(CHAIN_ID.USE.SWC)) {
|
|
32
|
+
rule.use(CHAIN_ID.USE.SWC).tap(modifier);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
42
37
|
var applyBasicReactSupport = (api, options) => {
|
|
43
|
-
const REACT_REFRESH_PATH =
|
|
38
|
+
const REACT_REFRESH_PATH = __require.resolve("react-refresh");
|
|
44
39
|
api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd: isProd2, target }) => {
|
|
45
40
|
const config = api.getNormalizedConfig();
|
|
46
|
-
const usingHMR =
|
|
41
|
+
const usingHMR = isUsingHMR(config, { isProd: isProd2, target });
|
|
47
42
|
const reactOptions = {
|
|
48
43
|
development: isDev,
|
|
49
44
|
refresh: usingHMR,
|
|
50
45
|
runtime: "automatic",
|
|
51
46
|
...options.swcReactOptions
|
|
52
47
|
};
|
|
53
|
-
|
|
48
|
+
modifySwcLoaderOptions({
|
|
54
49
|
chain,
|
|
50
|
+
CHAIN_ID,
|
|
55
51
|
modifier: (opts) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
const extraOptions = {
|
|
53
|
+
jsc: {
|
|
54
|
+
parser: {
|
|
55
|
+
syntax: "typescript",
|
|
56
|
+
// enable supports for React JSX/TSX compilation
|
|
57
|
+
tsx: true
|
|
58
|
+
},
|
|
59
|
+
transform: {
|
|
60
|
+
react: reactOptions
|
|
61
|
+
}
|
|
62
|
+
}
|
|
62
63
|
};
|
|
63
|
-
return opts;
|
|
64
|
+
return deepmerge(opts, extraOptions);
|
|
64
65
|
}
|
|
65
66
|
});
|
|
66
67
|
if (!usingHMR) {
|
|
67
68
|
return;
|
|
68
69
|
}
|
|
69
|
-
chain.resolve.alias.set("react-refresh",
|
|
70
|
+
chain.resolve.alias.set("react-refresh", path2.dirname(REACT_REFRESH_PATH));
|
|
70
71
|
const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
|
|
71
72
|
chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
|
|
72
73
|
{
|
|
73
|
-
include: [
|
|
74
|
+
include: [SCRIPT_REGEX],
|
|
74
75
|
...options.reactRefreshOptions
|
|
75
76
|
}
|
|
76
77
|
]);
|
|
@@ -100,7 +101,7 @@ var applyReactProfiler = (api) => {
|
|
|
100
101
|
};
|
|
101
102
|
|
|
102
103
|
// src/splitChunks.ts
|
|
103
|
-
|
|
104
|
+
import { createCacheGroups, isPlainObject, isProd } from "@rsbuild/shared";
|
|
104
105
|
var applySplitChunksRule = (api, options = {
|
|
105
106
|
react: true,
|
|
106
107
|
router: true
|
|
@@ -111,7 +112,7 @@ var applySplitChunksRule = (api, options = {
|
|
|
111
112
|
return;
|
|
112
113
|
}
|
|
113
114
|
const currentConfig = chain.optimization.splitChunks.values();
|
|
114
|
-
if (!
|
|
115
|
+
if (!isPlainObject(currentConfig)) {
|
|
115
116
|
return;
|
|
116
117
|
}
|
|
117
118
|
const extraGroups = {};
|
|
@@ -120,7 +121,7 @@ var applySplitChunksRule = (api, options = {
|
|
|
120
121
|
"react",
|
|
121
122
|
"react-dom",
|
|
122
123
|
"scheduler",
|
|
123
|
-
...
|
|
124
|
+
...isProd() ? [] : ["react-refresh", /@rspack[\\/]plugin-react-refresh/]
|
|
124
125
|
];
|
|
125
126
|
}
|
|
126
127
|
if (options.router) {
|
|
@@ -139,7 +140,7 @@ var applySplitChunksRule = (api, options = {
|
|
|
139
140
|
// @ts-expect-error Rspack and Webpack uses different cacheGroups type
|
|
140
141
|
cacheGroups: {
|
|
141
142
|
...currentConfig.cacheGroups,
|
|
142
|
-
...
|
|
143
|
+
...createCacheGroups(extraGroups)
|
|
143
144
|
}
|
|
144
145
|
});
|
|
145
146
|
});
|
|
@@ -155,7 +156,7 @@ var pluginReact = ({
|
|
|
155
156
|
setup(api) {
|
|
156
157
|
if (api.context.bundlerType === "rspack") {
|
|
157
158
|
applyBasicReactSupport(api, options);
|
|
158
|
-
const isProdProfile = enableProfiler &&
|
|
159
|
+
const isProdProfile = enableProfiler && getNodeEnv() === "production";
|
|
159
160
|
if (isProdProfile) {
|
|
160
161
|
applyReactProfiler(api);
|
|
161
162
|
}
|
|
@@ -163,8 +164,7 @@ var pluginReact = ({
|
|
|
163
164
|
applySplitChunksRule(api, options?.splitChunks);
|
|
164
165
|
}
|
|
165
166
|
});
|
|
166
|
-
|
|
167
|
-
0 && (module.exports = {
|
|
167
|
+
export {
|
|
168
168
|
PLUGIN_REACT_NAME,
|
|
169
169
|
pluginReact
|
|
170
|
-
}
|
|
170
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-react",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240528072128",
|
|
4
4
|
"description": "React plugin for Rsbuild",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,32 +8,32 @@
|
|
|
8
8
|
"directory": "packages/plugin-react"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"type": "
|
|
11
|
+
"type": "module",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
|
-
"import": "./dist/index.
|
|
16
|
-
"
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
|
-
"main": "./dist/index.
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
20
|
"types": "./dist/index.d.ts",
|
|
21
21
|
"files": [
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@rspack/plugin-react-refresh": "0.
|
|
25
|
+
"@rspack/plugin-react-refresh": "0.7.0",
|
|
26
26
|
"react-refresh": "^0.14.2",
|
|
27
|
-
"@rsbuild/shared": "0.0.0-next-
|
|
27
|
+
"@rsbuild/shared": "0.0.0-next-20240528072128"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "18.x",
|
|
31
31
|
"typescript": "^5.4.2",
|
|
32
|
-
"@rsbuild/core": "0.0.0-next-
|
|
33
|
-
"@scripts/test-helper": "0.0.0-next-
|
|
32
|
+
"@rsbuild/core": "0.0.0-next-20240528072128",
|
|
33
|
+
"@scripts/test-helper": "0.0.0-next-20240528072128"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@rsbuild/core": "0.0.0-next-
|
|
36
|
+
"@rsbuild/core": "0.0.0-next-20240528072128"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public",
|
package/dist/index.mjs
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'module';
|
|
2
|
-
var require = createRequire(import.meta['url']);
|
|
3
|
-
|
|
4
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
-
}) : x)(function(x) {
|
|
7
|
-
if (typeof require !== "undefined")
|
|
8
|
-
return require.apply(this, arguments);
|
|
9
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
// ../../node_modules/.pnpm/@modern-js+module-tools@2.49.2_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
|
|
13
|
-
import { fileURLToPath } from "url";
|
|
14
|
-
import path from "path";
|
|
15
|
-
|
|
16
|
-
// src/index.ts
|
|
17
|
-
import { getNodeEnv } from "@rsbuild/shared";
|
|
18
|
-
|
|
19
|
-
// src/react.ts
|
|
20
|
-
import path2 from "path";
|
|
21
|
-
import {
|
|
22
|
-
SCRIPT_REGEX,
|
|
23
|
-
isUsingHMR,
|
|
24
|
-
modifySwcLoaderOptions
|
|
25
|
-
} from "@rsbuild/shared";
|
|
26
|
-
var applyBasicReactSupport = (api, options) => {
|
|
27
|
-
const REACT_REFRESH_PATH = __require.resolve("react-refresh");
|
|
28
|
-
api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd: isProd2, target }) => {
|
|
29
|
-
const config = api.getNormalizedConfig();
|
|
30
|
-
const usingHMR = isUsingHMR(config, { isProd: isProd2, target });
|
|
31
|
-
const reactOptions = {
|
|
32
|
-
development: isDev,
|
|
33
|
-
refresh: usingHMR,
|
|
34
|
-
runtime: "automatic",
|
|
35
|
-
...options.swcReactOptions
|
|
36
|
-
};
|
|
37
|
-
modifySwcLoaderOptions({
|
|
38
|
-
chain,
|
|
39
|
-
modifier: (opts) => {
|
|
40
|
-
var _a;
|
|
41
|
-
opts.jsc ?? (opts.jsc = {});
|
|
42
|
-
(_a = opts.jsc).transform ?? (_a.transform = {});
|
|
43
|
-
opts.jsc.transform.react = {
|
|
44
|
-
...opts.jsc.transform.react,
|
|
45
|
-
...reactOptions
|
|
46
|
-
};
|
|
47
|
-
return opts;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
if (!usingHMR) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
chain.resolve.alias.set("react-refresh", path2.dirname(REACT_REFRESH_PATH));
|
|
54
|
-
const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
|
|
55
|
-
chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
|
|
56
|
-
{
|
|
57
|
-
include: [SCRIPT_REGEX],
|
|
58
|
-
...options.reactRefreshOptions
|
|
59
|
-
}
|
|
60
|
-
]);
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
var applyReactProfiler = (api) => {
|
|
64
|
-
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
|
|
65
|
-
const enableProfilerConfig = {
|
|
66
|
-
output: {
|
|
67
|
-
minify: {
|
|
68
|
-
jsOptions: {
|
|
69
|
-
// Need to keep classnames and function names like Components for debugging purposes.
|
|
70
|
-
mangle: {
|
|
71
|
-
keep_classnames: true,
|
|
72
|
-
keep_fnames: true
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
return mergeRsbuildConfig(config, enableProfilerConfig);
|
|
79
|
-
});
|
|
80
|
-
api.modifyBundlerChain((chain) => {
|
|
81
|
-
chain.resolve.alias.set("react-dom$", "react-dom/profiling");
|
|
82
|
-
chain.resolve.alias.set("scheduler/tracing", "scheduler/tracing-profiling");
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
// src/splitChunks.ts
|
|
87
|
-
import {
|
|
88
|
-
createCacheGroups,
|
|
89
|
-
isPlainObject,
|
|
90
|
-
isProd
|
|
91
|
-
} from "@rsbuild/shared";
|
|
92
|
-
var applySplitChunksRule = (api, options = {
|
|
93
|
-
react: true,
|
|
94
|
-
router: true
|
|
95
|
-
}) => {
|
|
96
|
-
api.modifyBundlerChain((chain) => {
|
|
97
|
-
const config = api.getNormalizedConfig();
|
|
98
|
-
if (config.performance.chunkSplit.strategy !== "split-by-experience") {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const currentConfig = chain.optimization.splitChunks.values();
|
|
102
|
-
if (!isPlainObject(currentConfig)) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
const extraGroups = {};
|
|
106
|
-
if (options.react) {
|
|
107
|
-
extraGroups.react = [
|
|
108
|
-
"react",
|
|
109
|
-
"react-dom",
|
|
110
|
-
"scheduler",
|
|
111
|
-
...isProd() ? [] : ["react-refresh", /@rspack[\\/]plugin-react-refresh/]
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
if (options.router) {
|
|
115
|
-
extraGroups.router = [
|
|
116
|
-
"react-router",
|
|
117
|
-
"react-router-dom",
|
|
118
|
-
"history",
|
|
119
|
-
/@remix-run[\\/]router/
|
|
120
|
-
];
|
|
121
|
-
}
|
|
122
|
-
if (!Object.keys(extraGroups).length) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
chain.optimization.splitChunks({
|
|
126
|
-
...currentConfig,
|
|
127
|
-
// @ts-expect-error Rspack and Webpack uses different cacheGroups type
|
|
128
|
-
cacheGroups: {
|
|
129
|
-
...currentConfig.cacheGroups,
|
|
130
|
-
...createCacheGroups(extraGroups)
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
// src/index.ts
|
|
137
|
-
var PLUGIN_REACT_NAME = "rsbuild:react";
|
|
138
|
-
var pluginReact = ({
|
|
139
|
-
enableProfiler = false,
|
|
140
|
-
...options
|
|
141
|
-
} = {}) => ({
|
|
142
|
-
name: PLUGIN_REACT_NAME,
|
|
143
|
-
setup(api) {
|
|
144
|
-
if (api.context.bundlerType === "rspack") {
|
|
145
|
-
applyBasicReactSupport(api, options);
|
|
146
|
-
const isProdProfile = enableProfiler && getNodeEnv() === "production";
|
|
147
|
-
if (isProdProfile) {
|
|
148
|
-
applyReactProfiler(api);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
applySplitChunksRule(api, options?.splitChunks);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
export {
|
|
155
|
-
PLUGIN_REACT_NAME,
|
|
156
|
-
pluginReact
|
|
157
|
-
};
|