Skip to content

Commit 653ac83

Browse files
authored
Prevent typescript errors in IDE for newly generated tests (#78247)
When generating a new test with `pnpm new-test`, the IDE will show the following error when opening the generated `page.tsx` or `layout.tsx` files: ```sh 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts (2686) ``` This is because by default, the generated test app is covered by the root `tsconfig.json`, containing `{"jsx": "react-jsx"}` which expects there to be a `React` import that the template files don't have. When first running `pnpm next dev` or `pnpm next build`, a local `tsconfig.json` file is generated for the test app. This config contains `{"jsx": "preserve"}`, which fixes the initial errors. To avoid this step, and prevent the initial compiler errors, we can just copy the `tsconfig.json` as well as the `next-env.d.ts` from the CNA template files into the new test app. (Those two files are git-ignored, by the way.)
1 parent 691ca60 commit 653ac83

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

turbo/generators/config.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as helpers from './helpers'
55
interface TestResponse {
66
appDir: string
77
type: 'e2e' | 'production' | 'development' | 'unit'
8+
name: string
89
}
910

1011
interface ErrorResponse {
@@ -61,25 +62,44 @@ export default function generator(plop: NodePlopAPI): void {
6162
},
6263
],
6364
actions: function (answers) {
64-
const { appDir, type } = answers as TestResponse
65-
const testRoot = path.join(plop.getDestBasePath(), 'test')
66-
65+
const { appDir, type, name } = answers as TestResponse
66+
const basePath = plop.getDestBasePath()
67+
const testRoot = path.join(basePath, 'test')
6768
const appDirPath = appDir ? 'app-dir/' : ''
68-
let templatePath = path.join(
69+
70+
const templatePath = path.join(
6971
testRoot,
7072
type === 'unit' ? 'unit' : 'e2e',
7173
appDirPath,
7274
'test-template'
7375
)
74-
let targetPath = path.join(testRoot, type, appDirPath)
76+
77+
const targetPath = path.join(testRoot, type, appDirPath)
78+
79+
const cnaTemplatePath = path.join(
80+
basePath,
81+
'packages/create-next-app/templates',
82+
appDir ? 'app-empty' : 'default-empty',
83+
'ts'
84+
)
7585

7686
return [
7787
{
7888
type: 'addMany',
79-
templateFiles: `${templatePath}/**/*`,
89+
templateFiles: path.join(templatePath, '**/*'),
8090
base: templatePath,
8191
destination: targetPath,
8292
},
93+
{
94+
type: 'add',
95+
templateFile: path.join(cnaTemplatePath, 'tsconfig.json'),
96+
path: path.join(targetPath, name, 'tsconfig.json'),
97+
},
98+
{
99+
type: 'add',
100+
templateFile: path.join(cnaTemplatePath, 'next-env.d.ts'),
101+
path: path.join(targetPath, name, 'next-env.d.ts'),
102+
},
83103
]
84104
},
85105
})

0 commit comments

Comments
 (0)