Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: verbatimModuleSyntax: true support in tsconfig.json #48018

Merged
merged 6 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/next/src/lib/typescript/writeConfigurationDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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)

Expand All @@ -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)) {
Expand Down
56 changes: 56 additions & 0 deletions test/integration/tsconfig-verifier/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down