fix(react): use babel-loader when using styled-jsx with rspack (#16443)

This commit is contained in:
Jack Hsu 2023-04-20 15:35:44 -04:00 committed by GitHub
parent 04fed26279
commit 0a6d37675c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -19,7 +19,9 @@ import {
formatFiles, formatFiles,
GeneratorCallback, GeneratorCallback,
joinPathFragments, joinPathFragments,
logger,
runTasksInSerial, runTasksInSerial,
stripIndents,
Tree, Tree,
updateJson, updateJson,
} from '@nx/devkit'; } from '@nx/devkit';
@ -28,6 +30,7 @@ import reactInitGenerator from '../init/init';
import { Linter, lintProjectGenerator } from '@nx/linter'; import { Linter, lintProjectGenerator } from '@nx/linter';
import { mapLintPattern } from '@nx/linter/src/generators/lint-project/lint-project'; import { mapLintPattern } from '@nx/linter/src/generators/lint-project/lint-project';
import { import {
babelLoaderVersion,
nxRspackVersion, nxRspackVersion,
nxVersion, nxVersion,
swcLoaderVersion, swcLoaderVersion,
@ -35,6 +38,7 @@ import {
import { installCommonDependencies } from './lib/install-common-dependencies'; import { installCommonDependencies } from './lib/install-common-dependencies';
import { extractTsConfigBase } from '../../utils/create-ts-config'; import { extractTsConfigBase } from '../../utils/create-ts-config';
import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies'; import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies';
import * as chalk from 'chalk';
async function addLinting(host: Tree, options: NormalizedSchema) { async function addLinting(host: Tree, options: NormalizedSchema) {
const tasks: GeneratorCallback[] = []; const tasks: GeneratorCallback[] = [];
@ -207,6 +211,46 @@ export async function applicationGenerator(
tasks.push(routingTask); tasks.push(routingTask);
setDefaults(host, options); setDefaults(host, options);
if (options.bundler === 'rspack' && options.style === 'styled-jsx') {
logger.warn(
`${chalk.bold('styled-jsx')} is not supported by ${chalk.bold(
'Rspack'
)}. We've added ${chalk.bold(
'babel-loader'
)} to your project, but using babel will slow down your build.`
);
tasks.push(
addDependenciesToPackageJson(
host,
{},
{ 'babel-loader': babelLoaderVersion }
)
);
host.write(
joinPathFragments(options.appProjectRoot, 'rspack.config.js'),
stripIndents`
const { composePlugins, withNx, withWeb } = require('@nrwl/rspack');
module.exports = composePlugins(withNx(), withWeb(), (config) => {
config.module.rules.push({
test: /\\.[jt]sx$/i,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
plugins: ['styled-jsx/babel'],
},
},
],
});
return config;
});
`
);
}
if (!options.skipFormat) { if (!options.skipFormat) {
await formatFiles(host); await formatFiles(host);
} }

View File

@ -7,6 +7,7 @@ export const reactVersion = '18.2.0';
export const reactDomVersion = '18.2.0'; export const reactDomVersion = '18.2.0';
export const reactIsVersion = '18.2.0'; export const reactIsVersion = '18.2.0';
export const swcLoaderVersion = '0.1.15'; export const swcLoaderVersion = '0.1.15';
export const babelLoaderVersion = '^9.1.2';
export const typesReactVersion = '18.0.28'; export const typesReactVersion = '18.0.28';
export const typesReactDomVersion = '18.0.11'; export const typesReactDomVersion = '18.0.11';
export const typesReactIsVersion = '17.0.3'; export const typesReactIsVersion = '17.0.3';