"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { PLUGIN_REACT_NAME: () => PLUGIN_REACT_NAME, pluginReact: () => pluginReact }); module.exports = __toCommonJS(src_exports); // src/react.ts var import_node_path = __toESM(require("path")); var applyBasicReactSupport = (api, options) => { const REACT_REFRESH_PATH = require.resolve("react-refresh"); api.modifyEnvironmentConfig((userConfig, { mergeEnvironmentConfig }) => { const isDev = userConfig.mode === "development"; const reactOptions = { development: isDev, refresh: isDev && userConfig.dev.hmr && userConfig.output.target === "web", runtime: "automatic", ...options.swcReactOptions }; const extraConfig = { tools: { swc: { jsc: { parser: { syntax: "typescript", // enable supports for JSX/TSX compilation tsx: true }, transform: { react: reactOptions } } } } }; return mergeEnvironmentConfig(extraConfig, userConfig); }); api.modifyBundlerChain( async (chain, { CHAIN_ID, environment, isDev, target }) => { const { config } = environment; const usingHMR = isDev && config.dev.hmr && target === "web"; if (!usingHMR) { return; } chain.resolve.alias.set( "react-refresh", import_node_path.default.dirname(REACT_REFRESH_PATH) ); const { default: ReactRefreshRspackPlugin } = await import("@rspack/plugin-react-refresh"); const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/; chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).use(ReactRefreshRspackPlugin, [ { include: [SCRIPT_REGEX], ...options.reactRefreshOptions } ]); } ); }; var applyReactProfiler = (api) => { api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig }) => { if (config.mode !== "production") { return; } const enableProfilerConfig = { output: { minify: { jsOptions: { minimizerOptions: { // Need to keep classnames and function names like Components for debugging purposes. mangle: { keep_classnames: true, keep_fnames: true } } } } } }; return mergeEnvironmentConfig(config, enableProfilerConfig); }); api.modifyBundlerChain((chain, { isProd }) => { if (!isProd) { return; } chain.resolve.alias.set("react-dom$", "react-dom/profiling"); chain.resolve.alias.set("scheduler/tracing", "scheduler/tracing-profiling"); }); }; // src/splitChunks.ts var isPlainObject = (obj) => obj !== null && typeof obj === "object" && Object.prototype.toString.call(obj) === "[object Object]"; var applySplitChunksRule = (api, options = { react: true, router: true }) => { api.modifyBundlerChain((chain, { environment, isProd }) => { const { config } = environment; if (config.performance.chunkSplit.strategy !== "split-by-experience") { return; } const currentConfig = chain.optimization.splitChunks.values(); if (!isPlainObject(currentConfig)) { return; } const extraGroups = {}; if (options.react) { extraGroups.react = { name: "lib-react", test: isProd ? /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/, priority: 0 }; } if (options.router) { extraGroups.router = { name: "lib-router", test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/, priority: 0 }; } if (!Object.keys(extraGroups).length) { return; } chain.optimization.splitChunks({ ...currentConfig, cacheGroups: { ...currentConfig.cacheGroups, ...extraGroups } }); }); }; // src/index.ts var PLUGIN_REACT_NAME = "rsbuild:react"; var pluginReact = ({ enableProfiler = false, ...options } = {}) => ({ name: PLUGIN_REACT_NAME, setup(api) { if (api.context.bundlerType === "rspack") { applyBasicReactSupport(api, options); if (enableProfiler) { applyReactProfiler(api); } } applySplitChunksRule(api, options?.splitChunks); } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PLUGIN_REACT_NAME, pluginReact });