Skip to content

Commit

Permalink
fix(plugin-react): allow configuring tools.swc to override react ru…
Browse files Browse the repository at this point in the history
…ntime (#3449)
  • Loading branch information
chenjiahan committed Sep 11, 2024
1 parent a9995ac commit 3d445bc
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ exports[`webpackConfig > should allow to append and prepend plugins 1`] = `
},
},
RsbuildCorePlugin {},
HotModuleReplacementPlugin {
"options": {},
},
MiniCssExtractPlugin {
"_sortedModulesCache": WeakMap {},
"options": {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const pluginBasic = (): RsbuildPlugin => ({

setup(api) {
api.modifyBundlerChain(
(chain, { env, isProd, target, bundler, environment, CHAIN_ID }) => {
(chain, { env, isDev, target, bundler, environment, CHAIN_ID }) => {
const { config } = environment;

chain.name(environment.name);
Expand Down Expand Up @@ -46,7 +46,7 @@ export const pluginBasic = (): RsbuildPlugin => ({
},
});

const usingHMR = !isProd && config.dev.hmr && target === 'web';
const usingHMR = isDev && config.dev.hmr && target === 'web';

if (usingHMR) {
chain
Expand Down
80 changes: 32 additions & 48 deletions packages/plugin-react/src/react.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,53 @@
import path from 'node:path';
import type {
ChainIdentifier,
EnvironmentConfig,
RsbuildConfig,
RsbuildPluginAPI,
Rspack,
RspackChain,
} from '@rsbuild/core';
import type { PluginReactOptions } from '.';

const modifySwcLoaderOptions = ({
chain,
CHAIN_ID,
modifier,
}: {
chain: RspackChain;
CHAIN_ID: ChainIdentifier;
modifier: (config: Rspack.SwcLoaderOptions) => Rspack.SwcLoaderOptions;
}) => {
const ruleIds = [CHAIN_ID.RULE.JS, CHAIN_ID.RULE.JS_DATA_URI];

for (const ruleId of ruleIds) {
if (chain.module.rules.has(ruleId)) {
const rule = chain.module.rule(ruleId);
if (rule.uses.has(CHAIN_ID.USE.SWC)) {
rule.use(CHAIN_ID.USE.SWC).tap(modifier);
}
}
}
};

export const applyBasicReactSupport = (
api: RsbuildPluginAPI,
options: PluginReactOptions,
): void => {
const REACT_REFRESH_PATH = require.resolve('react-refresh');

api.modifyBundlerChain(
async (chain, { CHAIN_ID, environment, isDev, isProd, target }) => {
const { config } = environment;
const usingHMR = !isProd && config.dev.hmr && target === 'web';
const reactOptions: Rspack.SwcLoaderTransformConfig['react'] = {
development: isDev,
refresh: usingHMR,
runtime: 'automatic',
...options.swcReactOptions,
};
api.modifyEnvironmentConfig((userConfig, { mergeEnvironmentConfig }) => {
const isDev = userConfig.mode === 'development';

modifySwcLoaderOptions({
chain,
CHAIN_ID,
modifier: (opts) => {
opts.jsc ||= {};
opts.jsc.transform ||= {};
opts.jsc.transform.react = reactOptions;
opts.jsc.parser = {
...opts.jsc.parser,
syntax: 'typescript',
// enable supports for React JSX/TSX compilation
tsx: true,
};
const reactOptions: Rspack.SwcLoaderTransformConfig['react'] = {
development: isDev,
refresh:
isDev && userConfig.dev.hmr && userConfig.output.target === 'web',
runtime: 'automatic',
...options.swcReactOptions,
};

return opts;
const extraConfig: EnvironmentConfig = {
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;
}
Expand Down
Loading

0 comments on commit 3d445bc

Please sign in to comment.