{"version":3,"sources":["../../../src/build/next-config-ts/transpile-config.ts"],"sourcesContent":["import type { Options as SWCOptions } from '@swc/core'\nimport type { CompilerOptions } from 'typescript'\n\nimport { resolve } from 'node:path'\nimport { readFile } from 'node:fs/promises'\nimport { pathToFileURL } from 'node:url'\nimport { deregisterHook, registerHook, requireFromString } from './require-hook'\nimport { warn, warnOnce } from '../output/log'\nimport { installDependencies } from '../../lib/install-dependencies'\nimport { getNodeOptionsArgs } from '../../server/lib/utils'\n\nfunction resolveSWCOptions(\n cwd: string,\n compilerOptions: CompilerOptions\n): SWCOptions {\n return {\n jsc: {\n parser: {\n syntax: 'typescript',\n },\n ...(compilerOptions.paths ? { paths: compilerOptions.paths } : {}),\n ...(compilerOptions.baseUrl\n ? // Needs to be an absolute path.\n { baseUrl: resolve(cwd, compilerOptions.baseUrl) }\n : compilerOptions.paths\n ? // If paths is given, baseUrl is required.\n { baseUrl: cwd }\n : {}),\n },\n module: {\n type: 'commonjs',\n },\n isModule: 'unknown',\n env: {\n targets: {\n // Setting the Node.js version can reduce unnecessary code generation.\n node: process?.versions?.node ?? '20.19.0',\n },\n },\n } satisfies SWCOptions\n}\n\n// Ported from next/src/lib/verify-typescript-setup.ts\n// Although this overlaps with the later `verifyTypeScriptSetup`,\n// it is acceptable since the time difference in the worst case is trivial,\n// as we are only preparing to install the dependencies once more.\nasync function verifyTypeScriptSetup(cwd: string, configFileName: string) {\n try {\n // Quick module check.\n require.resolve('typescript', { paths: [cwd] })\n } catch (error) {\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n error.code === 'MODULE_NOT_FOUND'\n ) {\n warn(\n `Installing TypeScript as it was not found while loading \"${configFileName}\".`\n )\n\n await installDependencies(cwd, [{ pkg: 'typescript' }], true).catch(\n (err) => {\n if (err && typeof err === 'object' && 'command' in err) {\n console.error(\n `Failed to install TypeScript, please install it manually to continue:\\n` +\n (err as any).command +\n '\\n'\n )\n }\n throw err\n }\n )\n }\n }\n}\n\nasync function getTsConfig(cwd: string): Promise {\n const ts: typeof import('typescript') = require(\n require.resolve('typescript', { paths: [cwd] })\n )\n\n // NOTE: This doesn't fully cover the edge case for setting\n // \"typescript.tsconfigPath\" in next config which is currently\n // a restriction.\n const tsConfigPath = ts.findConfigFile(\n cwd,\n ts.sys.fileExists,\n 'tsconfig.json'\n )\n\n if (!tsConfigPath) {\n // It is ok to not return ts.getDefaultCompilerOptions() because\n // we are only looking for paths and baseUrl from tsConfig.\n return {}\n }\n\n const configFile = ts.readConfigFile(tsConfigPath, ts.sys.readFile)\n const parsedCommandLine = ts.parseJsonConfigFileContent(\n configFile.config,\n ts.sys,\n cwd\n )\n\n return parsedCommandLine.options\n}\n\nexport async function transpileConfig({\n nextConfigPath,\n configFileName,\n cwd,\n}: {\n nextConfigPath: string\n configFileName: string\n cwd: string\n}) {\n try {\n // envs are passed to the workers and preserve the flag\n if (process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED === 'true') {\n try {\n // Node.js v22.10.0+\n // Value is 'strip' or 'transform' based on how the feature is enabled.\n // https://nodejs.org/api/process.html#processfeaturestypescript\n // TODO: Remove `as any` once we bump @types/node to v22.10.0+\n if ((process.features as any).typescript) {\n // Run import() here to catch errors and fallback to legacy resolution.\n return (await import(pathToFileURL(nextConfigPath).href)).default\n }\n\n if (\n getNodeOptionsArgs().includes('--no-experimental-strip-types') ||\n process.execArgv.includes('--no-experimental-strip-types')\n ) {\n warnOnce(\n `Skipped resolving \"${configFileName}\" using Node.js native TypeScript resolution because it was disabled by the \"--no-experimental-strip-types\" flag.` +\n ' Falling back to legacy resolution.' +\n ' Learn more: https://nextjs.org/docs/app/api-reference/config/typescript#using-nodejs-native-typescript-resolver-for-nextconfigts'\n )\n }\n\n // Feature is not enabled, fallback to legacy resolution for current session.\n process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'false'\n } catch (cause) {\n warnOnce(\n `Failed to import \"${configFileName}\" using Node.js native TypeScript resolution.` +\n ' Falling back to legacy resolution.' +\n ' Learn more: https://nextjs.org/docs/app/api-reference/config/typescript#using-nodejs-native-typescript-resolver-for-nextconfigts',\n { cause }\n )\n // Once failed, fallback to legacy resolution for current session.\n process.env.__NEXT_NODE_NATIVE_TS_LOADER_ENABLED = 'false'\n }\n }\n\n // Ensure TypeScript is installed to use the API.\n await verifyTypeScriptSetup(cwd, configFileName)\n const compilerOptions = await getTsConfig(cwd)\n\n return handleCJS({ cwd, nextConfigPath, compilerOptions })\n } catch (cause) {\n throw new Error(`Failed to transpile \"${configFileName}\".`, { cause })\n }\n}\n\nasync function handleCJS({\n cwd,\n nextConfigPath,\n compilerOptions,\n}: {\n cwd: string\n nextConfigPath: string\n compilerOptions: CompilerOptions\n}) {\n const swcOptions = resolveSWCOptions(cwd, compilerOptions)\n let hasRequire = false\n try {\n const nextConfigString = await readFile(nextConfigPath, 'utf8')\n // lazy require swc since it loads React before even setting NODE_ENV\n // resulting loading Development React on Production\n const { transform } = require('../swc') as typeof import('../swc')\n const { code } = await transform(nextConfigString, swcOptions)\n\n // register require hook only if require exists\n if (code.includes('require(')) {\n registerHook(swcOptions)\n hasRequire = true\n }\n\n // filename & extension don't matter here\n return requireFromString(code, resolve(cwd, 'next.config.compiled.js'))\n } catch (error) {\n throw error\n } finally {\n if (hasRequire) {\n deregisterHook()\n }\n }\n}\n"],"names":["transpileConfig","resolveSWCOptions","cwd","compilerOptions","process","jsc","parser","syntax","paths","baseUrl","resolve","module","type","isModule","env","targets","node","versions","verifyTypeScriptSetup","configFileName","require","error","code","warn","installDependencies","pkg","catch","err","console","command","getTsConfig","ts","tsConfigPath","findConfigFile","sys","fileExists","configFile","readConfigFile","readFile","parsedCommandLine","parseJsonConfigFileContent","config","options","nextConfigPath","__NEXT_NODE_NATIVE_TS_LOADER_ENABLED","features","typescript","pathToFileURL","href","default","getNodeOptionsArgs","includes","execArgv","warnOnce","cause","handleCJS","Error","swcOptions","hasRequire","nextConfigString","transform","registerHook","requireFromString","deregisterHook"],"mappings":";;;;+BA2GsB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