Skip to content

Commit

Permalink
fix(config): write temporary vite config to node_modules (#18509)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 1, 2024
1 parent eefe895 commit 72eaef5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { createLogger } from './logger'
import type { DepOptimizationOptions } from './optimizer'
import type { JsonOptions } from './plugins/json'
import type { PackageCache } from './packages'
import { findNearestPackageData } from './packages'
import { findNearestNodeModules, findNearestPackageData } from './packages'
import { loadEnv, resolveEnvPrefix } from './env'
import type { ResolvedSSROptions, SSROptions } from './ssr'
import { resolveSSROptions } from './ssr'
Expand Down Expand Up @@ -1717,16 +1717,24 @@ async function loadConfigFromBundledFile(
// with --experimental-loader themselves, we have to do a hack here:
// write it to disk, load it with native Node ESM, then delete the file.
if (isESM) {
const fileBase = `${fileName}.timestamp-${Date.now()}-${Math.random()
.toString(16)
.slice(2)}`
const fileNameTmp = `${fileBase}.mjs`
const fileUrl = `${pathToFileURL(fileBase)}.mjs`
await fsp.writeFile(fileNameTmp, bundledCode)
const nodeModulesDir = findNearestNodeModules(path.dirname(fileName))
if (nodeModulesDir) {
await fsp.mkdir(path.resolve(nodeModulesDir, '.vite-temp/'), {
recursive: true,
})
}
const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`
const tempFileName = nodeModulesDir
? path.resolve(
nodeModulesDir,
`.vite-temp/${path.basename(fileName)}.${hash}.mjs`,
)
: `${fileName}.${hash}.mjs`
await fsp.writeFile(tempFileName, bundledCode)
try {
return (await import(fileUrl)).default
return (await import(pathToFileURL(tempFileName).href)).default
} finally {
fs.unlink(fileNameTmp, () => {}) // Ignore errors
fs.unlink(tempFileName, () => {}) // Ignore errors
}
}
// for cjs, we can register a custom loader via `_require.extensions`
Expand Down
15 changes: 15 additions & 0 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ function getResolveCacheKey(key: string, options: InternalResolveOptions) {
].join('|')
}

export function findNearestNodeModules(basedir: string): string | null {
while (basedir) {
const pkgPath = path.join(basedir, 'node_modules')
if (tryStatSync(pkgPath)?.isDirectory()) {
return pkgPath
}

const nextBasedir = path.dirname(basedir)
if (nextBasedir === basedir) break
basedir = nextBasedir
}

return null
}

export function watchPackageDataPlugin(packageCache: PackageCache): Plugin {
// a list of files to watch before the plugin is ready
const watchQueue = new Set<string>()
Expand Down

0 comments on commit 72eaef5

Please sign in to comment.