@rsbuild/plugin-react 0.0.17 → 0.0.19

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,5 @@
1
- import type { RsbuildPlugin } from '@rsbuild/core';
2
- export declare const pluginReact: () => RsbuildPlugin;
1
+ import { RsbuildPlugin } from '@rsbuild/core';
2
+
3
+ declare const pluginReact: () => RsbuildPlugin;
4
+
5
+ export { pluginReact };
package/dist/index.js CHANGED
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function __require() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
6
11
  var __export = (target, all) => {
7
12
  for (var name in all)
8
13
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -15,26 +20,472 @@ var __copyProps = (to, from, except, desc) => {
15
20
  }
16
21
  return to;
17
22
  };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
18
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+
33
+ // ../core/dist/plugins/splitChunks.js
34
+ var require_splitChunks = __commonJS({
35
+ "../core/dist/plugins/splitChunks.js"(exports, module2) {
36
+ "use strict";
37
+ var __create2 = Object.create;
38
+ var __defProp2 = Object.defineProperty;
39
+ var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
40
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
41
+ var __getProtoOf2 = Object.getPrototypeOf;
42
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
43
+ var __export2 = (target, all) => {
44
+ for (var name in all)
45
+ __defProp2(target, name, { get: all[name], enumerable: true });
46
+ };
47
+ var __copyProps2 = (to, from, except, desc) => {
48
+ if (from && typeof from === "object" || typeof from === "function") {
49
+ for (let key of __getOwnPropNames2(from))
50
+ if (!__hasOwnProp2.call(to, key) && key !== except)
51
+ __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
52
+ }
53
+ return to;
54
+ };
55
+ var __toESM2 = (mod, isNodeMode, target) => (target = mod != null ? __create2(__getProtoOf2(mod)) : {}, __copyProps2(
56
+ // If the importer is in node compatibility mode or this is not an ESM
57
+ // file that has been converted to a CommonJS file using a Babel-
58
+ // compatible transform (i.e. "__esModule" has not been set), then set
59
+ // "default" to the CommonJS "module.exports" for node compatibility.
60
+ isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target,
61
+ mod
62
+ ));
63
+ var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
64
+ var splitChunks_exports = {};
65
+ __export2(splitChunks_exports, {
66
+ createDependenciesRegExp: () => createDependenciesRegExp2,
67
+ pluginSplitChunks: () => pluginSplitChunks
68
+ });
69
+ module2.exports = __toCommonJS2(splitChunks_exports);
70
+ var import_assert = __toESM2(require("assert"));
71
+ var import_shared5 = require("@rsbuild/shared");
72
+ function getUserDefinedCacheGroups(forceSplitting) {
73
+ const cacheGroups = {};
74
+ const pairs = Array.isArray(forceSplitting) ? forceSplitting.map(
75
+ (regexp, index) => [`force-split-${index}`, regexp]
76
+ ) : Object.entries(forceSplitting);
77
+ pairs.forEach(([key, regexp]) => {
78
+ cacheGroups[key] = {
79
+ test: regexp,
80
+ name: key,
81
+ chunks: "all",
82
+ // Ignore minimum size, minimum chunks and maximum requests and always create chunks for user defined cache group.
83
+ enforce: true
84
+ };
85
+ });
86
+ return cacheGroups;
87
+ }
88
+ var DEPENDENCY_MATCH_TEMPL = /[\\/]node_modules[\\/](<SOURCES>)[\\/]/.source;
89
+ var createDependenciesRegExp2 = (...dependencies) => {
90
+ const sources = dependencies.map(
91
+ (d) => typeof d === "string" ? d : d.source
92
+ );
93
+ const expr = DEPENDENCY_MATCH_TEMPL.replace("<SOURCES>", sources.join("|"));
94
+ return new RegExp(expr);
95
+ };
96
+ function splitByExperience2(ctx) {
97
+ const { override, polyfill, defaultConfig, userDefinedCacheGroups } = ctx;
98
+ const experienceCacheGroup = {};
99
+ const packageRegExps = {
100
+ lodash: createDependenciesRegExp2("lodash", "lodash-es"),
101
+ axios: createDependenciesRegExp2("axios", /axios-.+/)
102
+ };
103
+ if (polyfill === "entry" || polyfill === "usage") {
104
+ packageRegExps.polyfill = createDependenciesRegExp2(
105
+ "tslib",
106
+ "core-js",
107
+ "@babel/runtime",
108
+ "@swc/helpers"
109
+ );
110
+ }
111
+ Object.entries(packageRegExps).forEach(([name, test]) => {
112
+ const key = `lib-${name}`;
113
+ experienceCacheGroup[key] = {
114
+ test,
115
+ priority: 0,
116
+ name: key,
117
+ reuseExistingChunk: true
118
+ };
119
+ });
120
+ return {
121
+ ...defaultConfig,
122
+ ...override,
123
+ cacheGroups: {
124
+ ...defaultConfig.cacheGroups,
125
+ ...experienceCacheGroup,
126
+ ...userDefinedCacheGroups,
127
+ ...override.cacheGroups
128
+ }
129
+ };
130
+ }
131
+ function splitByModule(ctx) {
132
+ const { override, userDefinedCacheGroups, defaultConfig } = ctx;
133
+ return {
134
+ ...defaultConfig,
135
+ minSize: 0,
136
+ maxInitialRequests: Infinity,
137
+ ...override,
138
+ cacheGroups: {
139
+ ...defaultConfig.cacheGroups,
140
+ ...userDefinedCacheGroups,
141
+ // Core group
142
+ vendors: {
143
+ priority: -10,
144
+ test: import_shared5.NODE_MODULES_REGEX,
145
+ // todo: not support in rspack
146
+ // @ts-expect-error
147
+ name(module22) {
148
+ return (0, import_shared5.getPackageNameFromModulePath)(module22.context);
149
+ }
150
+ },
151
+ ...override.cacheGroups
152
+ }
153
+ };
154
+ }
155
+ function splitBySize(ctx) {
156
+ var _a, _b;
157
+ const { override, userDefinedCacheGroups, defaultConfig, rsbuildConfig } = ctx;
158
+ (0, import_assert.default)(rsbuildConfig.strategy === "split-by-size");
159
+ return {
160
+ ...defaultConfig,
161
+ minSize: (_a = rsbuildConfig.minSize) != null ? _a : 0,
162
+ maxSize: (_b = rsbuildConfig.maxSize) != null ? _b : Infinity,
163
+ ...override,
164
+ cacheGroups: {
165
+ ...defaultConfig.cacheGroups,
166
+ ...userDefinedCacheGroups,
167
+ ...override.cacheGroups
168
+ }
169
+ };
170
+ }
171
+ function splitCustom(ctx) {
172
+ const { override, userDefinedCacheGroups, defaultConfig } = ctx;
173
+ return {
174
+ ...defaultConfig,
175
+ ...override,
176
+ cacheGroups: {
177
+ ...defaultConfig.cacheGroups,
178
+ ...userDefinedCacheGroups,
179
+ ...override.cacheGroups
180
+ }
181
+ };
182
+ }
183
+ function allInOne(_ctx) {
184
+ return false;
185
+ }
186
+ function singleVendor(ctx) {
187
+ const { override, defaultConfig, userDefinedCacheGroups } = ctx;
188
+ const singleVendorCacheGroup = {
189
+ singleVendor: {
190
+ test: import_shared5.NODE_MODULES_REGEX,
191
+ priority: 0,
192
+ chunks: "all",
193
+ name: "vendor",
194
+ enforce: true,
195
+ reuseExistingChunk: true
196
+ }
197
+ };
198
+ return {
199
+ ...defaultConfig,
200
+ ...override,
201
+ cacheGroups: {
202
+ ...defaultConfig.cacheGroups,
203
+ ...singleVendorCacheGroup,
204
+ ...userDefinedCacheGroups,
205
+ ...override.cacheGroups
206
+ }
207
+ };
208
+ }
209
+ var SPLIT_STRATEGY_DISPATCHER = {
210
+ "split-by-experience": splitByExperience2,
211
+ "split-by-module": splitByModule,
212
+ "split-by-size": splitBySize,
213
+ custom: splitCustom,
214
+ "all-in-one": allInOne,
215
+ "single-vendor": singleVendor
216
+ };
217
+ function pluginSplitChunks() {
218
+ return {
219
+ name: "plugin-split-chunks",
220
+ setup(api) {
221
+ api.modifyBundlerChain(
222
+ async (chain, { isServer, isWebWorker, isServiceWorker }) => {
223
+ var _a;
224
+ if (isServer || isWebWorker || isServiceWorker) {
225
+ chain.optimization.splitChunks(false);
226
+ if (isWebWorker || isServiceWorker) {
227
+ chain.module.parser.merge({
228
+ javascript: {
229
+ dynamicImportMode: "eager"
230
+ }
231
+ });
232
+ }
233
+ return;
234
+ }
235
+ const config = api.getNormalizedConfig();
236
+ const defaultConfig = {
237
+ // Optimize both `initial` and `async` chunks
238
+ chunks: "all",
239
+ // When chunk size >= 50000 bytes, split it into separate chunk
240
+ // @ts-expect-error Rspack type missing
241
+ enforceSizeThreshold: 5e4,
242
+ cacheGroups: {}
243
+ };
244
+ const { chunkSplit } = config.performance;
245
+ let userDefinedCacheGroups = {};
246
+ if (chunkSplit.forceSplitting) {
247
+ userDefinedCacheGroups = getUserDefinedCacheGroups(
248
+ chunkSplit.forceSplitting
249
+ );
250
+ }
251
+ const override = chunkSplit.strategy === "custom" ? (
252
+ // `chunkSplit.splitChunks` compat for Eden
253
+ (_a = chunkSplit.splitChunks) != null ? _a : chunkSplit.override
254
+ ) : chunkSplit.override;
255
+ const splitChunksOptions = await SPLIT_STRATEGY_DISPATCHER[chunkSplit.strategy]({
256
+ defaultConfig,
257
+ override: override || {},
258
+ userDefinedCacheGroups,
259
+ rsbuildConfig: chunkSplit,
260
+ rootPath: api.context.rootPath,
261
+ polyfill: config.output.polyfill
262
+ });
263
+ chain.optimization.splitChunks(splitChunksOptions);
264
+ if (chunkSplit.strategy !== "all-in-one") {
265
+ chain.optimization.runtimeChunk({
266
+ name: import_shared5.RUNTIME_CHUNK_NAME
267
+ });
268
+ }
269
+ }
270
+ );
271
+ }
272
+ };
273
+ }
274
+ }
275
+ });
276
+
277
+ // src/index.ts
19
278
  var src_exports = {};
20
279
  __export(src_exports, {
21
280
  pluginReact: () => pluginReact
22
281
  });
23
282
  module.exports = __toCommonJS(src_exports);
24
- var import_antd = require("./antd");
25
- var import_arco = require("./arco");
26
- var import_splitChunks = require("./splitChunks");
27
- var import_react = require("./react");
28
- const pluginReact = () => ({
283
+
284
+ // src/antd.ts
285
+ var import_shared = require("@rsbuild/shared");
286
+ var getAntdMajorVersion = (appDirectory) => {
287
+ try {
288
+ const pkgJsonPath = require.resolve("antd/package.json", {
289
+ paths: [appDirectory]
290
+ });
291
+ const { version } = require(pkgJsonPath);
292
+ return Number(version.split(".")[0]);
293
+ } catch (err) {
294
+ return null;
295
+ }
296
+ };
297
+ var applyAntdSupport = (api) => {
298
+ api.modifyRsbuildConfig((rsbuildConfig) => {
299
+ var _a, _b, _c;
300
+ (_a = rsbuildConfig.source) != null ? _a : rsbuildConfig.source = {};
301
+ if (rsbuildConfig.source.transformImport === false || ((_b = rsbuildConfig.source.transformImport) == null ? void 0 : _b.some(
302
+ (item) => item.libraryName === "antd"
303
+ ))) {
304
+ return;
305
+ }
306
+ const antdMajorVersion = getAntdMajorVersion(api.context.rootPath);
307
+ if (antdMajorVersion && antdMajorVersion < 5) {
308
+ (_c = rsbuildConfig.source) != null ? _c : rsbuildConfig.source = {};
309
+ rsbuildConfig.source.transformImport = [
310
+ ...rsbuildConfig.source.transformImport || [],
311
+ {
312
+ libraryName: "antd",
313
+ libraryDirectory: (0, import_shared.isServerTarget)(api.context.target) ? "lib" : "es",
314
+ style: true
315
+ }
316
+ ];
317
+ }
318
+ });
319
+ };
320
+
321
+ // src/arco.ts
322
+ var import_shared2 = require("@rsbuild/shared");
323
+ var applyArcoSupport = (api) => {
324
+ const ARCO_NAME = "@arco-design/web-react";
325
+ const ARCO_ICON = `${ARCO_NAME}/icon`;
326
+ api.modifyRsbuildConfig((rsbuildConfig) => {
327
+ const { transformImport = [] } = rsbuildConfig.source || {};
328
+ if (transformImport === false || !(0, import_shared2.isPackageInstalled)(ARCO_NAME, api.context.rootPath)) {
329
+ return;
330
+ }
331
+ const isUseSSR = (0, import_shared2.isServerTarget)(api.context.target);
332
+ if (!(transformImport == null ? void 0 : transformImport.some((item) => item.libraryName === ARCO_NAME))) {
333
+ transformImport.push({
334
+ libraryName: ARCO_NAME,
335
+ libraryDirectory: isUseSSR ? "lib" : "es",
336
+ camelToDashComponentName: false,
337
+ style: true
338
+ });
339
+ }
340
+ if (!(transformImport == null ? void 0 : transformImport.some((item) => item.libraryName === ARCO_ICON))) {
341
+ transformImport.push({
342
+ libraryName: ARCO_ICON,
343
+ libraryDirectory: isUseSSR ? "react-icon-cjs" : "react-icon",
344
+ camelToDashComponentName: false
345
+ });
346
+ }
347
+ rsbuildConfig.source || (rsbuildConfig.source = {});
348
+ rsbuildConfig.source.transformImport = transformImport;
349
+ });
350
+ };
351
+
352
+ // src/splitChunks.ts
353
+ var import_splitChunks = __toESM(require_splitChunks());
354
+ var import_shared3 = require("@rsbuild/shared");
355
+ async function splitByExperience(rootPath) {
356
+ const experienceCacheGroup = {};
357
+ const packageRegExps = {
358
+ react: (0, import_splitChunks.createDependenciesRegExp)("react", "react-dom", "scheduler"),
359
+ router: (0, import_splitChunks.createDependenciesRegExp)(
360
+ "react-router",
361
+ "react-router-dom",
362
+ "@remix-run/router",
363
+ "history"
364
+ )
365
+ };
366
+ if ((0, import_shared3.isPackageInstalled)("antd", rootPath)) {
367
+ packageRegExps.antd = (0, import_splitChunks.createDependenciesRegExp)("antd");
368
+ }
369
+ if ((0, import_shared3.isPackageInstalled)("@arco-design/web-react", rootPath)) {
370
+ packageRegExps.arco = (0, import_splitChunks.createDependenciesRegExp)(/@?arco-design/);
371
+ }
372
+ if ((0, import_shared3.isPackageInstalled)("@douyinfe/semi-ui", rootPath)) {
373
+ packageRegExps.semi = (0, import_splitChunks.createDependenciesRegExp)(
374
+ /@(ies|douyinfe)[\\/]semi-.*/
375
+ );
376
+ }
377
+ Object.entries(packageRegExps).forEach(([name, test]) => {
378
+ const key = `lib-${name}`;
379
+ experienceCacheGroup[key] = {
380
+ test,
381
+ priority: 0,
382
+ name: key,
383
+ reuseExistingChunk: true
384
+ };
385
+ });
386
+ return experienceCacheGroup;
387
+ }
388
+ var applySplitChunksRule = (api) => {
389
+ api.modifyRsbuildConfig(async (rsbuildConfig) => {
390
+ const { chunkSplit } = rsbuildConfig.performance || {};
391
+ if ((chunkSplit == null ? void 0 : chunkSplit.strategy) !== "split-by-experience") {
392
+ return;
393
+ }
394
+ const cacheGroups = await splitByExperience(api.context.rootPath);
395
+ const override = rsbuildConfig.performance.chunkSplit.override;
396
+ rsbuildConfig.performance.chunkSplit.override = {
397
+ cacheGroups: {
398
+ ...cacheGroups,
399
+ ...override ? override.cacheGroups : {}
400
+ },
401
+ ...override || {}
402
+ };
403
+ });
404
+ };
405
+
406
+ // src/react.ts
407
+ var import_shared4 = require("@rsbuild/shared");
408
+ function getReactRefreshEntry(compiler) {
409
+ var _a, _b, _c, _d, _e;
410
+ const hot = (_b = (_a = compiler.options.devServer) == null ? void 0 : _a.hot) != null ? _b : true;
411
+ const refresh = (_e = (_d = (_c = compiler.options.builtins) == null ? void 0 : _c.react) == null ? void 0 : _d.refresh) != null ? _e : true;
412
+ if (hot && refresh) {
413
+ const reactRefreshEntryPath = require.resolve("@rspack/plugin-react-refresh/react-refresh-entry");
414
+ return reactRefreshEntryPath;
415
+ }
416
+ return null;
417
+ }
418
+ var setupCompiler = (compiler) => {
419
+ if (!(0, import_shared4.isClientCompiler)(compiler)) {
420
+ return;
421
+ }
422
+ const reactRefreshEntry = getReactRefreshEntry(compiler);
423
+ if (!reactRefreshEntry) {
424
+ return;
425
+ }
426
+ for (const key in compiler.options.entry) {
427
+ compiler.options.entry[key].import = [
428
+ reactRefreshEntry,
429
+ ...compiler.options.entry[key].import || []
430
+ ];
431
+ }
432
+ };
433
+ var applyBasicReactSupport = (api) => {
434
+ api.onAfterCreateCompiler(({ compiler: multiCompiler }) => {
435
+ if ((0, import_shared4.isProd)()) {
436
+ return;
437
+ }
438
+ if (multiCompiler.compilers) {
439
+ multiCompiler.compilers.forEach(setupCompiler);
440
+ } else {
441
+ setupCompiler(multiCompiler);
442
+ }
443
+ });
444
+ api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd: isProd2, target }) => {
445
+ const config = api.getNormalizedConfig();
446
+ const usingHMR = (0, import_shared4.isUsingHMR)(config, { isProd: isProd2, target });
447
+ const rule = chain.module.rule(CHAIN_ID.RULE.JS);
448
+ const reactOptions = {
449
+ development: !isProd2,
450
+ refresh: usingHMR,
451
+ runtime: "automatic"
452
+ };
453
+ rule.use(CHAIN_ID.USE.SWC).tap((options) => {
454
+ options.jsc.transform.react = {
455
+ ...reactOptions
456
+ };
457
+ return options;
458
+ });
459
+ if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) {
460
+ chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).use(CHAIN_ID.USE.SWC).tap((options) => {
461
+ options.jsc.transform.react = {
462
+ ...reactOptions
463
+ };
464
+ return options;
465
+ });
466
+ }
467
+ if (!usingHMR) {
468
+ return;
469
+ }
470
+ const { default: ReactRefreshRspackPlugin } = await import(
471
+ // TODO https://github.com/web-infra-dev/rspack/issues/4471
472
+ "@rspack/plugin-react-refresh"
473
+ );
474
+ chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin);
475
+ });
476
+ };
477
+
478
+ // src/index.ts
479
+ var pluginReact = () => ({
29
480
  name: "plugin-react",
30
481
  pre: ["plugin-swc"],
31
482
  setup(api) {
32
483
  if (api.context.bundlerType === "rspack") {
33
- (0, import_react.applyBasicReactSupport)(api);
484
+ applyBasicReactSupport(api);
34
485
  }
35
- (0, import_antd.applyAntdSupport)(api);
36
- (0, import_arco.applyArcoSupport)(api);
37
- (0, import_splitChunks.applySplitChunksRule)(api);
486
+ applyAntdSupport(api);
487
+ applyArcoSupport(api);
488
+ applySplitChunksRule(api);
38
489
  }
39
490
  });
40
491
  // Annotate the CommonJS export names for ESM import in node: