{"version":3,"sources":["../../../../src/build/next-config-ts/transpile-config.ts"],"sourcesContent":["import type { Options as SWCOptions } from '@swc/core'\n\nimport path from 'node:path'\nimport { readFileSync, existsSync } from 'node:fs'\nimport { pathToFileURL } from 'node:url'\nimport { deregisterHook, registerHook, requireFromString } from './require-hook'\nimport { warn, warnOnce } from '../output/log'\nimport { getNodeOptionsArgs } from '../../server/lib/utils'\nimport {\n  loadTsConfigOptions,\n  type RelevantCompilerOptions,\n} from '../../lib/typescript/loadTsConfig'\n\nfunction resolveSWCOptions(\n  cwd: string,\n  compilerOptions: RelevantCompilerOptions\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: path.resolve(cwd, compilerOptions.baseUrl) }\n        : compilerOptions.paths\n          ? // If paths is given, baseUrl is required.\n            { baseUrl: compilerOptions.pathsBasePath ?? 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\nasync function loadTsConfig(dir: string): Promise<RelevantCompilerOptions> {\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  // It's a chicken-and-egg problem since we need to transpile\n  // the next config to get that value.\n  const resolvedTsConfigPath = path.join(dir, 'tsconfig.json')\n\n  if (!existsSync(resolvedTsConfigPath)) {\n    return {}\n  }\n\n  return loadTsConfigOptions(resolvedTsConfigPath)\n}\n\nexport async function transpileConfig({\n  nextConfigPath,\n  dir,\n}: {\n  nextConfigPath: string\n  dir: 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 \"${path.basename(nextConfigPath)}\" 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 \"${path.basename(nextConfigPath)}\" 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    const compilerOptions = await loadTsConfig(dir)\n    return handleCJS({ dir, nextConfigPath, compilerOptions })\n  } catch (cause) {\n    throw new Error(`Failed to transpile \"${path.basename(nextConfigPath)}\".`, {\n      cause,\n    })\n  }\n}\n\nasync function handleCJS({\n  dir,\n  nextConfigPath,\n  compilerOptions,\n}: {\n  dir: string\n  nextConfigPath: string\n  compilerOptions: RelevantCompilerOptions\n}) {\n  const swcOptions = resolveSWCOptions(dir, compilerOptions)\n  let hasRequire = false\n  try {\n    const nextConfigString = readFileSync(nextConfigPath, 'utf8')\n    // lazy require swc since it loads React before even setting NODE_ENV\n    // resulting loading Development React on Production\n    const { loadBindings } = require('../swc') as typeof import('../swc')\n    const bindings = await loadBindings()\n    const { code } = await bindings.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    const config = requireFromString(\n      code,\n      path.resolve(dir, 'next.config.compiled.js')\n    )\n    // At this point we have already loaded the bindings without this configuration setting due to the `transform` call above.\n    // Possibly we fell back to wasm in which case, it all works out but if not we need to warn\n    // that the configuration was ignored.\n    if (config?.experimental?.useWasmBinary && !bindings.isWasm) {\n      warn(\n        'Using a next.config.ts file is incompatible with `experimental.useWasmBinary` unless ' +\n          '`--experimental-next-config-strip-types` is also passed.\\nSetting `useWasmBinary` to `false'\n      )\n      config.experimental.useWasmBinary = false\n    }\n    return config\n  } catch (error) {\n    throw error\n  } finally {\n    if (hasRequire) {\n      deregisterHook()\n    }\n  }\n}\n"],"names":["path","readFileSync","existsSync","pathToFileURL","deregisterHook","registerHook","requireFromString","warn","warnOnce","getNodeOptionsArgs","loadTsConfigOptions","resolveSWCOptions","cwd","compilerOptions","process","jsc","parser","syntax","paths","baseUrl","resolve","pathsBasePath","module","type","isModule","env","targets","node","versions","loadTsConfig","dir","resolvedTsConfigPath","join","transpileConfig","nextConfigPath","__NEXT_NODE_NATIVE_TS_LOADER_ENABLED","features","typescript","href","default","includes","execArgv","basename","cause","handleCJS","Error","swcOptions","hasRequire","config","nextConfigString","loadBindings","require","bindings","code","transform","experimental","useWasmBinary","isWasm","error"],"mappings":"AAEA,OAAOA,UAAU,YAAW;AAC5B,SAASC,YAAY,EAAEC,UAAU,QAAQ,UAAS;AAClD,SAASC,aAAa,QAAQ,WAAU;AACxC,SAASC,cAAc,EAAEC,YAAY,EAAEC,iBAAiB,QAAQ,iBAAgB;AAChF,SAASC,IAAI,EAAEC,QAAQ,QAAQ,gBAAe;AAC9C,SAASC,kBAAkB,QAAQ,yBAAwB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