Skip to content

Commit

Permalink
fix: using stable name for metadataPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jul 8, 2024
1 parent 98d021f commit 5894423
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function renderAssetUrlInJS(
s ||= new MagicString(code)
const [full, referenceId, postfix = ''] = match
const file = ctx.getFileName(referenceId)
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file))
getChunkMetadata(chunk.name)!.importedAssets.add(cleanUrl(file))
const filename = file + postfix
const replacement = toOutputFilePathInJS(
filename,
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// replace asset url references with resolved url.
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const filename = this.getFileName(fileHash) + postfix
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(filename))
getChunkMetadata(chunk.name)!.importedAssets.add(cleanUrl(filename))
return encodeURIPath(
toOutputFilePathInCss(
filename,
Expand Down Expand Up @@ -768,7 +768,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
generatedAssets
.get(config)!
.set(referenceId, { originalName: originalFilename, isEntry })
getChunkMetadata(chunk.fileName)!.importedCss.add(this.getFileName(referenceId))
getChunkMetadata(chunk.name)!.importedCss.add(this.getFileName(referenceId))
} else if (!config.build.ssr) {
// legacy build and inline css

Expand Down Expand Up @@ -828,9 +828,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
},

augmentChunkHash(chunk) {
if (getChunkMetadata(chunk.fileName)?.importedCss.size) {
if (getChunkMetadata(chunk.name)?.importedCss.size) {
let hash = ''
for (const id of getChunkMetadata(chunk.fileName)!.importedCss) {
for (const id of getChunkMetadata(chunk.name)!.importedCss) {
hash += id
}
return hash
Expand Down Expand Up @@ -923,10 +923,10 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
if (pureCssChunkNames.includes(file)) {
const { importedCss, importedAssets } = getChunkMetadata((bundle[file] as OutputChunk).fileName)!
importedCss.forEach((file) =>
getChunkMetadata(chunk.fileName)!.importedCss.add(file),
getChunkMetadata(chunk.name)!.importedCss.add(file),
)
importedAssets.forEach((file) =>
getChunkMetadata(chunk.fileName)!.importedAssets.add(file),
getChunkMetadata(chunk.name)!.importedAssets.add(file),
)
chunkImportsPureCssChunk = true
return false
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})
}

getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
getChunkMetadata(chunk.name)!.importedCss.forEach((file) => {
if (!seen.has(file)) {
seen.add(file)
tags.push({
Expand Down Expand Up @@ -907,7 +907,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const file = this.getFileName(fileHash)
if (chunk) {
getChunkMetadata(chunk.fileName)!.importedAssets.add(cleanUrl(file))
getChunkMetadata(chunk.name)!.importedAssets.add(cleanUrl(file))
}
return encodeURIPath(toOutputAssetFilePath(file)) + postfix
})
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
chunk.imports.forEach(addDeps)
// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
getChunkMetadata(chunk.name)!.importedCss.forEach((file) => {
deps.add(file)
})
}
Expand All @@ -552,8 +552,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
removedPureCssFilesCache.get(config)!
const chunk = removedPureCssFiles.get(filename)
if (chunk) {
if (getChunkMetadata(chunk.fileName)!.importedCss.size) {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
if (getChunkMetadata(chunk.name)!.importedCss.size) {
getChunkMetadata(chunk.name)!.importedCss.forEach((file) => {
deps.add(file)
})
hasRemovedPureCssChunk = true
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

if (getChunkMetadata(chunk.fileName)?.importedCss.size) {
manifestChunk.css = [...getChunkMetadata(chunk.fileName)!.importedCss]
if (getChunkMetadata(chunk.name)?.importedCss.size) {
manifestChunk.css = [...getChunkMetadata(chunk.name)!.importedCss]
}
if (getChunkMetadata(chunk.fileName)?.importedAssets.size) {
manifestChunk.assets = [...getChunkMetadata(chunk.fileName)!.importedAssets]
if (getChunkMetadata(chunk.name)?.importedAssets.size) {
manifestChunk.assets = [...getChunkMetadata(chunk.name)!.importedAssets]
}

return manifestChunk
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function metadataPlugin(): Plugin {
async renderChunk(_code, chunk) {
// Since the chunk come from rust side, mutate it directly will not sync back to rust side.
// The next usage will lost the metadata
chunkMetadataMap.set(chunk.fileName, {
chunkMetadataMap.set(chunk.name, {
importedAssets: new Set(),
importedCss: new Set(),
})
Expand All @@ -26,6 +26,6 @@ export function metadataPlugin(): Plugin {
}
}

export function getChunkMetadata(fileName: string): ChunkMetadata | undefined {
return chunkMetadataMap.get(fileName)
export function getChunkMetadata(name: string): ChunkMetadata | undefined {
return chunkMetadataMap.get(name)
}
6 changes: 3 additions & 3 deletions packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
mappedChunks.push(joinUrlSegments(base, chunk.fileName))
// <link> tags for entry chunks are already generated in static HTML,
// so we only need to record info for non-entry chunks.
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
getChunkMetadata(chunk.name)!.importedCss.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
getChunkMetadata(chunk.fileName)!.importedAssets.forEach((file) => {
getChunkMetadata(chunk.name)!.importedAssets.forEach((file) => {
mappedChunks.push(joinUrlSegments(base, file))
})
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
analyzed.add(filename)
const chunk = bundle[filename] as OutputChunk | undefined
if (chunk) {
getChunkMetadata(chunk.fileName)!.importedCss.forEach((file) => {
getChunkMetadata(chunk.name)!.importedCss.forEach((file) => {
deps.push(joinUrlSegments(base, file)) // TODO:base
})
chunk.imports.forEach(addDeps)
Expand Down

0 comments on commit 5894423

Please sign in to comment.