Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion packages/next/src/lib/typescript/runTypeScriptCli.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { ChildProcess } from 'node:child_process'
import { EventEmitter } from 'node:events'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { PassThrough } from 'node:stream'

const mockSpawn = jest.fn()
Expand All @@ -9,9 +12,114 @@ jest.mock('next/dist/compiled/cross-spawn', () => ({
default: (...args: unknown[]) => mockSpawn(...args),
}))

const { runTypeScriptCli } =
const { getTypeScriptPackageInfo, runTypeScriptCli } =
require('./runTypeScriptCli') as typeof import('./runTypeScriptCli')

function createTypeScriptFixture({
packageJson,
files,
}: {
packageJson: Record<string, unknown>
files: string[]
}) {
const baseDir = mkdtempSync(path.join(tmpdir(), 'next-typescript-'))
const packageDir = path.join(baseDir, 'node_modules', 'typescript')

mkdirSync(packageDir, { recursive: true })
writeFileSync(
path.join(packageDir, 'package.json'),
JSON.stringify(packageJson)
)

for (const file of files) {
const filePath = path.join(packageDir, file)
mkdirSync(path.dirname(filePath), { recursive: true })
writeFileSync(filePath, '')
}

return { baseDir, packageDir }
}

function removeTypeScriptFixture(baseDir: string) {
rmSync(baseDir, { recursive: true, force: true })
}

describe('getTypeScriptPackageInfo', () => {
it('resolves a standard tsc bin', () => {
const { baseDir, packageDir } = createTypeScriptFixture({
packageJson: {
version: '6.0.1',
bin: { tsc: './bin/tsc' },
},
files: ['bin/tsc', 'lib/typescript.js'],
})

try {
expect(getTypeScriptPackageInfo(baseDir)).toMatchObject({
apiPath: path.join(packageDir, 'lib', 'typescript.js'),
tscPath: path.join(packageDir, 'bin', 'tsc'),
})
} finally {
removeTypeScriptFixture(baseDir)
}
})

it('resolves a versioned tsc bin from an aliased TypeScript package', () => {
const { baseDir, packageDir } = createTypeScriptFixture({
packageJson: {
name: '@typescript/typescript6',
version: '6.0.1',
bin: { tsc6: './bin/tsc6' },
},
files: ['bin/tsc6', 'lib/typescript.js'],
})

try {
expect(getTypeScriptPackageInfo(baseDir)).toMatchObject({
apiPath: path.join(packageDir, 'lib', 'typescript.js'),
tscPath: path.join(packageDir, 'bin', 'tsc6'),
})
} finally {
removeTypeScriptFixture(baseDir)
}
})

it('does not return a tsc path when the declared bin is missing', () => {
const { baseDir } = createTypeScriptFixture({
packageJson: {
version: '6.0.1',
bin: { tsc6: './bin/tsc6' },
},
files: ['lib/typescript.js'],
})

try {
expect(getTypeScriptPackageInfo(baseDir)?.tscPath).toBeUndefined()
} finally {
removeTypeScriptFixture(baseDir)
}
})

it('uses the JS CLI wrapper for an extensionless ESM bin', () => {
const { baseDir, packageDir } = createTypeScriptFixture({
packageJson: {
version: '7.0.0',
type: 'module',
bin: { tsc: './bin/tsc' },
},
files: ['bin/tsc', 'lib/tsc.js'],
})

try {
expect(getTypeScriptPackageInfo(baseDir)?.tscPath).toBe(
path.join(packageDir, 'lib', 'tsc.js')
)
} finally {
removeTypeScriptFixture(baseDir)
}
})
})

const processEvents = ['exit', 'SIGINT', 'SIGTERM', 'SIGHUP'] as const

type ProcessEvent = (typeof processEvents)[number]
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/lib/typescript/runTypeScriptCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export function getTypeScriptPackageInfo(
const packageDir = path.dirname(packageJsonPath)
const apiPath = path.join(packageDir, 'lib', 'typescript.js')
const tscBin =
typeof packageJson.bin === 'string' ? packageJson.bin : packageJson.bin?.tsc
typeof packageJson.bin === 'string'
? packageJson.bin
: (packageJson.bin?.tsc ??
Object.entries(packageJson.bin ?? {}).find(([name]) =>
/^tsc\d+$/.test(name)
)?.[1])
const tscBinPath = tscBin ? path.resolve(packageDir, tscBin) : undefined
let tscPath = tscBinPath

Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/lib/verify-typescript-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const typescriptApiPackage: MissingDependency = {
}

const typescriptCliPackage: MissingDependency = {
file: 'typescript/bin/tsc',
// The CLI path is resolved from the package's bin metadata after this initial
// package-presence check, since aliased TypeScript packages may rename it.
file: 'typescript/package.json',
pkg: 'typescript',
exportsRestrict: true,
}
Expand Down
23 changes: 23 additions & 0 deletions test/production/app-dir/typescript-cli/typescript-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,27 @@ describe('TypeScript CLI backend', () => {
expect(await next.hasFile('.next/cache/.tsbuildinfo')).toBe(true)
})
})

describe('TypeScript 6 npm alias', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
dependencies: {
typescript: 'npm:@typescript/typescript6@6.0.1',
},
})

if (skipped) return

it('builds with the versioned tsc6 entry point', async () => {
const result = await next.build()

expect(result.exitCode).toBe(0)
expect(result.cliOutput).not.toContain(
'do not have the required package(s) installed'
)
expect(await next.hasFile('.next/cache/.tsbuildinfo')).toBe(true)
})
})
})
Loading