Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): change application generation steps sequence #29246

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
"description": "Add a serve target to run a local rspack dev-server",
"default": false
},
"overwriteConfig": {
"type": "boolean",
"description": "Whether to overwrite the existing rspack.config.js file",
"default": true
},
"style": {
"type": "string",
"description": "The style solution to use.",
Expand Down
64 changes: 19 additions & 45 deletions packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ export async function applicationGeneratorInternal(
);
tasks.push(ensureDependencies(host, { uiFramework: 'react' }));
}
} else if (options.bundler === 'rspack') {
const { rspackInitGenerator } = ensurePackage('@nx/rspack', nxVersion);
const rspackInitTask = await rspackInitGenerator(host, {
...options,
addPlugin: false,
skipFormat: true,
});
tasks.push(rspackInitTask);
}

if (!options.rootProject) {
Expand Down Expand Up @@ -230,6 +222,7 @@ export async function applicationGeneratorInternal(
false
);
} else if (options.bundler === 'rspack') {
/* 'rspackInitGenerator' is being called inside 'configurationGenerator' */
const { configurationGenerator } = ensurePackage('@nx/rspack', nxVersion);
const rspackTask = await configurationGenerator(host, {
project: options.projectName,
Expand All @@ -247,6 +240,7 @@ export async function applicationGeneratorInternal(
target: 'web',
newProject: true,
framework: 'react',
overwriteConfig: false,
});
tasks.push(rspackTask);
}
Expand Down Expand Up @@ -321,44 +315,24 @@ export async function applicationGeneratorInternal(
tasks.push(routingTask);
setDefaults(host, options);

if (options.bundler === 'rspack' && options.style === 'styled-jsx') {
logger.warn(
`${pc.bold('styled-jsx')} is not supported by ${pc.bold(
'Rspack'
)}. We've added ${pc.bold(
'babel-loader'
)} to your project, but using babel will slow down your build.`
);

tasks.push(
addDependenciesToPackageJson(
host,
{},
{ 'babel-loader': babelLoaderVersion }
)
);
if (options.bundler === 'rspack') {
if (options.style === 'styled-jsx') {
logger.warn(
`${pc.bold('styled-jsx')} is not supported by ${pc.bold(
'Rspack'
)}. We've added ${pc.bold(
'babel-loader'
)} to your project, but using babel will slow down your build.`
);

host.write(
joinPathFragments(options.appProjectRoot, 'rspack.config.js'),
stripIndents`
const { composePlugins, withNx, withReact } = require('@nx/rspack');
module.exports = composePlugins(withNx(), withReact(), (config) => {
config.module.rules.push({
test: /\\.[jt]sx$/i,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
plugins: ['styled-jsx/babel'],
},
},
],
});
return config;
});
`
);
tasks.push(
addDependenciesToPackageJson(
host,
{},
{ 'babel-loader': babelLoaderVersion }
)
);
}
}

updateTsconfigFiles(host, options.appProjectRoot, 'tsconfig.app.json', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ module.exports = composePlugins(
(config) => {
// Update the rspack config as needed here.
// e.g. `config.plugins.push(new MyPlugin())`
<%_ if (style === 'styled-jsx') { _%>config.module.rules.push({
test: /\.[jt]sx$/i,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
plugins: ['styled-jsx/babel'],
},
},
],
});<%_ } _%>
return config;
}
);
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface ConfigurationSchema extends InitGeneratorSchema {
newProject?: boolean;
buildTarget?: string;
serveTarget?: string;
overwriteConfig?: boolean;
}
5 changes: 5 additions & 0 deletions packages/rspack/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
"description": "Add a serve target to run a local rspack dev-server",
"default": false
},
"overwriteConfig": {
"type": "boolean",
"description": "Whether to overwrite the existing rspack.config.js file",
"default": true
},
"style": {
"type": "string",
"description": "The style solution to use.",
Expand Down
7 changes: 7 additions & 0 deletions packages/rspack/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ export function writeRspackConfigFile(
) {
const project = readProjectConfiguration(tree, options.project);

if (
tree.exists(joinPathFragments(project.root, 'rspack.config.js')) &&
options.overwriteConfig === false
) {
return;
}

tree.write(
joinPathFragments(project.root, 'rspack.config.js'),
createConfig(tree, { ...options, stylePreprocessorOptions })
Expand Down
Loading