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: use relative paths in _metadata.json #7299

Merged
merged 1 commit into from Mar 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -514,7 +514,7 @@ export async function createOptimizeDepsRun(
function commitProcessingDepsCacheSync() {
// Rewire the file paths from the temporal processing dir to the final deps cache dir
const dataPath = path.join(processingCacheDir, '_metadata.json')
writeFile(dataPath, stringifyOptimizedDepsMetadata(metadata))
writeFile(dataPath, stringifyOptimizedDepsMetadata(metadata, depsCacheDir))
// Processing is done, we can now replace the depsCacheDir with processingCacheDir
if (fs.existsSync(depsCacheDir)) {
const rmSync = fs.rmSync ?? fs.rmdirSync // TODO: Remove after support for Node 12 is dropped
Expand Down Expand Up @@ -633,19 +633,33 @@ function parseOptimizedDepsMetadata(
depsCacheDir: string,
processing: Promise<DepOptimizationResult | undefined>
) {
const metadata = JSON.parse(jsonMetadata)
const metadata = JSON.parse(jsonMetadata, (key: string, value: string) => {
// Paths can be absolute or relative to the deps cache dir where
// the _metadata.json is located
if (key === 'file' || key === 'src') {
return normalizePath(path.resolve(depsCacheDir, value))
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
return value
})
for (const o of Object.keys(metadata.optimized)) {
metadata.optimized[o].processing = processing
}
return { ...metadata, discovered: {}, processing }
}

function stringifyOptimizedDepsMetadata(metadata: DepOptimizationMetadata) {
function stringifyOptimizedDepsMetadata(
metadata: DepOptimizationMetadata,
depsCacheDir: string
) {
return JSON.stringify(
metadata,
(key: string, value: any) => {
if (key === 'processing' || key === 'discovered') return

if (key === 'processing' || key === 'discovered') {
return
}
if (key === 'file' || key === 'src') {
return normalizePath(path.relative(depsCacheDir, value))
}
return value
},
2
Expand Down