@rsbuild/plugin-react 0.2.17 → 0.3.0

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 CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Rsbuild
6
6
 
7
- Unleash the power of Rspack with the out-of-the-box build tool.
7
+ The Rspack-based build tool. It's fast, out-of-the-box and extensible.
8
8
 
9
9
  ## Documentation
10
10
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
+ import { SwcReactConfig } from '@rsbuild/shared';
2
3
 
3
4
  declare const isBeyondReact17: (cwd: string) => Promise<any>;
4
5
 
@@ -17,6 +18,11 @@ type SplitReactChunkOptions = {
17
18
  router?: boolean;
18
19
  };
19
20
  type PluginReactOptions = {
21
+ /**
22
+ * Configure the behavior of SWC to transform React code,
23
+ * the same as SWC's [jsc.transform.react](https://swc.rs/docs/configuration/compilation#jsctransformreact).
24
+ */
25
+ swcReactOptions?: SwcReactConfig;
20
26
  /**
21
27
  * Configuration for chunk splitting of React-related dependencies.
22
28
  */
package/dist/index.js CHANGED
@@ -148,65 +148,39 @@ var applySplitChunksRule = (api, options = {
148
148
  };
149
149
 
150
150
  // src/react.ts
151
+ var import_node_path = __toESM(require("path"));
151
152
  var import_shared4 = require("@rsbuild/shared");
152
- function getReactRefreshEntry(compiler) {
153
- const hot = compiler.options.devServer?.hot ?? true;
154
- const refresh = compiler.options.builtins?.react?.refresh ?? true;
155
- if (hot && refresh) {
156
- const reactRefreshEntryPath = require.resolve("@rspack/plugin-react-refresh/react-refresh-entry");
157
- return reactRefreshEntryPath;
158
- }
159
- return null;
160
- }
161
- var setupCompiler = (compiler) => {
162
- if (!(0, import_shared4.isClientCompiler)(compiler)) {
163
- return;
164
- }
165
- const reactRefreshEntry = getReactRefreshEntry(compiler);
166
- if (!reactRefreshEntry) {
167
- return;
168
- }
169
- new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntry, {
170
- name: void 0
171
- }).apply(compiler);
172
- };
173
- var applyBasicReactSupport = (api) => {
174
- api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
175
- if ((0, import_shared4.isProd)()) {
176
- return;
177
- }
178
- if (multiCompiler.compilers) {
179
- multiCompiler.compilers.forEach(setupCompiler);
180
- } else {
181
- setupCompiler(multiCompiler);
182
- }
183
- });
184
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd3, target }) => {
153
+ var REACT_REFRESH_PATH = require.resolve("react-refresh");
154
+ var REACT_REFRESH_DIR_PATH = import_node_path.default.dirname(REACT_REFRESH_PATH);
155
+ var applyBasicReactSupport = (api, options) => {
156
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
185
157
  const config = api.getNormalizedConfig();
186
- const usingHMR = (0, import_shared4.isUsingHMR)(config, { isProd: isProd3, target });
158
+ const usingHMR = (0, import_shared4.isUsingHMR)(config, { isProd: isProd2, target });
187
159
  const rule = chain.module.rule(CHAIN_ID.RULE.JS);
188
160
  const reactOptions = {
189
- development: !isProd3,
161
+ development: !isProd2,
190
162
  refresh: usingHMR,
191
- runtime: "automatic"
163
+ runtime: "automatic",
164
+ ...options.swcReactOptions
192
165
  };
193
- rule.use(CHAIN_ID.USE.SWC).tap((options) => {
194
- options.jsc.transform.react = {
166
+ rule.use(CHAIN_ID.USE.SWC).tap((options2) => {
167
+ options2.jsc.transform.react = {
195
168
  ...reactOptions
196
169
  };
197
- return options;
170
+ return options2;
198
171
  });
199
172
  if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) {
200
- chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options) => {
201
- options.jsc.transform.react = {
173
+ chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options2) => {
174
+ options2.jsc.transform.react = {
202
175
  ...reactOptions
203
176
  };
204
- return options;
177
+ return options2;
205
178
  });
206
179
  }
207
180
  if (!usingHMR) {
208
181
  return;
209
182
  }
183
+ chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
210
184
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
211
185
  chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
212
186
  });
@@ -238,7 +212,7 @@ var pluginReact = (options = {}) => ({
238
212
  pre: ["rsbuild:swc"],
239
213
  setup(api) {
240
214
  if (api.context.bundlerType === "rspack") {
241
- applyBasicReactSupport(api);
215
+ applyBasicReactSupport(api, options);
242
216
  }
243
217
  applyAntdSupport(api);
244
218
  applyArcoSupport(api);
package/dist/index.mjs CHANGED
@@ -134,67 +134,39 @@ var applySplitChunksRule = (api, options = {
134
134
  };
135
135
 
136
136
  // src/react.ts
137
- import { isUsingHMR, isClientCompiler, isProd as isProd2 } from "@rsbuild/shared";
138
- function getReactRefreshEntry(compiler) {
139
- const hot = compiler.options.devServer?.hot ?? true;
140
- const refresh = compiler.options.builtins?.react?.refresh ?? true;
141
- if (hot && refresh) {
142
- const reactRefreshEntryPath = __require.resolve(
143
- "@rspack/plugin-react-refresh/react-refresh-entry"
144
- );
145
- return reactRefreshEntryPath;
146
- }
147
- return null;
148
- }
149
- var setupCompiler = (compiler) => {
150
- if (!isClientCompiler(compiler)) {
151
- return;
152
- }
153
- const reactRefreshEntry = getReactRefreshEntry(compiler);
154
- if (!reactRefreshEntry) {
155
- return;
156
- }
157
- new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntry, {
158
- name: void 0
159
- }).apply(compiler);
160
- };
161
- var applyBasicReactSupport = (api) => {
162
- api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
163
- if (isProd2()) {
164
- return;
165
- }
166
- if (multiCompiler.compilers) {
167
- multiCompiler.compilers.forEach(setupCompiler);
168
- } else {
169
- setupCompiler(multiCompiler);
170
- }
171
- });
172
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd3, target }) => {
137
+ import path2 from "path";
138
+ import { isUsingHMR } from "@rsbuild/shared";
139
+ var REACT_REFRESH_PATH = __require.resolve("react-refresh");
140
+ var REACT_REFRESH_DIR_PATH = path2.dirname(REACT_REFRESH_PATH);
141
+ var applyBasicReactSupport = (api, options) => {
142
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
173
143
  const config = api.getNormalizedConfig();
174
- const usingHMR = isUsingHMR(config, { isProd: isProd3, target });
144
+ const usingHMR = isUsingHMR(config, { isProd: isProd2, target });
175
145
  const rule = chain.module.rule(CHAIN_ID.RULE.JS);
176
146
  const reactOptions = {
177
- development: !isProd3,
147
+ development: !isProd2,
178
148
  refresh: usingHMR,
179
- runtime: "automatic"
149
+ runtime: "automatic",
150
+ ...options.swcReactOptions
180
151
  };
181
- rule.use(CHAIN_ID.USE.SWC).tap((options) => {
182
- options.jsc.transform.react = {
152
+ rule.use(CHAIN_ID.USE.SWC).tap((options2) => {
153
+ options2.jsc.transform.react = {
183
154
  ...reactOptions
184
155
  };
185
- return options;
156
+ return options2;
186
157
  });
187
158
  if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) {
188
- chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options) => {
189
- options.jsc.transform.react = {
159
+ chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options2) => {
160
+ options2.jsc.transform.react = {
190
161
  ...reactOptions
191
162
  };
192
- return options;
163
+ return options2;
193
164
  });
194
165
  }
195
166
  if (!usingHMR) {
196
167
  return;
197
168
  }
169
+ chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
198
170
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
199
171
  chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
200
172
  });
@@ -226,7 +198,7 @@ var pluginReact = (options = {}) => ({
226
198
  pre: ["rsbuild:swc"],
227
199
  setup(api) {
228
200
  if (api.context.bundlerType === "rspack") {
229
- applyBasicReactSupport(api);
201
+ applyBasicReactSupport(api, options);
230
202
  }
231
203
  applyAntdSupport(api);
232
204
  applyArcoSupport(api);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "0.2.17",
3
+ "version": "0.3.0",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,15 +22,15 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@rspack/plugin-react-refresh": "0.4.5",
25
+ "@rspack/plugin-react-refresh": "0.5.0",
26
26
  "react-refresh": "^0.14.0",
27
- "@rsbuild/shared": "0.2.17"
27
+ "@rsbuild/shared": "0.3.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "16.x",
31
31
  "typescript": "^5.3.0",
32
- "@rsbuild/test-helper": "0.2.17",
33
- "@rsbuild/core": "0.2.17"
32
+ "@rsbuild/core": "0.3.0",
33
+ "@rsbuild/test-helper": "0.3.0"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public",