From 593418fbabbcb5fcd1f6812e71c364ce23fd1dcf Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 6 Apr 2023 14:42:55 +0200 Subject: [PATCH 1/4] fix: allow ts compilerOption `verbatimModuleSyntax: true` --- .../typescript/writeConfigurationDefaults.ts | 19 ++++--- .../tsconfig-verifier/test/index.test.js | 49 +++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts index 691ecefd0b5f4..5b0e32f977f15 100644 --- a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts @@ -18,7 +18,8 @@ type DesiredCompilerOptionsShape = { } function getDesiredCompilerOptions( - ts: typeof import('typescript') + ts: typeof import('typescript'), + userTsConfig: any ): DesiredCompilerOptionsShape { const o: DesiredCompilerOptionsShape = { // These are suggested values and will be set when not present in the @@ -73,10 +74,6 @@ function getDesiredCompilerOptions( reason: 'to match webpack resolution', }, resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, - isolatedModules: { - value: true, - reason: 'requirement for SWC / Babel', - }, jsx: { parsedValue: ts.JsxEmit.Preserve, value: 'preserve', @@ -84,6 +81,13 @@ function getDesiredCompilerOptions( }, } + if (userTsConfig.compilerOptions?.verbatimModuleSyntax !== true) { + o.isolatedModules = { + value: true, + reason: 'requirement for SWC / Babel', + }; + } + return o } @@ -92,7 +96,7 @@ export function getRequiredConfiguration( ): Partial { const res: Partial = {} - const desiredCompilerOptions = getDesiredCompilerOptions(ts) + const desiredCompilerOptions = getDesiredCompilerOptions(ts, {}) for (const optionKey of Object.keys(desiredCompilerOptions)) { const ev = desiredCompilerOptions[optionKey] if (!('value' in ev)) { @@ -116,7 +120,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 +132,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)) { diff --git a/test/integration/tsconfig-verifier/test/index.test.js b/test/integration/tsconfig-verifier/test/index.test.js index 18bccf6797341..f74fdd4eedb69 100644 --- a/test/integration/tsconfig-verifier/test/index.test.js +++ b/test/integration/tsconfig-verifier/test/index.test.js @@ -408,6 +408,55 @@ describe('tsconfig.json verifier', () => { `) }) + it('does not add `isolatedModules: true` when `verbatimModuleSyntax: true` is set', 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('moduleResolution') + expect(code).toBe(0) + + expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(` + "{ + \\"compilerOptions\\": { + \\"esModuleInterop\\": true, + \\"module\\": \\"node16\\", + \\"moduleResolution\\": \\"node16\\", + \\"lib\\": [ + \\"dom\\", + \\"dom.iterable\\", + \\"esnext\\" + ], + \\"allowJs\\": true, + \\"skipLibCheck\\": true, + \\"strict\\": false, + \\"forceConsistentCasingInFileNames\\": true, + \\"noEmit\\": true, + \\"incremental\\": true, + \\"resolveJsonModule\\": true, + \\"verbatimModuleSyntax\\": true, + \\"jsx\\": \\"preserve\\" + }, + \\"include\\": [ + \\"next-env.d.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) From 1f11d44aa0f102a9b012cc0873b36c48368bfc1e Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 26 May 2023 18:36:13 -0400 Subject: [PATCH 2/4] Change to inline --- .../lib/typescript/writeConfigurationDefaults.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts index 996509e06d9d4..1136ae4b128ce 100644 --- a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts @@ -19,7 +19,7 @@ type DesiredCompilerOptionsShape = { function getDesiredCompilerOptions( ts: typeof import('typescript'), - userTsConfig: any + userTsConfig?: { compilerOptions?: CompilerOptions } ): DesiredCompilerOptionsShape { const o: DesiredCompilerOptionsShape = { // These are suggested values and will be set when not present in the @@ -74,6 +74,14 @@ function getDesiredCompilerOptions( reason: 'to match webpack resolution', }, resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, + ...(userTsConfig?.compilerOptions?.verbatimModuleSyntax === true + ? undefined + : { + isolatedModules: { + value: true, + reason: 'requirement for SWC / Babel', + }, + }), jsx: { parsedValue: ts.JsxEmit.Preserve, value: 'preserve', @@ -81,11 +89,11 @@ function getDesiredCompilerOptions( }, } - if (userTsConfig.compilerOptions?.verbatimModuleSyntax !== true) { + if (userTsConfig?.compilerOptions?.verbatimModuleSyntax !== true) { o.isolatedModules = { value: true, reason: 'requirement for SWC / Babel', - }; + } } return o @@ -96,7 +104,7 @@ export function getRequiredConfiguration( ): Partial { const res: Partial = {} - const desiredCompilerOptions = getDesiredCompilerOptions(ts, {}) + const desiredCompilerOptions = getDesiredCompilerOptions(ts) for (const optionKey of Object.keys(desiredCompilerOptions)) { const ev = desiredCompilerOptions[optionKey] if (!('value' in ev)) { From 17ffba80a27b88197770ffed9bf3a0d3dec479d4 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 30 May 2023 18:52:28 -0400 Subject: [PATCH 3/4] Try with ts-ignore --- .../next/src/lib/typescript/writeConfigurationDefaults.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts index 1136ae4b128ce..1bdd38bd235fc 100644 --- a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts @@ -74,6 +74,7 @@ function getDesiredCompilerOptions( reason: 'to match webpack resolution', }, resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, + // @ts-ignore - need to upgrade to typescript@5 ...(userTsConfig?.compilerOptions?.verbatimModuleSyntax === true ? undefined : { @@ -89,13 +90,6 @@ function getDesiredCompilerOptions( }, } - if (userTsConfig?.compilerOptions?.verbatimModuleSyntax !== true) { - o.isolatedModules = { - value: true, - reason: 'requirement for SWC / Babel', - } - } - return o } From 801563dadbd08e0230856ceeb609735f4be2fe42 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 9 Jun 2023 14:46:56 -0400 Subject: [PATCH 4/4] Fix test --- .../typescript/writeConfigurationDefaults.ts | 1 - .../tsconfig-verifier/test/index.test.js | 23 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts index 1bdd38bd235fc..fb52e4d652e5d 100644 --- a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts @@ -74,7 +74,6 @@ function getDesiredCompilerOptions( reason: 'to match webpack resolution', }, resolveJsonModule: { value: true, reason: 'to match webpack resolution' }, - // @ts-ignore - need to upgrade to typescript@5 ...(userTsConfig?.compilerOptions?.verbatimModuleSyntax === true ? undefined : { diff --git a/test/integration/tsconfig-verifier/test/index.test.js b/test/integration/tsconfig-verifier/test/index.test.js index 61c5175d94b4e..159f3773fbbad 100644 --- a/test/integration/tsconfig-verifier/test/index.test.js +++ b/test/integration/tsconfig-verifier/test/index.test.js @@ -470,27 +470,25 @@ describe('tsconfig.json verifier', () => { `) }) - it('does not add `isolatedModules: true` when `verbatimModuleSyntax: true` is set', async () => { + it('allows you to set verbatimModuleSyntax true without adding isolatedModules', async () => { expect(await exists(tsConfig)).toBe(false) await writeFile( tsConfig, - `{ "compilerOptions": { "verbatimModuleSyntax": true} }` + `{ "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('moduleResolution') + expect(stderr + stdout).not.toContain('isolatedModules') expect(code).toBe(0) expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(` "{ \\"compilerOptions\\": { - \\"esModuleInterop\\": true, - \\"module\\": \\"node16\\", - \\"moduleResolution\\": \\"node16\\", + \\"verbatimModuleSyntax\\": true, \\"lib\\": [ \\"dom\\", \\"dom.iterable\\", @@ -502,12 +500,21 @@ describe('tsconfig.json verifier', () => { \\"forceConsistentCasingInFileNames\\": true, \\"noEmit\\": true, \\"incremental\\": true, + \\"esModuleInterop\\": true, + \\"module\\": \\"esnext\\", + \\"moduleResolution\\": \\"node\\", \\"resolveJsonModule\\": true, - \\"verbatimModuleSyntax\\": true, - \\"jsx\\": \\"preserve\\" + \\"jsx\\": \\"preserve\\", + \\"plugins\\": [ + { + \\"name\\": \\"next\\" + } + ], + \\"strictNullChecks\\": true }, \\"include\\": [ \\"next-env.d.ts\\", + \\".next/types/**/*.ts\\", \\"**/*.ts\\", \\"**/*.tsx\\" ],