Unverified Commit a7c24aca authored by Julius Marminge's avatar Julius Marminge Committed by GitHub

feat: allow `module: Preserve` tsconfig option (#64110)

Fixes #64018

Adds support for modern configurations introduced in TS 5.4

Expected output: not modifying the `module`, `esModuleInterop` configs
since `module: preserve` is present

```
   We detected TypeScript in your project and reconfigured your tsconfig.json file for you. Strict-mode is set to false
 by default.
   The following suggested values were added to your tsconfig.json. These values can be changed to fit your project's n
eeds:

        - lib was set to dom,dom.iterable,esnext
        - allowJs was set to true
        - skipLibCheck was set to true
        - strict was set to false
        - noEmit was set to true
        - incremental was set to true
        - include was set to ['next-env.d.ts', '**/*.ts', '**/*.tsx']
        - exclude was set to ['node_modules']

   The following mandatory changes were made to your tsconfig.json:

        - isolatedModules was set to true (requirement for SWC / Babel)
        - jsx was set to preserve (next.js implements its own optimized jsx transform)
```

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent e8f8c8e2
...@@ -40,14 +40,11 @@ function getDesiredCompilerOptions( ...@@ -40,14 +40,11 @@ function getDesiredCompilerOptions(
// These values are required and cannot be changed by the user // These values are required and cannot be changed by the user
// Keep this in sync with the webpack config // Keep this in sync with the webpack config
// 'parsedValue' matches the output value from ts.parseJsonConfigFileContent() // 'parsedValue' matches the output value from ts.parseJsonConfigFileContent()
esModuleInterop: {
value: true,
reason: 'requirement for SWC / babel',
},
module: { module: {
parsedValue: ts.ModuleKind.ESNext, parsedValue: ts.ModuleKind.ESNext,
// All of these values work: // All of these values work:
parsedValues: [ parsedValues: [
semver.gte(ts.version, '5.4.0') && (ts.ModuleKind as any).Preserve,
ts.ModuleKind.ES2020, ts.ModuleKind.ES2020,
ts.ModuleKind.ESNext, ts.ModuleKind.ESNext,
ts.ModuleKind.CommonJS, ts.ModuleKind.CommonJS,
...@@ -58,6 +55,21 @@ function getDesiredCompilerOptions( ...@@ -58,6 +55,21 @@ function getDesiredCompilerOptions(
value: 'esnext', value: 'esnext',
reason: 'for dynamic import() support', reason: 'for dynamic import() support',
}, },
// TODO: Semver check not needed once Next.js repo uses 5.4.
...(semver.gte(ts.version, '5.4.0') &&
tsOptions?.module === (ts.ModuleKind as any).Preserve
? {
// TypeScript 5.4 introduced `Preserve`. Using `Preserve` implies
// - `moduleResolution` is `Bundler`
// - `esModuleInterop` is `true`
// - `resolveJsonModule` is `true`
// This means that if the user is using Preserve, they don't need these options
}
: {
esModuleInterop: {
value: true,
reason: 'requirement for SWC / babel',
},
moduleResolution: { moduleResolution: {
// In TypeScript 5.0, `NodeJs` has renamed to `Node10` // In TypeScript 5.0, `NodeJs` has renamed to `Node10`
parsedValue: parsedValue:
...@@ -79,7 +91,11 @@ function getDesiredCompilerOptions( ...@@ -79,7 +91,11 @@ function getDesiredCompilerOptions(
value: 'node', value: 'node',
reason: 'to match webpack resolution', reason: 'to match webpack resolution',
}, },
resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, resolveJsonModule: {
value: true,
reason: 'to match webpack resolution',
},
}),
...(tsOptions?.verbatimModuleSyntax === true ...(tsOptions?.verbatimModuleSyntax === true
? undefined ? undefined
: { : {
......
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
describe('tsconfig module: preserve', () => {
const { next } = nextTestSetup({
files: {
'tsconfig.json': JSON.stringify({
compilerOptions: { module: 'preserve' },
}),
'pages/index.tsx': `
export default function Page() {
return <p>hello world</p>
}
`,
},
dependencies: {
typescript: '5.4.4',
},
})
it('allows you to skip moduleResolution, esModuleInterop and resolveJsonModule when using "module: preserve"', async () => {
let output = ''
await retry(() => {
output = stripAnsi(next.cliOutput)
expect(output).toContain(
'The following mandatory changes were made to your tsconfig.json'
)
})
expect(output).not.toContain('moduleResolution')
expect(output).not.toContain('esModuleInterop')
expect(output).not.toContain('resolveJsonModule')
expect(await next.readFile('tsconfig.json')).toMatchInlineSnapshot(`
"{
"compilerOptions": {
"module": "preserve",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
"
`)
})
})
...@@ -37,8 +37,8 @@ import path from 'path' ...@@ -37,8 +37,8 @@ import path from 'path'
"strict": false, "strict": false,
"noEmit": true, "noEmit": true,
"incremental": true, "incremental": true,
"esModuleInterop": true,
"module": "esnext", "module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
...@@ -91,8 +91,8 @@ import path from 'path' ...@@ -91,8 +91,8 @@ import path from 'path'
"strict": false, "strict": false,
"noEmit": true, "noEmit": true,
"incremental": true, "incremental": true,
"esModuleInterop": true,
"module": "esnext", "module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
...@@ -430,8 +430,8 @@ import path from 'path' ...@@ -430,8 +430,8 @@ import path from 'path'
"strict": false, "strict": false,
"noEmit": true, "noEmit": true,
"incremental": true, "incremental": true,
"esModuleInterop": true,
"module": "esnext", "module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
...@@ -541,8 +541,8 @@ import path from 'path' ...@@ -541,8 +541,8 @@ import path from 'path'
"strict": false, "strict": false,
"noEmit": true, "noEmit": true,
"incremental": true, "incremental": true,
"esModuleInterop": true,
"module": "esnext", "module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"jsx": "preserve", "jsx": "preserve",
...@@ -598,8 +598,8 @@ import path from 'path' ...@@ -598,8 +598,8 @@ import path from 'path'
"strict": false, "strict": false,
"noEmit": true, "noEmit": true,
"incremental": true, "incremental": true,
"esModuleInterop": true,
"module": "esnext", "module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"jsx": "preserve", "jsx": "preserve",
...@@ -753,5 +753,60 @@ import path from 'path' ...@@ -753,5 +753,60 @@ import path from 'path'
" "
`) `)
}) })
// TODO: Enable this test when repo has upgraded to TypeScript 5.4. Currently tested as E2E: tsconfig-module-preserve
it.skip('allows you to skip moduleResolution, esModuleInterop and resolveJsonModule when using "module: preserve"', async () => {
expect(await exists(tsConfig)).toBe(false)
await writeFile(
tsConfig,
`{ "compilerOptions": { "module": "preserve" } }`
)
await new Promise((resolve) => setTimeout(resolve, 500))
const { code, stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).not.toContain('moduleResolution')
expect(stderr + stdout).not.toContain('esModuleInterop')
expect(stderr + stdout).not.toContain('resolveJsonModule')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
"compilerOptions": {
"module": "preserve",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": true
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
"
`)
})
} }
) )
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