@rsbuild/plugin-react 0.0.0-next-20240112061202 → 0.0.0-next-20240227022717

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,8 +1,6 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
  import { SwcReactConfig } from '@rsbuild/shared';
3
3
 
4
- declare const isBeyondReact17: (cwd: string) => Promise<any>;
5
-
6
4
  type SplitReactChunkOptions = {
7
5
  /**
8
6
  * Whether to enable split chunking for React-related dependencies (e.g., react, react-dom, scheduler).
@@ -28,6 +26,7 @@ type PluginReactOptions = {
28
26
  */
29
27
  splitChunks?: SplitReactChunkOptions;
30
28
  };
29
+ declare const PLUGIN_REACT_NAME = "rsbuild:react";
31
30
  declare const pluginReact: (options?: PluginReactOptions) => RsbuildPlugin;
32
31
 
33
- export { PluginReactOptions, SplitReactChunkOptions, isBeyondReact17, pluginReact };
32
+ export { PLUGIN_REACT_NAME, type PluginReactOptions, type SplitReactChunkOptions, pluginReact };
package/dist/index.js CHANGED
@@ -30,80 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
- isBeyondReact17: () => isBeyondReact17,
33
+ PLUGIN_REACT_NAME: () => PLUGIN_REACT_NAME,
34
34
  pluginReact: () => pluginReact
35
35
  });
36
36
  module.exports = __toCommonJS(src_exports);
37
37
 
38
- // src/antd.ts
39
- var import_shared = require("@rsbuild/shared");
40
- var getAntdMajorVersion = (appDirectory) => {
41
- try {
42
- const pkgJsonPath = require.resolve("antd/package.json", {
43
- paths: [appDirectory]
44
- });
45
- const { version } = require(pkgJsonPath);
46
- return Number(version.split(".")[0]);
47
- } catch (err) {
48
- return null;
49
- }
50
- };
51
- var applyAntdSupport = (api) => {
52
- api.modifyRsbuildConfig((rsbuildConfig) => {
53
- rsbuildConfig.source ?? (rsbuildConfig.source = {});
54
- if (rsbuildConfig.source.transformImport === false || rsbuildConfig.source.transformImport?.some(
55
- (item) => item.libraryName === "antd"
56
- )) {
57
- return;
58
- }
59
- const antdMajorVersion = getAntdMajorVersion(api.context.rootPath);
60
- if (antdMajorVersion && antdMajorVersion < 5) {
61
- rsbuildConfig.source ?? (rsbuildConfig.source = {});
62
- rsbuildConfig.source.transformImport = [
63
- ...rsbuildConfig.source.transformImport || [],
64
- {
65
- libraryName: "antd",
66
- libraryDirectory: (0, import_shared.isServerTarget)(api.context.targets) ? "lib" : "es",
67
- style: true
68
- }
69
- ];
70
- }
71
- });
72
- };
73
-
74
- // src/arco.ts
75
- var import_shared2 = require("@rsbuild/shared");
76
- var applyArcoSupport = (api) => {
77
- const ARCO_NAME = "@arco-design/web-react";
78
- const ARCO_ICON = `${ARCO_NAME}/icon`;
79
- api.modifyRsbuildConfig((rsbuildConfig) => {
80
- const { transformImport = [] } = rsbuildConfig.source || {};
81
- if (transformImport === false || !(0, import_shared2.isPackageInstalled)(ARCO_NAME, api.context.rootPath)) {
82
- return;
83
- }
84
- const isUseSSR = (0, import_shared2.isServerTarget)(api.context.targets);
85
- if (!transformImport?.some((item) => item.libraryName === ARCO_NAME)) {
86
- transformImport.push({
87
- libraryName: ARCO_NAME,
88
- libraryDirectory: isUseSSR ? "lib" : "es",
89
- camelToDashComponentName: false,
90
- style: true
91
- });
92
- }
93
- if (!transformImport?.some((item) => item.libraryName === ARCO_ICON)) {
94
- transformImport.push({
95
- libraryName: ARCO_ICON,
96
- libraryDirectory: isUseSSR ? "react-icon-cjs" : "react-icon",
97
- camelToDashComponentName: false
98
- });
99
- }
100
- rsbuildConfig.source || (rsbuildConfig.source = {});
101
- rsbuildConfig.source.transformImport = transformImport;
102
- });
103
- };
104
-
105
38
  // src/splitChunks.ts
106
- var import_shared3 = require("@rsbuild/shared");
39
+ var import_shared = require("@rsbuild/shared");
107
40
  var applySplitChunksRule = (api, options = {
108
41
  react: true,
109
42
  router: true
@@ -114,7 +47,7 @@ var applySplitChunksRule = (api, options = {
114
47
  return;
115
48
  }
116
49
  const currentConfig = chain.optimization.splitChunks.values();
117
- if (!(0, import_shared3.isPlainObject)(currentConfig)) {
50
+ if (!(0, import_shared.isPlainObject)(currentConfig)) {
118
51
  return;
119
52
  }
120
53
  const extraGroups = {};
@@ -123,7 +56,7 @@ var applySplitChunksRule = (api, options = {
123
56
  "react",
124
57
  "react-dom",
125
58
  "scheduler",
126
- ...(0, import_shared3.isProd)() ? [] : ["react-refresh", /@rspack[\\/]plugin-react-refresh/]
59
+ ...(0, import_shared.isProd)() ? [] : ["react-refresh", /@rspack[\\/]plugin-react-refresh/]
127
60
  ];
128
61
  }
129
62
  if (options.router) {
@@ -139,9 +72,10 @@ var applySplitChunksRule = (api, options = {
139
72
  }
140
73
  chain.optimization.splitChunks({
141
74
  ...currentConfig,
75
+ // @ts-expect-error Rspack and Webpack uses different cacheGroups type
142
76
  cacheGroups: {
143
77
  ...currentConfig.cacheGroups,
144
- ...(0, import_shared3.createCacheGroups)(extraGroups)
78
+ ...(0, import_shared.createCacheGroups)(extraGroups)
145
79
  }
146
80
  });
147
81
  });
@@ -149,75 +83,53 @@ var applySplitChunksRule = (api, options = {
149
83
 
150
84
  // src/react.ts
151
85
  var import_node_path = __toESM(require("path"));
152
- var import_shared4 = require("@rsbuild/shared");
153
- var REACT_REFRESH_PATH = require.resolve("react-refresh");
154
- var REACT_REFRESH_DIR_PATH = import_node_path.default.dirname(REACT_REFRESH_PATH);
86
+ var import_shared2 = require("@rsbuild/shared");
155
87
  var applyBasicReactSupport = (api, options) => {
156
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
88
+ const REACT_REFRESH_PATH = require.resolve("react-refresh");
89
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd: isProd2, target }) => {
157
90
  const config = api.getNormalizedConfig();
158
- const usingHMR = (0, import_shared4.isUsingHMR)(config, { isProd: isProd2, target });
91
+ const usingHMR = (0, import_shared2.isUsingHMR)(config, { isProd: isProd2, target });
159
92
  const reactOptions = {
160
- development: !isProd2,
93
+ development: isDev,
161
94
  refresh: usingHMR,
162
95
  runtime: "automatic",
163
96
  ...options.swcReactOptions
164
97
  };
165
- (0, import_shared4.modifySwcLoaderOptions)({
98
+ (0, import_shared2.modifySwcLoaderOptions)({
166
99
  chain,
167
- modifier: (options2) => {
168
- return (0, import_shared4.deepmerge)(options2, {
169
- jsc: {
170
- transform: {
171
- react: reactOptions
172
- }
173
- }
174
- });
100
+ modifier: (opts) => {
101
+ var _a;
102
+ opts.jsc ?? (opts.jsc = {});
103
+ (_a = opts.jsc).transform ?? (_a.transform = {});
104
+ opts.jsc.transform.react = {
105
+ ...opts.jsc.transform.react,
106
+ ...reactOptions
107
+ };
108
+ return opts;
175
109
  }
176
110
  });
177
111
  if (!usingHMR) {
178
112
  return;
179
113
  }
180
- chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
114
+ chain.resolve.alias.set("react-refresh", import_node_path.default.dirname(REACT_REFRESH_PATH));
181
115
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
182
- chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
116
+ chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [{ include: [import_shared2.SCRIPT_REGEX] }]);
183
117
  });
184
118
  };
185
119
 
186
- // src/utils.ts
187
- var import_fs = __toESM(require("fs"));
188
- var import_semver = __toESM(require("@rsbuild/shared/semver"));
189
- var import_shared5 = require("@rsbuild/shared");
190
- var isBeyondReact17 = async (cwd) => {
191
- const pkgPath = await (0, import_shared5.findUp)({ cwd, filename: "package.json" });
192
- if (!pkgPath) {
193
- return false;
194
- }
195
- const pkgInfo = JSON.parse(import_fs.default.readFileSync(pkgPath, "utf8"));
196
- const deps = {
197
- ...pkgInfo.devDependencies,
198
- ...pkgInfo.dependencies
199
- };
200
- if (typeof deps.react !== "string") {
201
- return false;
202
- }
203
- return import_semver.default.satisfies(import_semver.default.minVersion(deps.react), ">=17.0.0");
204
- };
205
-
206
120
  // src/index.ts
121
+ var PLUGIN_REACT_NAME = "rsbuild:react";
207
122
  var pluginReact = (options = {}) => ({
208
- name: "rsbuild:react",
209
- pre: ["rsbuild:swc"],
123
+ name: PLUGIN_REACT_NAME,
210
124
  setup(api) {
211
125
  if (api.context.bundlerType === "rspack") {
212
126
  applyBasicReactSupport(api, options);
213
127
  }
214
- applyAntdSupport(api);
215
- applyArcoSupport(api);
216
128
  applySplitChunksRule(api, options?.splitChunks);
217
129
  }
218
130
  });
219
131
  // Annotate the CommonJS export names for ESM import in node:
220
132
  0 && (module.exports = {
221
- isBeyondReact17,
133
+ PLUGIN_REACT_NAME,
222
134
  pluginReact
223
135
  });
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ import { createRequire } from 'module';
2
+ var require = createRequire(import.meta['url']);
3
+
1
4
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
5
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
6
  }) : x)(function(x) {
@@ -6,84 +9,10 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
9
  throw Error('Dynamic require of "' + x + '" is not supported');
7
10
  });
8
11
 
9
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.45.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
12
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.47.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
10
13
  import { fileURLToPath } from "url";
11
14
  import path from "path";
12
15
 
13
- // ../../scripts/requireShims.js
14
- import { createRequire } from "module";
15
- global.require = createRequire(import.meta.url);
16
-
17
- // src/antd.ts
18
- import { isServerTarget } from "@rsbuild/shared";
19
- var getAntdMajorVersion = (appDirectory) => {
20
- try {
21
- const pkgJsonPath = __require.resolve("antd/package.json", {
22
- paths: [appDirectory]
23
- });
24
- const { version } = __require(pkgJsonPath);
25
- return Number(version.split(".")[0]);
26
- } catch (err) {
27
- return null;
28
- }
29
- };
30
- var applyAntdSupport = (api) => {
31
- api.modifyRsbuildConfig((rsbuildConfig) => {
32
- rsbuildConfig.source ?? (rsbuildConfig.source = {});
33
- if (rsbuildConfig.source.transformImport === false || rsbuildConfig.source.transformImport?.some(
34
- (item) => item.libraryName === "antd"
35
- )) {
36
- return;
37
- }
38
- const antdMajorVersion = getAntdMajorVersion(api.context.rootPath);
39
- if (antdMajorVersion && antdMajorVersion < 5) {
40
- rsbuildConfig.source ?? (rsbuildConfig.source = {});
41
- rsbuildConfig.source.transformImport = [
42
- ...rsbuildConfig.source.transformImport || [],
43
- {
44
- libraryName: "antd",
45
- libraryDirectory: isServerTarget(api.context.targets) ? "lib" : "es",
46
- style: true
47
- }
48
- ];
49
- }
50
- });
51
- };
52
-
53
- // src/arco.ts
54
- import {
55
- isServerTarget as isServerTarget2,
56
- isPackageInstalled
57
- } from "@rsbuild/shared";
58
- var applyArcoSupport = (api) => {
59
- const ARCO_NAME = "@arco-design/web-react";
60
- const ARCO_ICON = `${ARCO_NAME}/icon`;
61
- api.modifyRsbuildConfig((rsbuildConfig) => {
62
- const { transformImport = [] } = rsbuildConfig.source || {};
63
- if (transformImport === false || !isPackageInstalled(ARCO_NAME, api.context.rootPath)) {
64
- return;
65
- }
66
- const isUseSSR = isServerTarget2(api.context.targets);
67
- if (!transformImport?.some((item) => item.libraryName === ARCO_NAME)) {
68
- transformImport.push({
69
- libraryName: ARCO_NAME,
70
- libraryDirectory: isUseSSR ? "lib" : "es",
71
- camelToDashComponentName: false,
72
- style: true
73
- });
74
- }
75
- if (!transformImport?.some((item) => item.libraryName === ARCO_ICON)) {
76
- transformImport.push({
77
- libraryName: ARCO_ICON,
78
- libraryDirectory: isUseSSR ? "react-icon-cjs" : "react-icon",
79
- camelToDashComponentName: false
80
- });
81
- }
82
- rsbuildConfig.source || (rsbuildConfig.source = {});
83
- rsbuildConfig.source.transformImport = transformImport;
84
- });
85
- };
86
-
87
16
  // src/splitChunks.ts
88
17
  import {
89
18
  isProd,
@@ -125,6 +54,7 @@ var applySplitChunksRule = (api, options = {
125
54
  }
126
55
  chain.optimization.splitChunks({
127
56
  ...currentConfig,
57
+ // @ts-expect-error Rspack and Webpack uses different cacheGroups type
128
58
  cacheGroups: {
129
59
  ...currentConfig.cacheGroups,
130
60
  ...createCacheGroups(extraGroups)
@@ -136,77 +66,55 @@ var applySplitChunksRule = (api, options = {
136
66
  // src/react.ts
137
67
  import path2 from "path";
138
68
  import {
139
- deepmerge,
140
69
  isUsingHMR,
70
+ SCRIPT_REGEX,
141
71
  modifySwcLoaderOptions
142
72
  } from "@rsbuild/shared";
143
- var REACT_REFRESH_PATH = __require.resolve("react-refresh");
144
- var REACT_REFRESH_DIR_PATH = path2.dirname(REACT_REFRESH_PATH);
145
73
  var applyBasicReactSupport = (api, options) => {
146
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
74
+ const REACT_REFRESH_PATH = __require.resolve("react-refresh");
75
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isDev, isProd: isProd2, target }) => {
147
76
  const config = api.getNormalizedConfig();
148
77
  const usingHMR = isUsingHMR(config, { isProd: isProd2, target });
149
78
  const reactOptions = {
150
- development: !isProd2,
79
+ development: isDev,
151
80
  refresh: usingHMR,
152
81
  runtime: "automatic",
153
82
  ...options.swcReactOptions
154
83
  };
155
84
  modifySwcLoaderOptions({
156
85
  chain,
157
- modifier: (options2) => {
158
- return deepmerge(options2, {
159
- jsc: {
160
- transform: {
161
- react: reactOptions
162
- }
163
- }
164
- });
86
+ modifier: (opts) => {
87
+ var _a;
88
+ opts.jsc ?? (opts.jsc = {});
89
+ (_a = opts.jsc).transform ?? (_a.transform = {});
90
+ opts.jsc.transform.react = {
91
+ ...opts.jsc.transform.react,
92
+ ...reactOptions
93
+ };
94
+ return opts;
165
95
  }
166
96
  });
167
97
  if (!usingHMR) {
168
98
  return;
169
99
  }
170
- chain.resolve.alias.set("react-refresh", REACT_REFRESH_DIR_PATH);
100
+ chain.resolve.alias.set("react-refresh", path2.dirname(REACT_REFRESH_PATH));
171
101
  const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh");
172
- chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
102
+ chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [{ include: [SCRIPT_REGEX] }]);
173
103
  });
174
104
  };
175
105
 
176
- // src/utils.ts
177
- import fs from "fs";
178
- import semver from "@rsbuild/shared/semver";
179
- import { findUp } from "@rsbuild/shared";
180
- var isBeyondReact17 = async (cwd) => {
181
- const pkgPath = await findUp({ cwd, filename: "package.json" });
182
- if (!pkgPath) {
183
- return false;
184
- }
185
- const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
186
- const deps = {
187
- ...pkgInfo.devDependencies,
188
- ...pkgInfo.dependencies
189
- };
190
- if (typeof deps.react !== "string") {
191
- return false;
192
- }
193
- return semver.satisfies(semver.minVersion(deps.react), ">=17.0.0");
194
- };
195
-
196
106
  // src/index.ts
107
+ var PLUGIN_REACT_NAME = "rsbuild:react";
197
108
  var pluginReact = (options = {}) => ({
198
- name: "rsbuild:react",
199
- pre: ["rsbuild:swc"],
109
+ name: PLUGIN_REACT_NAME,
200
110
  setup(api) {
201
111
  if (api.context.bundlerType === "rspack") {
202
112
  applyBasicReactSupport(api, options);
203
113
  }
204
- applyAntdSupport(api);
205
- applyArcoSupport(api);
206
114
  applySplitChunksRule(api, options?.splitChunks);
207
115
  }
208
116
  });
209
117
  export {
210
- isBeyondReact17,
118
+ PLUGIN_REACT_NAME,
211
119
  pluginReact
212
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "0.0.0-next-20240112061202",
3
+ "version": "0.0.0-next-20240227022717",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,15 +22,18 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@rspack/plugin-react-refresh": "0.5.0",
25
+ "@rspack/plugin-react-refresh": "0.5.4",
26
26
  "react-refresh": "^0.14.0",
27
- "@rsbuild/shared": "0.0.0-next-20240112061202"
27
+ "@rsbuild/shared": "0.0.0-next-20240227022717"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "16.x",
31
31
  "typescript": "^5.3.0",
32
- "@rsbuild/core": "0.0.0-next-20240112061202",
33
- "@scripts/test-helper": "0.0.0-next-20240112061202"
32
+ "@rsbuild/core": "0.0.0-next-20240227022717",
33
+ "@scripts/test-helper": "0.0.0-next-20240227022717"
34
+ },
35
+ "peerDependencies": {
36
+ "@rsbuild/core": "0.0.0-next-20240227022717"
34
37
  },
35
38
  "publishConfig": {
36
39
  "access": "public",