Skip to content

Commit 2c55685

Browse files
authored
fix(react): update react router logic with selected bundler (#30399)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- 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 --> Currently, when you generate a react app and select `--use-react-router` and `--bundler=` any other bundler except `vite` you would be forced into using `vite` ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> When you generate a React app and opt into using `--use-react-router` with `--bundler=webpack` (for example) you will get an error stating the React Router can only be used with `vite`. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent cfe32c2 commit 2c55685

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

packages/react/src/generators/application/application.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,34 @@ export async function applicationGeneratorInternal(
7575

7676
const options = await normalizeOptions(tree, schema);
7777

78-
options.useReactRouter = options.routing
79-
? options.useReactRouter ??
80-
(await promptWhenInteractive<{
81-
response: 'Yes' | 'No';
82-
}>(
83-
{
84-
name: 'response',
85-
message:
86-
'Would you like to use react-router for server-side rendering?',
87-
type: 'autocomplete',
88-
choices: [
89-
{
90-
name: 'Yes',
91-
message:
92-
'I want to use react-router [ https://reactrouter.com/start/framework/routing ]',
93-
},
94-
{
95-
name: 'No',
96-
message:
97-
'I do not want to use react-router for server-side rendering',
98-
},
99-
],
100-
initial: 0,
101-
},
102-
{ response: 'No' }
103-
).then((r) => r.response === 'Yes'))
104-
: false;
78+
options.useReactRouter =
79+
options.routing && options.bundler === 'vite'
80+
? options.useReactRouter ??
81+
(await promptWhenInteractive<{
82+
response: 'Yes' | 'No';
83+
}>(
84+
{
85+
name: 'response',
86+
message:
87+
'Would you like to use react-router for server-side rendering?',
88+
type: 'autocomplete',
89+
choices: [
90+
{
91+
name: 'Yes',
92+
message:
93+
'I want to use react-router [ https://reactrouter.com/start/framework/routing ]',
94+
},
95+
{
96+
name: 'No',
97+
message:
98+
'I do not want to use react-router for server-side rendering',
99+
},
100+
],
101+
initial: 0,
102+
},
103+
{ response: 'No' }
104+
).then((r) => r.response === 'Yes'))
105+
: false;
105106

106107
showPossibleWarnings(tree, options);
107108

packages/react/src/generators/application/lib/normalize-options.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
determineProjectNameAndRootOptions,
44
ensureRootProjectName,
55
} from '@nx/devkit/src/generators/project-name-and-root-utils';
6-
import { assertValidStyle } from '../../../utils/assertion';
6+
import {
7+
assertValidReactRouter,
8+
assertValidStyle,
9+
} from '../../../utils/assertion';
710
import { NormalizedSchema, Schema } from '../schema';
811
import { findFreePort } from './find-free-port';
912
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
@@ -52,8 +55,11 @@ export async function normalizeOptions<T extends Schema = Schema>(
5255
: options.style;
5356

5457
assertValidStyle(options.style);
58+
assertValidReactRouter(options.useReactRouter, options.bundler);
5559

56-
options.bundler = options.useReactRouter ? 'vite' : options.bundler;
60+
if (options.useReactRouter && !options.bundler) {
61+
options.bundler = 'vite';
62+
}
5763
options.useReactRouter = options.routing ? options.useReactRouter : false;
5864

5965
const normalized = {

packages/react/src/utils/assertion.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertValidStyle } from './assertion';
1+
import { assertValidStyle, assertValidReactRouter } from './assertion';
22

33
describe('assertValidStyle', () => {
44
it('should accept style option values from app, lib, component schematics', () => {
@@ -19,4 +19,17 @@ describe('assertValidStyle', () => {
1919
it('should throw for invalid values', () => {
2020
expect(() => assertValidStyle('bad')).toThrow(/Unsupported/);
2121
});
22+
23+
it('should throw for invalid react-router and bundler combination', () => {
24+
expect(() => assertValidReactRouter(true, 'webpack')).toThrow(
25+
/Unsupported/
26+
);
27+
expect(() => assertValidReactRouter(true, 'rspack')).toThrow(/Unsupported/);
28+
expect(() => assertValidReactRouter(true, 'rsbuild')).toThrow(
29+
/Unsupported/
30+
);
31+
expect(() => assertValidReactRouter(true, 'vite')).not.toThrow(
32+
/Unsupported/
33+
);
34+
});
2235
});

packages/react/src/utils/assertion.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { type Schema } from '../generators/application/schema';
2+
13
const VALID_STYLES = [
24
'css',
35
'scss',
@@ -18,3 +20,14 @@ export function assertValidStyle(style: string): void {
1820
);
1921
}
2022
}
23+
24+
export function assertValidReactRouter(
25+
reactRouter: boolean,
26+
bundler: Schema['bundler']
27+
): void {
28+
if (reactRouter && bundler !== 'vite') {
29+
throw new Error(
30+
`Unsupported bundler found: ${bundler}. React Router is only supported with 'vite'.`
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)