Unverified Commit be960ccd authored by Simon Podlipsky's avatar Simon Podlipsky Committed by GitHub

fix: `verbatimModuleSyntax: true` support in tsconfig.json (#48018)

### What?

When `verbatimModuleSyntax: true` is set, `isolatedModules: true` are not allowed by TS

### Why?

Next always adds `isolatedModules: true` which causes TS error.

### How?

- Fixes #46509

fix NEXT-689
Co-authored-by: 's avatarSteven <229881+styfle@users.noreply.github.com>
parent 558632b5
......@@ -18,7 +18,8 @@ type DesiredCompilerOptionsShape = {
}
function getDesiredCompilerOptions(
ts: typeof import('typescript')
ts: typeof import('typescript'),
userTsConfig?: { compilerOptions?: CompilerOptions }
): DesiredCompilerOptionsShape {
const o: DesiredCompilerOptionsShape = {
// These are suggested values and will be set when not present in the
......@@ -73,10 +74,14 @@ function getDesiredCompilerOptions(
reason: 'to match webpack resolution',
},
resolveJsonModule: { value: true, reason: 'to match webpack resolution' },
isolatedModules: {
value: true,
reason: 'requirement for SWC / Babel',
},
...(userTsConfig?.compilerOptions?.verbatimModuleSyntax === true
? undefined
: {
isolatedModules: {
value: true,
reason: 'requirement for SWC / Babel',
},
}),
jsx: {
parsedValue: ts.JsxEmit.Preserve,
value: 'preserve',
......@@ -116,7 +121,6 @@ export async function writeConfigurationDefaults(
await fs.writeFile(tsConfigPath, '{}' + os.EOL)
}
const desiredCompilerOptions = getDesiredCompilerOptions(ts)
const { options: tsOptions, raw: rawConfig } =
await getTypeScriptConfiguration(ts, tsConfigPath, true)
......@@ -129,6 +133,8 @@ export async function writeConfigurationDefaults(
isFirstTimeSetup = true
}
const desiredCompilerOptions = getDesiredCompilerOptions(ts, userTsConfig)
const suggestedActions: string[] = []
const requiredActions: string[] = []
for (const optionKey of Object.keys(desiredCompilerOptions)) {
......
......@@ -470,6 +470,62 @@ describe('tsconfig.json verifier', () => {
`)
})
it('allows you to set verbatimModuleSyntax true without adding isolatedModules', async () => {
expect(await exists(tsConfig)).toBe(false)
await writeFile(
tsConfig,
`{ "compilerOptions": { "verbatimModuleSyntax": true } }`
)
await new Promise((resolve) => setTimeout(resolve, 500))
const { code, stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).not.toContain('isolatedModules')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
\\"compilerOptions\\": {
\\"verbatimModuleSyntax\\": true,
\\"lib\\": [
\\"dom\\",
\\"dom.iterable\\",
\\"esnext\\"
],
\\"allowJs\\": true,
\\"skipLibCheck\\": true,
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"esModuleInterop\\": true,
\\"module\\": \\"esnext\\",
\\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true,
\\"jsx\\": \\"preserve\\",
\\"plugins\\": [
{
\\"name\\": \\"next\\"
}
],
\\"strictNullChecks\\": true
},
\\"include\\": [
\\"next-env.d.ts\\",
\\".next/types/**/*.ts\\",
\\"**/*.ts\\",
\\"**/*.tsx\\"
],
\\"exclude\\": [
\\"node_modules\\"
]
}
"
`)
})
it('allows you to extend another configuration file', async () => {
expect(await exists(tsConfig)).toBe(false)
expect(await exists(tsConfigBase)).toBe(false)
......
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