Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): write temporary vite config to node_modules #18509

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(config): write temporary vite config to node_modules
  • Loading branch information
bluwy committed Oct 29, 2024
commit ce23bcc290d8f26578e57f1cc7443353dc89d4b6
26 changes: 17 additions & 9 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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 @@ -1689,16 +1689,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/configs/'), {
recursive: true,
})
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`
const tempFileName = nodeModulesDir
? path.resolve(
nodeModulesDir,
`.vite/configs/${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 @@ -240,6 +240,21 @@ function getResolveCacheKey(
].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