@rsbuild/plugin-react 2.0.0-alpha.3 → 2.0.0-rc.2

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.js CHANGED
@@ -1,54 +1,39 @@
1
1
  import { createRequire } from "node:module";
2
2
  import node_path from "node:path";
3
- let react_require = createRequire(import.meta.url);
4
- function applyBasicReactSupport(api, options) {
5
- let REACT_REFRESH_PATH = options.fastRefresh ? react_require.resolve('react-refresh') : '';
6
- api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
7
- let isDev = 'development' === config.mode, usingHMR = isDev && config.dev.hmr && 'web' === config.output.target;
8
- return mergeEnvironmentConfig({
9
- tools: {
10
- swc: {
11
- jsc: {
12
- parser: {
13
- syntax: "typescript",
14
- tsx: !0
15
- },
16
- transform: {
17
- react: {
18
- development: isDev,
19
- refresh: usingHMR && options.fastRefresh,
20
- runtime: 'automatic',
21
- ...options.swcReactOptions
22
- }
23
- }
24
- }
25
- }
26
- }
27
- }, config);
28
- }), options.swcReactOptions?.runtime === 'preserve' && api.modifyBundlerChain((chain)=>{
29
- chain.module.parser.merge({
30
- javascript: {
31
- jsx: !0
32
- }
33
- });
34
- }), api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isDev, target })=>{
3
+ function applySplitChunksRule(api, options) {
4
+ api.modifyBundlerChain((chain, { environment, isProd })=>{
35
5
  let { config } = environment;
36
- if (!(isDev && config.dev.hmr && 'web' === target) || !options.fastRefresh) return;
37
- chain.resolve.alias.set('react-refresh', node_path.dirname(REACT_REFRESH_PATH));
38
- let { ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh"), jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
39
- chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
40
- {
41
- test: jsRule.get('test'),
42
- include: jsRule.include.values(),
43
- exclude: jsRule.exclude.values(),
44
- resourceQuery: {
45
- not: /^\?raw$/
46
- },
47
- ...options.reactRefreshOptions
6
+ if (!((config)=>{
7
+ let { performance, splitChunks } = config;
8
+ return performance.chunkSplit ? performance.chunkSplit?.strategy === 'split-by-experience' : 'object' == typeof splitChunks && (!splitChunks.preset || 'default' === splitChunks.preset);
9
+ })(config) || 'web' !== config.output.target || !1 === options) return;
10
+ let normalizedOptions = !0 === options ? {
11
+ react: !0,
12
+ router: !0
13
+ } : options, currentConfig = chain.optimization.splitChunks.values();
14
+ if ('object' != typeof currentConfig) return;
15
+ let extraGroups = {};
16
+ normalizedOptions.react && (extraGroups.react = {
17
+ name: 'lib-react',
18
+ test: isProd ? /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/,
19
+ priority: 0
20
+ }), normalizedOptions.router && (extraGroups.router = {
21
+ name: 'lib-router',
22
+ test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
23
+ priority: 0
24
+ }), Object.keys(extraGroups).length && chain.optimization.splitChunks({
25
+ ...currentConfig,
26
+ cacheGroups: {
27
+ ...extraGroups,
28
+ ...currentConfig.cacheGroups
48
29
  }
49
- ]);
30
+ });
50
31
  });
51
32
  }
33
+ let src_require = createRequire(import.meta.url), PLUGIN_REACT_NAME = 'rsbuild:react';
34
+ function assertCoreVersion(version) {
35
+ if ('1' === version.split('.')[0]) throw Error('"@rsbuild/plugin-react" v2 requires "@rsbuild/core" >= 2.0. Please upgrade "@rsbuild/core" or use "@rsbuild/plugin-react" v1.');
36
+ }
52
37
  function applyReactProfiler(api) {
53
38
  let hasReactDomClientCache;
54
39
  api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
@@ -70,7 +55,7 @@ function applyReactProfiler(api) {
70
55
  isProd && (chain.resolve.alias.set((()=>{
71
56
  if (void 0 !== hasReactDomClientCache) return hasReactDomClientCache;
72
57
  try {
73
- react_require.resolve('react-dom/client', {
58
+ src_require.resolve('react-dom/client', {
74
59
  paths: [
75
60
  api.context.rootPath
76
61
  ]
@@ -82,43 +67,57 @@ function applyReactProfiler(api) {
82
67
  })() ? 'react-dom/client$' : 'react-dom$', 'react-dom/profiling'), chain.resolve.alias.set('scheduler/tracing', 'scheduler/tracing-profiling'));
83
68
  });
84
69
  }
85
- function applySplitChunksRule(api, options) {
86
- api.modifyBundlerChain((chain, { environment, isProd })=>{
87
- let { config } = environment;
88
- if ('split-by-experience' !== config.performance.chunkSplit.strategy || !1 === options) return;
89
- let normalizedOptions = !0 === options ? {
90
- react: !0,
91
- router: !0
92
- } : options, currentConfig = chain.optimization.splitChunks.values();
93
- if ('object' != typeof currentConfig) return;
94
- let extraGroups = {};
95
- normalizedOptions.react && (extraGroups.react = {
96
- name: 'lib-react',
97
- test: isProd ? /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/,
98
- priority: 0
99
- }), normalizedOptions.router && (extraGroups.router = {
100
- name: 'lib-router',
101
- test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
102
- priority: 0
103
- }), Object.keys(extraGroups).length && chain.optimization.splitChunks({
104
- ...currentConfig,
105
- cacheGroups: {
106
- ...extraGroups,
107
- ...currentConfig.cacheGroups
108
- }
109
- });
110
- });
111
- }
112
- let PLUGIN_REACT_NAME = 'rsbuild:react', pluginReact = (options = {})=>({
70
+ let pluginReact = (options = {})=>({
113
71
  name: PLUGIN_REACT_NAME,
114
72
  setup (api) {
73
+ assertCoreVersion(api.context.version);
115
74
  let finalOptions = {
116
75
  fastRefresh: !0,
117
76
  splitChunks: !0,
118
77
  enableProfiler: !1,
119
78
  ...options
120
- };
121
- 'rspack' === api.context.bundlerType && (applyBasicReactSupport(api, finalOptions), finalOptions.enableProfiler && applyReactProfiler(api)), applySplitChunksRule(api, finalOptions.splitChunks);
79
+ }, reactRefreshPath = finalOptions.fastRefresh ? src_require.resolve('react-refresh') : '';
80
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
81
+ let isDev = 'development' === config.mode, usingHMR = isDev && config.dev.hmr && 'web' === config.output.target;
82
+ return mergeEnvironmentConfig({
83
+ tools: {
84
+ swc: {
85
+ jsc: {
86
+ transform: {
87
+ react: {
88
+ development: isDev,
89
+ refresh: usingHMR && finalOptions.fastRefresh,
90
+ runtime: 'automatic',
91
+ ...finalOptions.swcReactOptions
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }, config);
98
+ }), finalOptions.swcReactOptions?.runtime === 'preserve' && api.modifyBundlerChain((chain)=>{
99
+ chain.module.parser.merge({
100
+ javascript: {
101
+ jsx: !0
102
+ }
103
+ });
104
+ }), api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isDev, target })=>{
105
+ let { config } = environment;
106
+ if (!(isDev && config.dev.hmr && 'web' === target) || !finalOptions.fastRefresh) return;
107
+ chain.resolve.alias.set('react-refresh', node_path.dirname(reactRefreshPath));
108
+ let { ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh"), jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
109
+ chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
110
+ {
111
+ test: jsRule.get('test'),
112
+ include: jsRule.include.values(),
113
+ exclude: jsRule.exclude.values(),
114
+ resourceQuery: {
115
+ not: /^\?raw$/
116
+ },
117
+ ...finalOptions.reactRefreshOptions
118
+ }
119
+ ]);
120
+ }), applySplitChunksRule(api, finalOptions.splitChunks), finalOptions.enableProfiler && applyReactProfiler(api);
122
121
  }
123
122
  });
124
123
  export { PLUGIN_REACT_NAME, pluginReact };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-react",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-rc.2",
4
4
  "description": "React plugin for Rsbuild",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,36 +12,39 @@
12
12
  "exports": {
13
13
  ".": {
14
14
  "types": "./dist/index.d.ts",
15
- "import": "./dist/index.js",
16
- "require": "./dist/index.cjs"
15
+ "default": "./dist/index.js"
17
16
  }
18
17
  },
19
- "main": "./dist/index.cjs",
20
18
  "types": "./dist/index.d.ts",
21
19
  "files": [
22
20
  "dist"
23
21
  ],
24
22
  "dependencies": {
25
- "@rspack/plugin-react-refresh": "^1.6.0",
23
+ "@rspack/plugin-react-refresh": "2.0.0",
26
24
  "react-refresh": "^0.18.0"
27
25
  },
28
26
  "devDependencies": {
29
- "@rslib/core": "0.19.3",
30
- "@types/node": "^24.10.9",
31
- "typescript": "^5.9.3",
32
- "@scripts/test-helper": "1.0.1",
33
- "@rsbuild/core": "2.0.0-alpha.3"
27
+ "@rsbuild/core-v1": "npm:@rsbuild/core@^1.7.5",
28
+ "@rslib/core": "0.21.0",
29
+ "@types/node": "^24.12.2",
30
+ "typescript": "^6.0.2",
31
+ "@rsbuild/core": "2.0.0-rc.1",
32
+ "@scripts/test-helper": "1.0.0"
34
33
  },
35
34
  "peerDependencies": {
36
35
  "@rsbuild/core": "^2.0.0-0"
37
36
  },
37
+ "peerDependenciesMeta": {
38
+ "@rsbuild/core": {
39
+ "optional": true
40
+ }
41
+ },
38
42
  "publishConfig": {
39
43
  "access": "public",
40
44
  "registry": "https://registry.npmjs.org/"
41
45
  },
42
46
  "scripts": {
43
- "build": "rslib build",
44
- "dev": "rslib build -w",
45
- "bump": "pnpx bumpp --no-tag"
47
+ "build": "rslib",
48
+ "dev": "rslib -w"
46
49
  }
47
50
  }
package/dist/index.cjs DELETED
@@ -1,155 +0,0 @@
1
- "use strict";
2
- const __rslib_import_meta_url__ = "u" < typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
3
- var __webpack_require__ = {};
4
- __webpack_require__.n = (module)=>{
5
- var getter = module && module.__esModule ? ()=>module.default : ()=>module;
6
- return __webpack_require__.d(getter, {
7
- a: getter
8
- }), getter;
9
- }, __webpack_require__.d = (exports1, definition)=>{
10
- for(var key in definition)__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key) && Object.defineProperty(exports1, key, {
11
- enumerable: !0,
12
- get: definition[key]
13
- });
14
- }, __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop), __webpack_require__.r = (exports1)=>{
15
- "u" > typeof Symbol && Symbol.toStringTag && Object.defineProperty(exports1, Symbol.toStringTag, {
16
- value: 'Module'
17
- }), Object.defineProperty(exports1, '__esModule', {
18
- value: !0
19
- });
20
- };
21
- var __webpack_exports__ = {};
22
- __webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
23
- PLUGIN_REACT_NAME: ()=>PLUGIN_REACT_NAME,
24
- pluginReact: ()=>pluginReact
25
- });
26
- const external_node_module_namespaceObject = require("node:module"), external_node_path_namespaceObject = require("node:path");
27
- var external_node_path_default = __webpack_require__.n(external_node_path_namespaceObject);
28
- const react_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
29
- function applyBasicReactSupport(api, options) {
30
- let REACT_REFRESH_PATH = options.fastRefresh ? react_require.resolve('react-refresh') : '';
31
- api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
32
- let isDev = 'development' === config.mode, usingHMR = isDev && config.dev.hmr && 'web' === config.output.target;
33
- return mergeEnvironmentConfig({
34
- tools: {
35
- swc: {
36
- jsc: {
37
- parser: {
38
- syntax: "typescript",
39
- tsx: !0
40
- },
41
- transform: {
42
- react: {
43
- development: isDev,
44
- refresh: usingHMR && options.fastRefresh,
45
- runtime: 'automatic',
46
- ...options.swcReactOptions
47
- }
48
- }
49
- }
50
- }
51
- }
52
- }, config);
53
- }), options.swcReactOptions?.runtime === 'preserve' && api.modifyBundlerChain((chain)=>{
54
- chain.module.parser.merge({
55
- javascript: {
56
- jsx: !0
57
- }
58
- });
59
- }), api.modifyBundlerChain(async (chain, { CHAIN_ID, environment, isDev, target })=>{
60
- let { config } = environment;
61
- if (!(isDev && config.dev.hmr && 'web' === target) || !options.fastRefresh) return;
62
- chain.resolve.alias.set('react-refresh', external_node_path_default().dirname(REACT_REFRESH_PATH));
63
- let { ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh"), jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
64
- chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [
65
- {
66
- test: jsRule.get('test'),
67
- include: jsRule.include.values(),
68
- exclude: jsRule.exclude.values(),
69
- resourceQuery: {
70
- not: /^\?raw$/
71
- },
72
- ...options.reactRefreshOptions
73
- }
74
- ]);
75
- });
76
- }
77
- function applyReactProfiler(api) {
78
- let hasReactDomClientCache;
79
- api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig })=>{
80
- if ('production' === config.mode) return mergeEnvironmentConfig(config, {
81
- output: {
82
- minify: {
83
- jsOptions: {
84
- minimizerOptions: {
85
- mangle: {
86
- keep_classnames: !0,
87
- keep_fnames: !0
88
- }
89
- }
90
- }
91
- }
92
- }
93
- });
94
- }), api.modifyBundlerChain((chain, { isProd })=>{
95
- isProd && (chain.resolve.alias.set((()=>{
96
- if (void 0 !== hasReactDomClientCache) return hasReactDomClientCache;
97
- try {
98
- react_require.resolve('react-dom/client', {
99
- paths: [
100
- api.context.rootPath
101
- ]
102
- }), hasReactDomClientCache = !0;
103
- } catch {
104
- hasReactDomClientCache = !1;
105
- }
106
- return hasReactDomClientCache;
107
- })() ? 'react-dom/client$' : 'react-dom$', 'react-dom/profiling'), chain.resolve.alias.set('scheduler/tracing', 'scheduler/tracing-profiling'));
108
- });
109
- }
110
- function applySplitChunksRule(api, options) {
111
- api.modifyBundlerChain((chain, { environment, isProd })=>{
112
- let { config } = environment;
113
- if ('split-by-experience' !== config.performance.chunkSplit.strategy || !1 === options) return;
114
- let normalizedOptions = !0 === options ? {
115
- react: !0,
116
- router: !0
117
- } : options, currentConfig = chain.optimization.splitChunks.values();
118
- if ('object' != typeof currentConfig) return;
119
- let extraGroups = {};
120
- normalizedOptions.react && (extraGroups.react = {
121
- name: 'lib-react',
122
- test: isProd ? /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/,
123
- priority: 0
124
- }), normalizedOptions.router && (extraGroups.router = {
125
- name: 'lib-router',
126
- test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
127
- priority: 0
128
- }), Object.keys(extraGroups).length && chain.optimization.splitChunks({
129
- ...currentConfig,
130
- cacheGroups: {
131
- ...extraGroups,
132
- ...currentConfig.cacheGroups
133
- }
134
- });
135
- });
136
- }
137
- const PLUGIN_REACT_NAME = 'rsbuild:react', pluginReact = (options = {})=>({
138
- name: PLUGIN_REACT_NAME,
139
- setup (api) {
140
- let finalOptions = {
141
- fastRefresh: !0,
142
- splitChunks: !0,
143
- enableProfiler: !1,
144
- ...options
145
- };
146
- 'rspack' === api.context.bundlerType && (applyBasicReactSupport(api, finalOptions), finalOptions.enableProfiler && applyReactProfiler(api)), applySplitChunksRule(api, finalOptions.splitChunks);
147
- }
148
- });
149
- for(var __rspack_i in exports.PLUGIN_REACT_NAME = __webpack_exports__.PLUGIN_REACT_NAME, exports.pluginReact = __webpack_exports__.pluginReact, __webpack_exports__)-1 === [
150
- "PLUGIN_REACT_NAME",
151
- "pluginReact"
152
- ].indexOf(__rspack_i) && (exports[__rspack_i] = __webpack_exports__[__rspack_i]);
153
- Object.defineProperty(exports, '__esModule', {
154
- value: !0
155
- });
package/dist/react.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { RsbuildPluginAPI } from '@rsbuild/core';
2
- import type { PluginReactOptions } from './index.js';
3
- export declare function applyBasicReactSupport(api: RsbuildPluginAPI, options: PluginReactOptions): void;
4
- export declare function applyReactProfiler(api: RsbuildPluginAPI): void;