@rsbuild/plugin-react 0.0.10 → 0.0.12

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
@@ -1,2 +1,2 @@
1
- import { DefaultRsbuildPlugin } from '@rsbuild/shared';
2
- export declare const pluginReact: () => DefaultRsbuildPlugin;
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ export declare const pluginReact: () => RsbuildPlugin;
package/dist/index.js CHANGED
@@ -29,7 +29,9 @@ const pluginReact = () => ({
29
29
  name: "plugin-react",
30
30
  pre: ["plugin-swc"],
31
31
  setup(api) {
32
- (0, import_react.applyBasicReactSupport)(api);
32
+ if (api.context.bundlerType === "rspack") {
33
+ (0, import_react.applyBasicReactSupport)(api);
34
+ }
33
35
  (0, import_antd.applyAntdSupport)(api);
34
36
  (0, import_arco.applyArcoSupport)(api);
35
37
  (0, import_splitChunks.applySplitChunksRule)(api);
package/dist/react.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { type SharedRsbuildPluginAPI } from '@rsbuild/shared';
2
- export declare const applyBasicReactSupport: (api: SharedRsbuildPluginAPI) => void;
1
+ import type { RsbuildPluginAPI } from '@rsbuild/core/rspack-provider';
2
+ export declare const applyBasicReactSupport: (api: RsbuildPluginAPI) => void;
package/dist/react.js CHANGED
@@ -32,7 +32,42 @@ __export(react_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(react_exports);
34
34
  var import_shared = require("@rsbuild/shared");
35
- const applyRspack = (api) => {
35
+ function getReactRefreshEntry(compiler) {
36
+ var _a, _b, _c, _d, _e;
37
+ const hot = (_b = (_a = compiler.options.devServer) == null ? void 0 : _a.hot) != null ? _b : true;
38
+ const refresh = (_e = (_d = (_c = compiler.options.builtins) == null ? void 0 : _c.react) == null ? void 0 : _d.refresh) != null ? _e : true;
39
+ if (hot && refresh) {
40
+ const reactRefreshEntryPath = require.resolve("@rspack/plugin-react-refresh/react-refresh-entry");
41
+ return reactRefreshEntryPath;
42
+ }
43
+ return null;
44
+ }
45
+ const setupCompiler = (compiler) => {
46
+ if (!(0, import_shared.isClientCompiler)(compiler)) {
47
+ return;
48
+ }
49
+ const reactRefreshEntry = getReactRefreshEntry(compiler);
50
+ if (!reactRefreshEntry) {
51
+ return;
52
+ }
53
+ for (const key in compiler.options.entry) {
54
+ compiler.options.entry[key].import = [
55
+ reactRefreshEntry,
56
+ ...compiler.options.entry[key].import || []
57
+ ];
58
+ }
59
+ };
60
+ const applyBasicReactSupport = (api) => {
61
+ api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
62
+ if ((0, import_shared.isProd)()) {
63
+ return;
64
+ }
65
+ if (multiCompiler.compilers) {
66
+ multiCompiler.compilers.forEach(setupCompiler);
67
+ } else {
68
+ setupCompiler(multiCompiler);
69
+ }
70
+ });
36
71
  api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
37
72
  const config = api.getNormalizedConfig();
38
73
  const usingHMR = (0, import_shared.isUsingHMR)(config, { isProd: isProd2, target });
@@ -60,69 +95,13 @@ const applyRspack = (api) => {
60
95
  return;
61
96
  }
62
97
  const { default: ReactRefreshRspackPlugin } = await Promise.resolve().then(() => __toESM(require(
98
+ // TODO https://github.com/web-infra-dev/rspack/issues/4471
63
99
  // @ts-expect-error
64
100
  "@rspack/plugin-react-refresh"
65
101
  )));
66
102
  chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
67
103
  });
68
104
  };
69
- const applyWebpack = (api) => {
70
- api.modifyRsbuildConfig(async (config, { mergeRsbuildConfig }) => {
71
- const babelConfig = {
72
- tools: {
73
- babel(_, { addPresets, addPlugins }) {
74
- const isNewJsx = (0, import_shared.isBeyondReact17)(api.context.rootPath);
75
- const presetReactOptions = {
76
- development: !(0, import_shared.isProd)(),
77
- // Will use the native built-in instead of trying to polyfill
78
- useBuiltIns: true,
79
- useSpread: false,
80
- runtime: isNewJsx ? "automatic" : "classic"
81
- };
82
- addPresets([
83
- [require.resolve("@babel/preset-react"), presetReactOptions]
84
- ]);
85
- if ((0, import_shared.isProd)()) {
86
- addPlugins([
87
- [
88
- require.resolve("babel-plugin-transform-react-remove-prop-types"),
89
- { removeImport: true }
90
- ]
91
- ]);
92
- }
93
- }
94
- }
95
- };
96
- return mergeRsbuildConfig(babelConfig, config);
97
- });
98
- api.modifyWebpackChain(async (chain, utils) => {
99
- const config = api.getNormalizedConfig();
100
- if (!(0, import_shared.isUsingHMR)(config, utils)) {
101
- return;
102
- }
103
- const { CHAIN_ID } = utils;
104
- const { default: ReactFastRefreshPlugin } = await Promise.resolve().then(() => __toESM(require("@pmmmwh/react-refresh-webpack-plugin")));
105
- const useTsLoader = Boolean(config.tools.tsLoader);
106
- const rule = useTsLoader ? chain.module.rule(CHAIN_ID.RULE.TS) : chain.module.rule(CHAIN_ID.RULE.JS);
107
- rule.use(CHAIN_ID.USE.BABEL).tap((options) => ({
108
- ...options,
109
- plugins: [
110
- ...options.plugins || [],
111
- [require.resolve("react-refresh/babel"), { skipEnvCheck: true }]
112
- ]
113
- }));
114
- chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactFastRefreshPlugin, [
115
- {
116
- overlay: false,
117
- exclude: [/node_modules/]
118
- }
119
- ]);
120
- });
121
- };
122
- const applyBasicReactSupport = (api) => {
123
- const { bundlerType } = api.context;
124
- bundlerType === "rspack" ? applyRspack(api) : applyWebpack(api);
125
- };
126
105
  // Annotate the CommonJS export names for ESM import in node:
127
106
  0 && (module.exports = {
128
107
  applyBasicReactSupport
package/package.json CHANGED
@@ -1,45 +1,42 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/web-infra-dev/rsbuild",
8
8
  "directory": "packages/plugin-react"
9
9
  },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
10
+ "license": "MIT",
12
11
  "exports": {
13
12
  ".": {
14
13
  "types": "./dist/index.d.ts",
15
14
  "default": "./dist/index.js"
16
15
  }
17
16
  },
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
18
19
  "files": [
19
20
  "dist"
20
21
  ],
21
- "license": "MIT",
22
22
  "dependencies": {
23
- "@babel/preset-react": "^7.22.15",
24
- "@pmmmwh/react-refresh-webpack-plugin": "0.5.10",
25
23
  "@rspack/plugin-react-refresh": "0.3.8",
26
- "babel-plugin-transform-react-remove-prop-types": "0.4.24",
27
- "@rsbuild/shared": "0.0.10"
24
+ "react-refresh": "^0.14.0",
25
+ "@rsbuild/shared": "0.0.12"
28
26
  },
29
27
  "devDependencies": {
30
28
  "@types/node": "^16",
31
- "typescript": "^5",
32
- "@rsbuild/core": "0.0.10",
33
- "@rsbuild/test-helper": "0.0.10",
34
- "@rsbuild/webpack": "0.0.9"
29
+ "typescript": "^5.2.2",
30
+ "@rsbuild/core": "0.0.12",
31
+ "@rsbuild/test-helper": "0.0.12"
35
32
  },
36
33
  "publishConfig": {
37
- "registry": "https://registry.npmjs.org/",
38
34
  "access": "public",
39
- "provenance": true
35
+ "provenance": true,
36
+ "registry": "https://registry.npmjs.org/"
40
37
  },
41
38
  "scripts": {
42
- "dev": "modern build --watch",
43
- "build": "modern build"
39
+ "build": "modern build",
40
+ "dev": "modern build --watch"
44
41
  }
45
42
  }