Unverified Commit d2b8fb79 authored by lauren's avatar lauren Committed by GitHub

[cna] Add `reactCompiler` option (#82251)

Co-authored-by: 's avatarSebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
parent b90559cd
......@@ -42,6 +42,7 @@ export async function createApp({
turbopack,
rspack,
disableGit,
reactCompiler,
}: {
appPath: string
packageManager: PackageManager
......@@ -60,6 +61,7 @@ export async function createApp({
turbopack: boolean
rspack: boolean
disableGit?: boolean
reactCompiler: boolean
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
......@@ -252,6 +254,7 @@ export async function createApp({
skipInstall,
turbopack,
rspack,
reactCompiler,
})
}
......
......@@ -49,6 +49,7 @@ const program = new Command(packageJson.name)
.option('--ts, --typescript', 'Initialize as a TypeScript project. (default)')
.option('--js, --javascript', 'Initialize as a JavaScript project.')
.option('--tailwind', 'Initialize with Tailwind CSS config. (default)')
.option('--react-compiler', 'Initialize with React Compiler enabled.')
.option('--eslint', 'Initialize with ESLint config.')
.option('--biome', 'Initialize with Biome config.')
.option('--app', 'Initialize as an App Router project.')
......@@ -239,6 +240,7 @@ async function run(): Promise<void> {
empty: false,
turbopack: true,
disableGit: false,
reactCompiler: false,
}
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field]
......@@ -346,6 +348,30 @@ async function run(): Promise<void> {
preferences.eslint = false
}
if (
!opts.reactCompiler &&
!args.includes('--no-react-compiler') &&
!opts.api
) {
if (skipPrompt) {
opts.reactCompiler = getPrefOrDefault('reactCompiler')
} else {
const styledReactCompiler = blue('React Compiler')
const { reactCompiler } = await prompts({
onState: onPromptState,
type: 'toggle',
name: 'reactCompiler',
// TODO: Remove "Release Candidate" when React Compiler is stable
message: `Would you like to use ${styledReactCompiler} (Release Candidate)?`,
initial: getPrefOrDefault('reactCompiler'),
active: 'Yes',
inactive: 'No',
})
opts.reactCompiler = Boolean(reactCompiler)
preferences.reactCompiler = Boolean(reactCompiler)
}
}
if (!opts.tailwind && !args.includes('--no-tailwind') && !opts.api) {
if (skipPrompt) {
opts.tailwind = getPrefOrDefault('tailwind')
......@@ -486,6 +512,7 @@ async function run(): Promise<void> {
turbopack: opts.turbopack,
rspack: opts.rspack,
disableGit: opts.disableGit,
reactCompiler: opts.reactCompiler,
})
} catch (reason) {
if (!(reason instanceof DownloadError)) {
......@@ -520,6 +547,7 @@ async function run(): Promise<void> {
turbopack: opts.turbopack,
rspack: opts.rspack,
disableGit: opts.disableGit,
reactCompiler: opts.reactCompiler,
})
}
conf.set('preferences', preferences)
......
......@@ -47,6 +47,7 @@ export const installTemplate = async ({
skipInstall,
turbopack,
rspack,
reactCompiler,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
......@@ -96,6 +97,28 @@ export const installTemplate = async ({
);
}
if (reactCompiler) {
const nextConfigFile = path.join(
root,
mode === "js" ? "next.config.mjs" : "next.config.ts",
);
let configContent = await fs.readFile(nextConfigFile, "utf8");
if (mode === "ts") {
configContent = configContent.replace(
"const nextConfig: NextConfig = {\n /* config options here */\n};",
`const nextConfig: NextConfig = {\n reactCompiler: true,\n};`,
);
} else {
configContent = configContent.replace(
"const nextConfig = {};",
`const nextConfig = {\n reactCompiler: true,\n};`,
);
}
await fs.writeFile(nextConfigFile, configContent);
}
const tsconfigFile = path.join(
root,
mode === "js" ? "jsconfig.json" : "tsconfig.json",
......@@ -223,6 +246,11 @@ export const installTemplate = async ({
}
}
if (reactCompiler) {
// TODO: Use "^19" when React Compiler is stable
packageJson.devDependencies["babel-plugin-react-compiler"] = "19.1.0-rc.3";
}
/**
* TypeScript projects will have type definitions and other devDependencies.
*/
......
......@@ -33,4 +33,5 @@ export interface InstallTemplateArgs {
skipInstall: boolean;
turbopack: boolean;
rspack: boolean;
reactCompiler: boolean;
}
......@@ -31,6 +31,7 @@ describe('create-next-app Biome configuration', () => {
'--app',
'--no-turbopack',
'--no-import-alias',
'--no-react-compiler',
'--skip-install',
],
nextTgzFilename,
......@@ -59,6 +60,7 @@ describe('create-next-app Biome configuration', () => {
'--app',
'--no-turbopack',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{ cwd }
......@@ -95,6 +97,7 @@ describe('create-next-app Biome configuration', () => {
'--app',
'--no-turbopack',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{ cwd }
......@@ -131,6 +134,7 @@ describe('create-next-app Biome configuration', () => {
'--app',
'--no-turbopack',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{ cwd }
......@@ -167,6 +171,7 @@ describe('create-next-app Biome configuration', () => {
'--app',
'--no-turbopack',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{ cwd }
......
......@@ -28,6 +28,7 @@ describe('create-next-app ESLint configuration', () => {
'--eslint',
'--no-tailwind',
'--no-src-dir',
'--no-react-compiler',
'--app',
'--no-turbopack',
'--no-import-alias',
......@@ -76,6 +77,7 @@ describe('create-next-app ESLint configuration', () => {
'--eslint',
'--no-tailwind',
'--no-src-dir',
'--no-react-compiler',
'--app',
'--no-turbopack',
'--no-import-alias',
......
......@@ -39,6 +39,7 @@ describe('create-next-app', () => {
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -81,6 +82,7 @@ describe('create-next-app', () => {
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -113,6 +115,7 @@ describe('create-next-app', () => {
'--no-src-dir',
'--no-import-alias',
'--skip-install',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......
......@@ -49,6 +49,7 @@ describe('create-next-app with package manager bun', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......@@ -78,6 +79,7 @@ describe('create-next-app with package manager bun', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
......@@ -38,6 +38,7 @@ describe('create-next-app with package manager npm', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......@@ -67,6 +68,7 @@ describe('create-next-app with package manager npm', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
......@@ -38,6 +38,7 @@ describe('create-next-app with package manager pnpm', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......@@ -67,6 +68,7 @@ describe('create-next-app with package manager pnpm', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
......@@ -46,6 +46,7 @@ describe('create-next-app with package manager yarn', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......@@ -75,6 +76,7 @@ describe('create-next-app with package manager yarn', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
......@@ -29,6 +29,7 @@ describe('create-next-app prompts', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
{
cwd,
......@@ -68,6 +69,7 @@ describe('create-next-app prompts', () => {
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
],
{
cwd,
......@@ -104,6 +106,7 @@ describe('create-next-app prompts', () => {
'--no-turbopack',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
],
{
cwd,
......@@ -140,6 +143,7 @@ describe('create-next-app prompts', () => {
'--no-turbopack',
'--no-tailwind',
'--no-src-dir',
'--no-react-compiler',
],
{
cwd,
......
......@@ -35,6 +35,7 @@ describe('create-next-app --app (App Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -65,6 +66,7 @@ describe('create-next-app --app (App Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -93,6 +95,7 @@ describe('create-next-app --app (App Router)', () => {
'--src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -130,6 +133,7 @@ describe('create-next-app --app (App Router)', () => {
'--src-dir',
'--tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -167,6 +171,7 @@ describe('create-next-app --app (App Router)', () => {
'--empty',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -206,6 +211,7 @@ describe('create-next-app --app (App Router)', () => {
'--tailwind',
'--empty',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -245,6 +251,7 @@ describe('create-next-app --app (App Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
......@@ -33,24 +33,30 @@ describe.each(['app', 'pages'] as const)(
// eslint: ['--eslint', '--no-linter'],
eslint: ['--eslint'],
srcDir: ['--src-dir', '--no-src-dir'],
// Trading test perf for robustness:
// srcDir and reactCompiler don't interact so we're testing them together
// instead of all permutations.
srcDirAndCompiler: [
'--src-dir --react-compiler',
'--no-src-dir --no-react-compiler',
],
tailwind: ['--tailwind', '--no-tailwind'],
// shouldn't affect if the app builds or not
// packageManager: ['--use-npm', '--use-pnpm', '--use-yarn', '--use-bun'],
}
const getPermutations = <T>(items: T[][]): T[][] => {
const getCombinations = (items: string[][]): string[][] => {
if (!items.length) return [[]]
const [first, ...rest] = items
const children = getPermutations(rest)
const children = getCombinations(rest)
return first.flatMap((value) =>
children.map((child) => [value, ...child])
children.map((child) => [...value.split(' '), ...child])
)
}
const flagPermutations = getPermutations(Object.values(allFlagValues))
const testCases = flagPermutations.map((flags) => ({
const flagCombinations = getCombinations(Object.values(allFlagValues))
const testCases = flagCombinations.map((flags) => ({
name: flags.join(' '),
flags,
}))
......
......@@ -35,6 +35,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -70,6 +71,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -102,6 +104,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -138,6 +141,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--src-dir',
'--tailwind',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -176,6 +180,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--no-tailwind',
'--empty',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -216,6 +221,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--tailwind',
'--empty',
'--no-import-alias',
'--no-react-compiler',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
......@@ -256,6 +262,7 @@ describe('create-next-app --no-app (Pages Router)', () => {
'--no-src-dir',
'--no-tailwind',
'--no-import-alias',
'--no-react-compiler',
],
nextTgzFilename,
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment