@rsbuild/plugin-react 1.0.4 → 1.0.6

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 CHANGED
@@ -22,7 +22,7 @@ function __webpack_require__(moduleId) {
22
22
  }
23
23
  /************************************************************************/ // webpack/runtime/compat_get_default_export
24
24
  (()=>{
25
- // getDefaultExport function for compatibility with non-harmony modules
25
+ // getDefaultExport function for compatibility with non-ESM modules
26
26
  __webpack_require__.n = function(module1) {
27
27
  var getter = module1 && module1.__esModule ? function() {
28
28
  return module1['default'];
@@ -119,13 +119,13 @@ const react_require = (0, external_node_module_namespaceObject.createRequire)(/*
119
119
  return 'undefined' == typeof document ? new (module.require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
120
120
  }());
121
121
  const applyBasicReactSupport = (api, options)=>{
122
- const REACT_REFRESH_PATH = react_require.resolve('react-refresh');
123
- api.modifyEnvironmentConfig((userConfig, param)=>{
124
- let { mergeEnvironmentConfig } = param;
125
- const isDev = 'development' === userConfig.mode;
122
+ const REACT_REFRESH_PATH = options.fastRefresh ? react_require.resolve('react-refresh') : '';
123
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
124
+ const isDev = 'development' === config.mode;
125
+ const usingHMR = isDev && config.dev.hmr && 'web' === config.output.target;
126
126
  const reactOptions = {
127
127
  development: isDev,
128
- refresh: isDev && userConfig.dev.hmr && 'web' === userConfig.output.target,
128
+ refresh: usingHMR && options.fastRefresh,
129
129
  runtime: 'automatic',
130
130
  ...options.swcReactOptions
131
131
  };
@@ -145,13 +145,12 @@ const applyBasicReactSupport = (api, options)=>{
145
145
  }
146
146
  }
147
147
  };
148
- return mergeEnvironmentConfig(extraConfig, userConfig);
148
+ return mergeEnvironmentConfig(extraConfig, config);
149
149
  });
150
- api.modifyBundlerChain(async (chain, param)=>{
151
- let { CHAIN_ID, environment, isDev, target } = param;
150
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isDev, target })=>{
152
151
  const { config } = environment;
153
152
  const usingHMR = isDev && config.dev.hmr && 'web' === target;
154
- if (!usingHMR) return;
153
+ if (!usingHMR || !options.fastRefresh) return;
155
154
  chain.resolve.alias.set('react-refresh', external_node_path_default().dirname(REACT_REFRESH_PATH));
156
155
  const { default: ReactRefreshRspackPlugin } = await Promise.resolve().then(__webpack_require__.t.bind(__webpack_require__, "@rspack/plugin-react-refresh", 23));
157
156
  const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
@@ -170,8 +169,7 @@ const applyBasicReactSupport = (api, options)=>{
170
169
  });
171
170
  };
172
171
  const applyReactProfiler = (api)=>{
173
- api.modifyEnvironmentConfig((config, param)=>{
174
- let { mergeEnvironmentConfig } = param;
172
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
175
173
  if ('production' !== config.mode) return;
176
174
  const enableProfilerConfig = {
177
175
  output: {
@@ -190,8 +188,7 @@ const applyReactProfiler = (api)=>{
190
188
  };
191
189
  return mergeEnvironmentConfig(config, enableProfilerConfig);
192
190
  });
193
- api.modifyBundlerChain((chain, param)=>{
194
- let { isProd } = param;
191
+ api.modifyBundlerChain((chain, { isProd })=>{
195
192
  if (!isProd) return;
196
193
  // Replace react-dom with the profiling version.
197
194
  // Reference: https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977
@@ -200,13 +197,11 @@ const applyReactProfiler = (api)=>{
200
197
  });
201
198
  };
202
199
  const isPlainObject = (obj)=>null !== obj && 'object' == typeof obj && '[object Object]' === Object.prototype.toString.call(obj);
203
- const applySplitChunksRule = function(api) {
204
- let options = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {
205
- react: true,
206
- router: true
207
- };
208
- api.modifyBundlerChain((chain, param)=>{
209
- let { environment, isProd } = param;
200
+ const applySplitChunksRule = (api, options = {
201
+ react: true,
202
+ router: true
203
+ })=>{
204
+ api.modifyBundlerChain((chain, { environment, isProd })=>{
210
205
  const { config } = environment;
211
206
  if ('split-by-experience' !== config.performance.chunkSplit.strategy) return;
212
207
  const currentConfig = chain.optimization.splitChunks.values();
@@ -233,19 +228,24 @@ const applySplitChunksRule = function(api) {
233
228
  });
234
229
  };
235
230
  const PLUGIN_REACT_NAME = 'rsbuild:react';
236
- const pluginReact = function() {
237
- let { enableProfiler = false, ...options } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
238
- return {
231
+ const pluginReact = (options = {})=>({
239
232
  name: PLUGIN_REACT_NAME,
240
233
  setup (api) {
234
+ const defaultOptions = {
235
+ fastRefresh: true,
236
+ enableProfiler: false
237
+ };
238
+ const finalOptions = {
239
+ ...defaultOptions,
240
+ ...options
241
+ };
241
242
  if ('rspack' === api.context.bundlerType) {
242
- applyBasicReactSupport(api, options);
243
- if (enableProfiler) applyReactProfiler(api);
243
+ applyBasicReactSupport(api, finalOptions);
244
+ if (finalOptions.enableProfiler) applyReactProfiler(api);
244
245
  }
245
- applySplitChunksRule(api, null == options ? void 0 : options.splitChunks);
246
+ applySplitChunksRule(api, null == finalOptions ? void 0 : finalOptions.splitChunks);
246
247
  }
247
- };
248
- };
248
+ });
249
249
  var __webpack_export_target__ = exports;
250
250
  for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
251
251
  if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
package/dist/index.d.ts CHANGED
@@ -34,6 +34,11 @@ export type PluginReactOptions = {
34
34
  * @see https://rspack.dev/guide/tech/react#rspackplugin-react-refresh
35
35
  */
36
36
  reactRefreshOptions?: ReactRefreshOptions;
37
+ /**
38
+ * Whether to enable React Fast Refresh in development mode.
39
+ * @default true
40
+ */
41
+ fastRefresh?: boolean;
37
42
  };
38
43
  export declare const PLUGIN_REACT_NAME = "rsbuild:react";
39
- export declare const pluginReact: ({ enableProfiler, ...options }?: PluginReactOptions) => RsbuildPlugin;
44
+ export declare const pluginReact: (options?: PluginReactOptions) => RsbuildPlugin;
package/dist/index.js CHANGED
@@ -2,13 +2,13 @@ import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
2
2
  import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
3
3
  const react_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
4
4
  const applyBasicReactSupport = (api, options)=>{
5
- const REACT_REFRESH_PATH = react_require.resolve('react-refresh');
6
- api.modifyEnvironmentConfig((userConfig, param)=>{
7
- let { mergeEnvironmentConfig } = param;
8
- const isDev = 'development' === userConfig.mode;
5
+ const REACT_REFRESH_PATH = options.fastRefresh ? react_require.resolve('react-refresh') : '';
6
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
7
+ const isDev = 'development' === config.mode;
8
+ const usingHMR = isDev && config.dev.hmr && 'web' === config.output.target;
9
9
  const reactOptions = {
10
10
  development: isDev,
11
- refresh: isDev && userConfig.dev.hmr && 'web' === userConfig.output.target,
11
+ refresh: usingHMR && options.fastRefresh,
12
12
  runtime: 'automatic',
13
13
  ...options.swcReactOptions
14
14
  };
@@ -28,13 +28,12 @@ const applyBasicReactSupport = (api, options)=>{
28
28
  }
29
29
  }
30
30
  };
31
- return mergeEnvironmentConfig(extraConfig, userConfig);
31
+ return mergeEnvironmentConfig(extraConfig, config);
32
32
  });
33
- api.modifyBundlerChain(async (chain, param)=>{
34
- let { CHAIN_ID, environment, isDev, target } = param;
33
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isDev, target })=>{
35
34
  const { config } = environment;
36
35
  const usingHMR = isDev && config.dev.hmr && 'web' === target;
37
- if (!usingHMR) return;
36
+ if (!usingHMR || !options.fastRefresh) return;
38
37
  chain.resolve.alias.set('react-refresh', __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(REACT_REFRESH_PATH));
39
38
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
40
39
  const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
@@ -53,8 +52,7 @@ const applyBasicReactSupport = (api, options)=>{
53
52
  });
54
53
  };
55
54
  const applyReactProfiler = (api)=>{
56
- api.modifyEnvironmentConfig((config, param)=>{
57
- let { mergeEnvironmentConfig } = param;
55
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
58
56
  if ('production' !== config.mode) return;
59
57
  const enableProfilerConfig = {
60
58
  output: {
@@ -73,8 +71,7 @@ const applyReactProfiler = (api)=>{
73
71
  };
74
72
  return mergeEnvironmentConfig(config, enableProfilerConfig);
75
73
  });
76
- api.modifyBundlerChain((chain, param)=>{
77
- let { isProd } = param;
74
+ api.modifyBundlerChain((chain, { isProd })=>{
78
75
  if (!isProd) return;
79
76
  // Replace react-dom with the profiling version.
80
77
  // Reference: https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977
@@ -83,13 +80,11 @@ const applyReactProfiler = (api)=>{
83
80
  });
84
81
  };
85
82
  const isPlainObject = (obj)=>null !== obj && 'object' == typeof obj && '[object Object]' === Object.prototype.toString.call(obj);
86
- const applySplitChunksRule = function(api) {
87
- let options = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {
88
- react: true,
89
- router: true
90
- };
91
- api.modifyBundlerChain((chain, param)=>{
92
- let { environment, isProd } = param;
83
+ const applySplitChunksRule = (api, options = {
84
+ react: true,
85
+ router: true
86
+ })=>{
87
+ api.modifyBundlerChain((chain, { environment, isProd })=>{
93
88
  const { config } = environment;
94
89
  if ('split-by-experience' !== config.performance.chunkSplit.strategy) return;
95
90
  const currentConfig = chain.optimization.splitChunks.values();
@@ -116,17 +111,22 @@ const applySplitChunksRule = function(api) {
116
111
  });
117
112
  };
118
113
  const PLUGIN_REACT_NAME = 'rsbuild:react';
119
- const pluginReact = function() {
120
- let { enableProfiler = false, ...options } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
121
- return {
114
+ const pluginReact = (options = {})=>({
122
115
  name: PLUGIN_REACT_NAME,
123
116
  setup (api) {
117
+ const defaultOptions = {
118
+ fastRefresh: true,
119
+ enableProfiler: false
120
+ };
121
+ const finalOptions = {
122
+ ...defaultOptions,
123
+ ...options
124
+ };
124
125
  if ('rspack' === api.context.bundlerType) {
125
- applyBasicReactSupport(api, options);
126
- if (enableProfiler) applyReactProfiler(api);
126
+ applyBasicReactSupport(api, finalOptions);
127
+ if (finalOptions.enableProfiler) applyReactProfiler(api);
127
128
  }
128
- applySplitChunksRule(api, null == options ? void 0 : options.splitChunks);
129
+ applySplitChunksRule(api, null == finalOptions ? void 0 : finalOptions.splitChunks);
129
130
  }
130
- };
131
- };
131
+ });
132
132
  export { PLUGIN_REACT_NAME, pluginReact };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,11 +28,11 @@
28
28
  "devDependencies": {
29
29
  "@types/node": "18.x",
30
30
  "typescript": "^5.6.3",
31
- "@rsbuild/core": "1.0.14",
31
+ "@rsbuild/core": "1.0.19",
32
32
  "@scripts/test-helper": "1.0.1"
33
33
  },
34
34
  "peerDependencies": {
35
- "@rsbuild/core": "1.x || ^1.0.1-rc.0"
35
+ "@rsbuild/core": "1.x"
36
36
  },
37
37
  "publishConfig": {
38
38
  "access": "public",