@rsbuild/plugin-react 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,13 @@ import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
3
  declare const isBeyondReact17: (cwd: string) => Promise<boolean>;
4
4
 
5
- declare const pluginReact: () => RsbuildPlugin;
5
+ type splitReactChunkOptions = {
6
+ react?: boolean;
7
+ router?: boolean;
8
+ };
9
+ type PluginReactOptions = {
10
+ splitChunks?: splitReactChunkOptions;
11
+ };
12
+ declare const pluginReact: (options?: PluginReactOptions) => RsbuildPlugin;
6
13
 
7
- export { isBeyondReact17, pluginReact };
14
+ export { PluginReactOptions, isBeyondReact17, pluginReact, splitReactChunkOptions };
package/dist/index.js CHANGED
@@ -50,16 +50,15 @@ var getAntdMajorVersion = (appDirectory) => {
50
50
  };
51
51
  var applyAntdSupport = (api) => {
52
52
  api.modifyRsbuildConfig((rsbuildConfig) => {
53
- var _a, _b, _c;
54
- (_a = rsbuildConfig.source) != null ? _a : rsbuildConfig.source = {};
55
- if (rsbuildConfig.source.transformImport === false || ((_b = rsbuildConfig.source.transformImport) == null ? void 0 : _b.some(
53
+ rsbuildConfig.source ?? (rsbuildConfig.source = {});
54
+ if (rsbuildConfig.source.transformImport === false || rsbuildConfig.source.transformImport?.some(
56
55
  (item) => item.libraryName === "antd"
57
- ))) {
56
+ )) {
58
57
  return;
59
58
  }
60
59
  const antdMajorVersion = getAntdMajorVersion(api.context.rootPath);
61
60
  if (antdMajorVersion && antdMajorVersion < 5) {
62
- (_c = rsbuildConfig.source) != null ? _c : rsbuildConfig.source = {};
61
+ rsbuildConfig.source ?? (rsbuildConfig.source = {});
63
62
  rsbuildConfig.source.transformImport = [
64
63
  ...rsbuildConfig.source.transformImport || [],
65
64
  {
@@ -83,7 +82,7 @@ var applyArcoSupport = (api) => {
83
82
  return;
84
83
  }
85
84
  const isUseSSR = (0, import_shared2.isServerTarget)(api.context.target);
86
- if (!(transformImport == null ? void 0 : transformImport.some((item) => item.libraryName === ARCO_NAME))) {
85
+ if (!transformImport?.some((item) => item.libraryName === ARCO_NAME)) {
87
86
  transformImport.push({
88
87
  libraryName: ARCO_NAME,
89
88
  libraryDirectory: isUseSSR ? "lib" : "es",
@@ -91,7 +90,7 @@ var applyArcoSupport = (api) => {
91
90
  style: true
92
91
  });
93
92
  }
94
- if (!(transformImport == null ? void 0 : transformImport.some((item) => item.libraryName === ARCO_ICON))) {
93
+ if (!transformImport?.some((item) => item.libraryName === ARCO_ICON)) {
95
94
  transformImport.push({
96
95
  libraryName: ARCO_ICON,
97
96
  libraryDirectory: isUseSSR ? "react-icon-cjs" : "react-icon",
@@ -105,36 +104,48 @@ var applyArcoSupport = (api) => {
105
104
 
106
105
  // src/splitChunks.ts
107
106
  var import_shared3 = require("@rsbuild/shared");
108
- var applySplitChunksRule = (api) => {
107
+ var applySplitChunksRule = (api, options) => {
109
108
  api.modifyBundlerChain((chain) => {
110
109
  const config = api.getNormalizedConfig();
111
110
  const { chunkSplit } = config.performance || {};
112
- if ((chunkSplit == null ? void 0 : chunkSplit.strategy) !== "split-by-experience") {
111
+ if (chunkSplit?.strategy !== "split-by-experience") {
113
112
  return;
114
113
  }
114
+ if (!options) {
115
+ options = {
116
+ react: true,
117
+ router: true
118
+ };
119
+ }
115
120
  const currentConfig = chain.optimization.splitChunks.values();
116
121
  if (!(0, import_shared3.isPlainObject)(currentConfig)) {
117
122
  return;
118
123
  }
119
- const extraGroups = (0, import_shared3.createCacheGroups)({
120
- react: [
124
+ const extraGroups = {};
125
+ if (options.react !== false) {
126
+ extraGroups.react = [
121
127
  "react",
122
128
  "react-dom",
123
129
  "scheduler",
124
130
  ...(0, import_shared3.isProd)() ? [] : ["react-refresh", "@pmmmwh/react-refresh-webpack-plugin"]
125
- ],
126
- router: [
131
+ ];
132
+ }
133
+ if (options.router !== false) {
134
+ extraGroups.router = [
127
135
  "react-router",
128
136
  "react-router-dom",
129
137
  "@remix-run/router",
130
138
  "history"
131
- ]
132
- });
139
+ ];
140
+ }
141
+ if (!Object.keys(extraGroups).length) {
142
+ return;
143
+ }
133
144
  chain.optimization.splitChunks({
134
145
  ...currentConfig,
135
146
  cacheGroups: {
136
147
  ...currentConfig.cacheGroups,
137
- ...extraGroups
148
+ ...(0, import_shared3.createCacheGroups)(extraGroups)
138
149
  }
139
150
  });
140
151
  });
@@ -143,9 +154,8 @@ var applySplitChunksRule = (api) => {
143
154
  // src/react.ts
144
155
  var import_shared4 = require("@rsbuild/shared");
145
156
  function getReactRefreshEntry(compiler) {
146
- var _a, _b, _c, _d, _e;
147
- const hot = (_b = (_a = compiler.options.devServer) == null ? void 0 : _a.hot) != null ? _b : true;
148
- const refresh = (_e = (_d = (_c = compiler.options.builtins) == null ? void 0 : _c.react) == null ? void 0 : _d.refresh) != null ? _e : true;
157
+ const hot = compiler.options.devServer?.hot ?? true;
158
+ const refresh = compiler.options.builtins?.react?.refresh ?? true;
149
159
  if (hot && refresh) {
150
160
  const reactRefreshEntryPath = require.resolve("@rspack/plugin-react-refresh/react-refresh-entry");
151
161
  return reactRefreshEntryPath;
@@ -230,7 +240,7 @@ var isBeyondReact17 = async (cwd) => {
230
240
  };
231
241
 
232
242
  // src/index.ts
233
- var pluginReact = () => ({
243
+ var pluginReact = (options = {}) => ({
234
244
  name: "rsbuild:react",
235
245
  pre: ["rsbuild:swc"],
236
246
  setup(api) {
@@ -239,7 +249,7 @@ var pluginReact = () => ({
239
249
  }
240
250
  applyAntdSupport(api);
241
251
  applyArcoSupport(api);
242
- applySplitChunksRule(api);
252
+ applySplitChunksRule(api, options?.splitChunks);
243
253
  }
244
254
  });
245
255
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -90,36 +90,48 @@ import {
90
90
  isPlainObject,
91
91
  createCacheGroups
92
92
  } from "@rsbuild/shared";
93
- var applySplitChunksRule = (api) => {
93
+ var applySplitChunksRule = (api, options) => {
94
94
  api.modifyBundlerChain((chain) => {
95
95
  const config = api.getNormalizedConfig();
96
96
  const { chunkSplit } = config.performance || {};
97
97
  if (chunkSplit?.strategy !== "split-by-experience") {
98
98
  return;
99
99
  }
100
+ if (!options) {
101
+ options = {
102
+ react: true,
103
+ router: true
104
+ };
105
+ }
100
106
  const currentConfig = chain.optimization.splitChunks.values();
101
107
  if (!isPlainObject(currentConfig)) {
102
108
  return;
103
109
  }
104
- const extraGroups = createCacheGroups({
105
- react: [
110
+ const extraGroups = {};
111
+ if (options.react !== false) {
112
+ extraGroups.react = [
106
113
  "react",
107
114
  "react-dom",
108
115
  "scheduler",
109
116
  ...isProd() ? [] : ["react-refresh", "@pmmmwh/react-refresh-webpack-plugin"]
110
- ],
111
- router: [
117
+ ];
118
+ }
119
+ if (options.router !== false) {
120
+ extraGroups.router = [
112
121
  "react-router",
113
122
  "react-router-dom",
114
123
  "@remix-run/router",
115
124
  "history"
116
- ]
117
- });
125
+ ];
126
+ }
127
+ if (!Object.keys(extraGroups).length) {
128
+ return;
129
+ }
118
130
  chain.optimization.splitChunks({
119
131
  ...currentConfig,
120
132
  cacheGroups: {
121
133
  ...currentConfig.cacheGroups,
122
- ...extraGroups
134
+ ...createCacheGroups(extraGroups)
123
135
  }
124
136
  });
125
137
  });
@@ -216,7 +228,7 @@ var isBeyondReact17 = async (cwd) => {
216
228
  };
217
229
 
218
230
  // src/index.ts
219
- var pluginReact = () => ({
231
+ var pluginReact = (options = {}) => ({
220
232
  name: "rsbuild:react",
221
233
  pre: ["rsbuild:swc"],
222
234
  setup(api) {
@@ -225,7 +237,7 @@ var pluginReact = () => ({
225
237
  }
226
238
  applyAntdSupport(api);
227
239
  applyArcoSupport(api);
228
- applySplitChunksRule(api);
240
+ applySplitChunksRule(api, options?.splitChunks);
229
241
  }
230
242
  });
231
243
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,17 +21,17 @@
21
21
  "dist"
22
22
  ],
23
23
  "dependencies": {
24
- "@rspack/plugin-react-refresh": "0.4.0",
24
+ "@rspack/plugin-react-refresh": "0.4.0-canary-d45e3e0-20231128075052",
25
25
  "react-refresh": "^0.14.0",
26
26
  "semver": "^7.5.4",
27
- "@rsbuild/shared": "0.1.2"
27
+ "@rsbuild/shared": "0.1.4"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^16",
31
31
  "@types/semver": "^7.5.4",
32
32
  "typescript": "^5.3.0",
33
- "@rsbuild/core": "0.1.2",
34
- "@rsbuild/test-helper": "0.1.2"
33
+ "@rsbuild/core": "0.1.4",
34
+ "@rsbuild/test-helper": "0.1.4"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public",