(options: PluginReactOptions = {})
| 156 | } |
| 157 | |
| 158 | export const pluginReact = (options: PluginReactOptions = {}): RsbuildPlugin => ({ |
| 159 | name: PLUGIN_REACT_NAME, |
| 160 | |
| 161 | setup(api) { |
| 162 | assertCoreVersion(api.context.version); |
| 163 | |
| 164 | const defaultOptions = { |
| 165 | fastRefresh: true, |
| 166 | splitChunks: true, |
| 167 | enableProfiler: false, |
| 168 | } satisfies PluginReactOptions; |
| 169 | |
| 170 | const finalOptions = { |
| 171 | ...defaultOptions, |
| 172 | ...options, |
| 173 | }; |
| 174 | |
| 175 | if (finalOptions.reactCompiler !== undefined) { |
| 176 | assertReactCompilerVersion(); |
| 177 | } |
| 178 | |
| 179 | const reactRefreshPath = finalOptions.fastRefresh ? require.resolve('react-refresh') : ''; |
| 180 | |
| 181 | api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig }) => { |
| 182 | const isDev = config.mode === 'development'; |
| 183 | const usingHMR = isDev && config.dev.hmr && config.output.target === 'web'; |
| 184 | |
| 185 | const reactOptions: Rspack.SwcLoaderTransformConfig['react'] = { |
| 186 | development: isDev, |
| 187 | refresh: usingHMR && finalOptions.fastRefresh, |
| 188 | runtime: 'automatic', |
| 189 | ...finalOptions.swcReactOptions, |
| 190 | }; |
| 191 | const transformOptions: Rspack.SwcLoaderTransformConfig = { |
| 192 | react: reactOptions, |
| 193 | }; |
| 194 | |
| 195 | if (finalOptions.reactCompiler !== undefined) { |
| 196 | transformOptions.reactCompiler = finalOptions.reactCompiler; |
| 197 | } |
| 198 | |
| 199 | return mergeEnvironmentConfig( |
| 200 | { |
| 201 | tools: { |
| 202 | swc: { |
| 203 | jsc: { |
| 204 | transform: transformOptions, |
| 205 | }, |
| 206 | }, |
| 207 | }, |
| 208 | }, |
| 209 | config, |
| 210 | ); |
| 211 | }); |
| 212 | |
| 213 | if (finalOptions.swcReactOptions?.runtime === 'preserve') { |
| 214 | api.modifyBundlerChain((chain) => { |
| 215 | chain.module.parser.merge({ |
no outgoing calls
no test coverage detected
searching dependent graphs…