@rsbuild/plugin-react 0.2.18 → 0.3.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 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.js CHANGED
@@ -148,66 +148,36 @@ 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
- };
153
+ var REACT_REFRESH_PATH = require.resolve("react-refresh");
154
+ var REACT_REFRESH_DIR_PATH = import_node_path.default.dirname(REACT_REFRESH_PATH);
173
155
  var applyBasicReactSupport = (api, options) => {
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 }) => {
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 });
187
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
158
+ const usingHMR = (0, import_shared4.isUsingHMR)(config, { isProd: isProd2, target });
188
159
  const reactOptions = {
189
- development: !isProd3,
160
+ development: !isProd2,
190
161
  refresh: usingHMR,
191
162
  runtime: "automatic",
192
163
  ...options.swcReactOptions
193
164
  };
194
- rule.use(CHAIN_ID.USE.SWC).tap((options2) => {
195
- options2.jsc.transform.react = {
196
- ...reactOptions
197
- };
198
- return options2;
165
+ (0, import_shared4.modifySwcLoaderOptions)({
166
+ chain,
167
+ modifier: (options2) => {
168
+ return (0, import_shared4.deepmerge)(options2, {
169
+ jsc: {
170
+ transform: {
171
+ react: reactOptions
172
+ }
173
+ }
174
+ });
175
+ }
199
176
  });
200
- if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) {
201
- chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options2) => {
202
- options2.jsc.transform.react = {
203
- ...reactOptions
204
- };
205
- return options2;
206
- });
207
- }
208
177
  if (!usingHMR) {
209
178
  return;
210
179
  }
180
+ chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
211
181
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
212
182
  chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
213
183
  });
package/dist/index.mjs CHANGED
@@ -134,72 +134,40 @@ var applySplitChunksRule = (api, options = {
134
134
  };
135
135
 
136
136
  // src/react.ts
137
+ import path2 from "path";
137
138
  import {
138
- isProd as isProd2,
139
+ deepmerge,
139
140
  isUsingHMR,
140
- isClientCompiler
141
+ modifySwcLoaderOptions
141
142
  } from "@rsbuild/shared";
142
- function getReactRefreshEntry(compiler) {
143
- const hot = compiler.options.devServer?.hot ?? true;
144
- const refresh = compiler.options.builtins?.react?.refresh ?? true;
145
- if (hot && refresh) {
146
- const reactRefreshEntryPath = __require.resolve(
147
- "@rspack/plugin-react-refresh/react-refresh-entry"
148
- );
149
- return reactRefreshEntryPath;
150
- }
151
- return null;
152
- }
153
- var setupCompiler = (compiler) => {
154
- if (!isClientCompiler(compiler)) {
155
- return;
156
- }
157
- const reactRefreshEntry = getReactRefreshEntry(compiler);
158
- if (!reactRefreshEntry) {
159
- return;
160
- }
161
- new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntry, {
162
- name: void 0
163
- }).apply(compiler);
164
- };
143
+ var REACT_REFRESH_PATH = __require.resolve("react-refresh");
144
+ var REACT_REFRESH_DIR_PATH = path2.dirname(REACT_REFRESH_PATH);
165
145
  var applyBasicReactSupport = (api, options) => {
166
- api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
167
- if (isProd2()) {
168
- return;
169
- }
170
- if (multiCompiler.compilers) {
171
- multiCompiler.compilers.forEach(setupCompiler);
172
- } else {
173
- setupCompiler(multiCompiler);
174
- }
175
- });
176
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd3, target }) => {
146
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
177
147
  const config = api.getNormalizedConfig();
178
- const usingHMR = isUsingHMR(config, { isProd: isProd3, target });
179
- const rule = chain.module.rule(CHAIN_ID.RULE.JS);
148
+ const usingHMR = isUsingHMR(config, { isProd: isProd2, target });
180
149
  const reactOptions = {
181
- development: !isProd3,
150
+ development: !isProd2,
182
151
  refresh: usingHMR,
183
152
  runtime: "automatic",
184
153
  ...options.swcReactOptions
185
154
  };
186
- rule.use(CHAIN_ID.USE.SWC).tap((options2) => {
187
- options2.jsc.transform.react = {
188
- ...reactOptions
189
- };
190
- return options2;
155
+ modifySwcLoaderOptions({
156
+ chain,
157
+ modifier: (options2) => {
158
+ return deepmerge(options2, {
159
+ jsc: {
160
+ transform: {
161
+ react: reactOptions
162
+ }
163
+ }
164
+ });
165
+ }
191
166
  });
192
- if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) {
193
- chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options2) => {
194
- options2.jsc.transform.react = {
195
- ...reactOptions
196
- };
197
- return options2;
198
- });
199
- }
200
167
  if (!usingHMR) {
201
168
  return;
202
169
  }
170
+ chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
203
171
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
204
172
  chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
205
173
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "0.2.18",
3
+ "version": "0.3.1",
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.18"
27
+ "@rsbuild/shared": "0.3.1"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "16.x",
31
31
  "typescript": "^5.3.0",
32
- "@rsbuild/core": "0.2.18",
33
- "@rsbuild/test-helper": "0.2.18"
32
+ "@rsbuild/core": "0.3.1",
33
+ "@scripts/test-helper": "1.0.0"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public",