From 31bcd044ffbd0a145d9b93515251223d9de666a8 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 22 Aug 2022 18:36:36 -0500 Subject: [PATCH] Ensure moduleResolution is written correctly (#39836) This removes the usage of `Node12` from the `parsedValues` for `moduleResolution` as this caused false detection of `undefined` being a valid value when it was removed in TypeScript. This also copies over the test from https://github.com/vercel/next.js/pull/37302 to ensure we don't regress on this. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Fixes: https://github.com/vercel/next.js/issues/37296 Closes: https://github.com/vercel/next.js/pull/37302 --- .../typescript/writeConfigurationDefaults.ts | 11 ++++- .../correct-tsconfig-defaults/index.test.ts | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/development/correct-tsconfig-defaults/index.test.ts diff --git a/packages/next/lib/typescript/writeConfigurationDefaults.ts b/packages/next/lib/typescript/writeConfigurationDefaults.ts index c534f63154eb..0aad82db632f 100644 --- a/packages/next/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/lib/typescript/writeConfigurationDefaults.ts @@ -3,10 +3,11 @@ import chalk from 'next/dist/compiled/chalk' import * as CommentJson from 'next/dist/compiled/comment-json' import semver from 'next/dist/compiled/semver' import os from 'os' +import type { CompilerOptions } from 'typescript' import { getTypeScriptConfiguration } from './getTypeScriptConfiguration' type DesiredCompilerOptionsShape = { - [key: string]: + [K in keyof CompilerOptions]: | { suggested: any } | { parsedValue?: any @@ -58,8 +59,11 @@ function getDesiredCompilerOptions( parsedValues: [ ts.ModuleResolutionKind.NodeJs, ts.ModuleResolutionKind.Node12, + // only newer TypeScript versions have this field, it + // will be filtered for older ones + (ts.ModuleResolutionKind as any).Node16, ts.ModuleResolutionKind.NodeNext, - ], + ].filter((val) => typeof val !== 'undefined'), value: 'node', reason: 'to match webpack resolution', }, @@ -86,6 +90,9 @@ export function getRequiredConfiguration( const desiredCompilerOptions = getDesiredCompilerOptions(ts) for (const optionKey of Object.keys(desiredCompilerOptions)) { const ev = desiredCompilerOptions[optionKey] + if (optionKey === 'moduleResolution') { + console.log({ optionKey, ev, current: res[optionKey] }) + } if (!('value' in ev)) { continue } diff --git a/test/development/correct-tsconfig-defaults/index.test.ts b/test/development/correct-tsconfig-defaults/index.test.ts new file mode 100644 index 000000000000..c271bd9cbb89 --- /dev/null +++ b/test/development/correct-tsconfig-defaults/index.test.ts @@ -0,0 +1,41 @@ +import { createNext } from 'e2e-utils' +import fs from 'fs' +import { waitFor } from 'next-test-utils' +import path from 'path' +import { NextInstance } from 'test/lib/next-modes/base' + +describe('correct tsconfig.json defaults', () => { + let next: NextInstance + + beforeAll(async () => { + next = await createNext({ + files: { + 'pages/index.tsx': 'export default function Page() {}', + }, + skipStart: true, + dependencies: { + typescript: 'latest', + '@types/react': 'latest', + '@types/node': 'latest', + }, + }) + }) + afterAll(() => next.destroy()) + + it('should add `moduleResoution` when generating tsconfig.json in dev', async () => { + const tsconfigPath = path.join(next.testDir, 'tsconfig.json') + expect(fs.existsSync(tsconfigPath)).toBeFalse() + + await next.start() + await waitFor(1000) + await next.stop() + + expect(fs.existsSync(tsconfigPath)).toBeTrue() + + const tsconfig = JSON.parse(await next.readFile('tsconfig.json')) + + expect(tsconfig.compilerOptions).toEqual( + expect.objectContaining({ moduleResolution: 'node' }) + ) + }) +})