MCPcopy
hub / github.com/vitejs/vite / loadConfigFromBundledFile

Function loadConfigFromBundledFile

packages/vite/src/node/config.ts:2614–2688  ·  view source on GitHub ↗
(
  fileName: string,
  bundledCode: string,
  isESM: boolean,
)

Source from the content-addressed store, hash-verified

2612
2613const _require = createRequire(/** #__KEEP__ */ import.meta.url)
2614async function loadConfigFromBundledFile(
2615 fileName: string,
2616 bundledCode: string,
2617 isESM: boolean,
2618): Promise<UserConfigExport> {
2619 // for esm, before we can register loaders without requiring users to run node
2620 // with --experimental-loader themselves, we have to do a hack here:
2621 // write it to disk, load it with native Node ESM, then delete the file.
2622 if (isESM) {
2623 // Storing the bundled file in node_modules/ is avoided for Deno
2624 // because Deno only supports Node.js style modules under node_modules/
2625 // and configs with `npm:` import statements will fail when executed.
2626 const nodeModulesDir =
2627 typeof process.versions.deno === 'string'
2628 ? undefined
2629 : findNearestNodeModules(path.dirname(fileName))
2630
2631 let viteTempDir = nodeModulesDir
2632 ? path.resolve(nodeModulesDir, '.vite-temp')
2633 : undefined
2634 if (viteTempDir) {
2635 try {
2636 await fsp.mkdir(viteTempDir, {
2637 recursive: true,
2638 })
2639 } catch (e) {
2640 if (e.code === 'EACCES') {
2641 // If there is no access permission, a temporary configuration file is created by default.
2642 viteTempDir = undefined
2643 } else {
2644 throw e
2645 }
2646 }
2647 }
2648 const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`
2649 const tempFileName = viteTempDir
2650 ? path.resolve(viteTempDir, `${path.basename(fileName)}.${hash}.mjs`)
2651 : `${fileName}.${hash}.mjs`
2652
2653 // Tell Vite Task to ignore node_modules/.vite-temp or the temp config file,
2654 // so the read-write of this path doesn't affect the cache fingerprints.
2655 const pathToIgnore = viteTempDir ?? tempFileName
2656 ignoreInput(pathToIgnore)
2657 ignoreOutput(pathToIgnore)
2658 await fsp.writeFile(tempFileName, bundledCode)
2659 try {
2660 return (await import(pathToFileURL(tempFileName).href)).default
2661 } finally {
2662 fs.unlink(tempFileName, () => {}) // Ignore errors
2663 }
2664 }
2665 // for cjs, we can register a custom loader via `_require.extensions`
2666 else {
2667 const extension = path.extname(fileName)
2668 // We don't use fsp.realpath() here because it has the same behaviour as
2669 // fs.realpath.native. On some Windows systems, it returns uppercase volume
2670 // letters (e.g. "C:\") while the Node.js loader uses lowercase volume letters.
2671 // See https://github.com/vitejs/vite/issues/12923

Callers 1

bundleAndLoadConfigFileFunction · 0.85

Calls 3

findNearestNodeModulesFunction · 0.90
_compileMethod · 0.80
resolveMethod · 0.65

Tested by

no test coverage detected