| | import type { Options as SWCOptions } from '@swc/core' |
| | import type { CompilerOptions } from 'typescript' |
| |
|
| | import { resolve } from 'node:path' |
| | import { readFile } from 'node:fs/promises' |
| | import { deregisterHook, registerHook, requireFromString } from './require-hook' |
| | import { warn } from '../output/log' |
| | import { installDependencies } from '../../lib/install-dependencies' |
| |
|
| | function resolveSWCOptions( |
| | cwd: string, |
| | compilerOptions: CompilerOptions |
| | ): SWCOptions { |
| | const resolvedBaseUrl = resolve(cwd, compilerOptions.baseUrl ?? '.') |
| | return { |
| | jsc: { |
| | target: 'es5', |
| | parser: { |
| | syntax: 'typescript', |
| | }, |
| | paths: compilerOptions.paths, |
| | baseUrl: resolvedBaseUrl, |
| | }, |
| | module: { |
| | type: 'commonjs', |
| | }, |
| | isModule: 'unknown', |
| | } satisfies SWCOptions |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | async function verifyTypeScriptSetup(cwd: string, configFileName: string) { |
| | try { |
| | |
| | require.resolve('typescript', { paths: [cwd] }) |
| | } catch (error) { |
| | if ( |
| | error && |
| | typeof error === 'object' && |
| | 'code' in error && |
| | error.code === 'MODULE_NOT_FOUND' |
| | ) { |
| | warn( |
| | `Installing TypeScript as it was not found while loading "${configFileName}".` |
| | ) |
| |
|
| | await installDependencies(cwd, [{ pkg: 'typescript' }], true).catch( |
| | (err) => { |
| | if (err && typeof err === 'object' && 'command' in err) { |
| | console.error( |
| | `Failed to install TypeScript, please install it manually to continue:\n` + |
| | (err as any).command + |
| | '\n' |
| | ) |
| | } |
| | throw err |
| | } |
| | ) |
| | } |
| | } |
| | } |
| |
|
| | async function getTsConfig(cwd: string): Promise<CompilerOptions> { |
| | const ts: typeof import('typescript') = require( |
| | require.resolve('typescript', { paths: [cwd] }) |
| | ) |
| |
|
| | |
| | |
| | |
| | const tsConfigPath = ts.findConfigFile( |
| | cwd, |
| | ts.sys.fileExists, |
| | 'tsconfig.json' |
| | ) |
| |
|
| | if (!tsConfigPath) { |
| | |
| | |
| | return {} |
| | } |
| |
|
| | const configFile = ts.readConfigFile(tsConfigPath, ts.sys.readFile) |
| | const parsedCommandLine = ts.parseJsonConfigFileContent( |
| | configFile.config, |
| | ts.sys, |
| | cwd |
| | ) |
| |
|
| | return parsedCommandLine.options |
| | } |
| |
|
| | export async function transpileConfig({ |
| | nextConfigPath, |
| | configFileName, |
| | cwd, |
| | }: { |
| | nextConfigPath: string |
| | configFileName: string |
| | cwd: string |
| | }) { |
| | let hasRequire = false |
| | try { |
| | |
| | await verifyTypeScriptSetup(cwd, configFileName) |
| |
|
| | const compilerOptions = await getTsConfig(cwd) |
| | const swcOptions = resolveSWCOptions(cwd, compilerOptions) |
| |
|
| | const nextConfigString = await readFile(nextConfigPath, 'utf8') |
| | |
| | |
| | const { transform } = require('../swc') as typeof import('../swc') |
| | const { code } = await transform(nextConfigString, swcOptions) |
| |
|
| | |
| | if (code.includes('require(')) { |
| | registerHook(swcOptions) |
| | hasRequire = true |
| | } |
| |
|
| | |
| | return requireFromString(code, resolve(cwd, 'next.config.compiled.js')) |
| | } catch (error) { |
| | throw error |
| | } finally { |
| | if (hasRequire) { |
| | deregisterHook() |
| | } |
| | } |
| | } |
| |
|