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

Ensure moduleResolution is written correctly #39836

Merged
merged 3 commits into from Aug 22, 2022
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
11 changes: 9 additions & 2 deletions packages/next/lib/typescript/writeConfigurationDefaults.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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',
},
Expand All @@ -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
}
Expand Down
41 changes: 41 additions & 0 deletions 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' })
)
})
})