Jack Hsu 9234fb30a6
feat(react): undeprecate svgr option for Next.js apps since --turbo supports it (#30909)
This PR delays deprecation of `svgr` for `@nx/next`, as Turbopack
supports it now.

This PR also deprecates all SVGR support for v22. It is not a well-used
feature, and the webpack plugin is not maintained. We'll ensure in v22
to add the SVGR webpack plugin to userland configs, but we'll not
maintain it ourselves moving forward.

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2025-04-29 08:44:21 -04:00

96 lines
2.5 KiB
TypeScript

import { Configuration, WebpackOptionsNormalized } from 'webpack';
import { SvgrOptions } from '../../with-react';
export function applyReactConfig(
options: { svgr?: boolean | SvgrOptions },
config: Partial<WebpackOptionsNormalized | Configuration> = {}
): void {
if (!process.env['NX_TASK_TARGET_PROJECT']) return;
addHotReload(config);
if (options.svgr !== false || typeof options.svgr === 'object') {
removeSvgLoaderIfPresent(config);
const defaultSvgrOptions = {
svgo: false,
titleProp: true,
ref: true,
};
const svgrOptions =
typeof options.svgr === 'object' ? options.svgr : defaultSvgrOptions;
config.module.rules.push({
test: /\.svg$/,
issuer: /\.(js|ts|md)x?$/,
use: [
{
loader: require.resolve('@svgr/webpack'),
options: svgrOptions,
},
{
loader: require.resolve('file-loader'),
options: {
name: '[name].[hash].[ext]',
},
},
],
});
}
// enable webpack node api
config.node = {
__dirname: true,
__filename: true,
};
}
function addHotReload(
config: Partial<WebpackOptionsNormalized | Configuration>
) {
if (
config.mode === 'development' &&
typeof config['devServer'] === 'object' &&
config['devServer']?.hot
) {
// add `react-refresh/babel` to babel loader plugin
const babelLoader = config.module.rules.find(
(rule) =>
rule &&
typeof rule !== 'string' &&
rule.loader?.toString().includes('babel-loader')
);
if (babelLoader && typeof babelLoader !== 'string') {
babelLoader.options['plugins'] = [
...(babelLoader.options['plugins'] || []),
[
require.resolve('react-refresh/babel'),
{
skipEnvCheck: true,
},
],
];
}
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
config.plugins.push(new ReactRefreshPlugin({ overlay: false }));
}
}
// We remove potentially conflicting rules that target SVGs because we use @svgr/webpack loader
// See https://github.com/nrwl/nx/issues/14383
function removeSvgLoaderIfPresent(
config: Partial<WebpackOptionsNormalized | Configuration>
) {
const svgLoaderIdx = config.module.rules.findIndex(
(rule) =>
typeof rule === 'object' &&
typeof rule.test !== 'undefined' &&
rule.test.toString().includes('svg')
);
if (svgLoaderIdx === -1) return;
config.module.rules.splice(svgLoaderIdx, 1);
}