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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modify manifest entries for asset entrypoints (close #1765) #1774

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface ManifestChunk {
dynamicImports?: string[]
}

function isEmptyChunk(chunk: OutputChunk): boolean {
return !chunk.code || (chunk.code.length <= 1 && !chunk.code.trim())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the cases I observed, code was always \n, so there's potential for simplification here.

Didn't want to run trim() on a large string, hence the check for length.

}

export function manifestPlugin(config: ResolvedConfig): Plugin {
const manifest: Manifest = {}

Expand All @@ -42,6 +46,15 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

function replaceFileWithAsset(chunk: OutputChunk) {
const name = chunk.facadeModuleId ? getChunkName(chunk) : chunk.name
const asset = Object.values(bundle).find(
(otherChunk) => otherChunk.type === 'asset' && otherChunk.name == name
)
if (asset) chunk.fileName = asset.fileName
return chunk
}
Copy link
Contributor Author

@ElMassimo ElMassimo Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to optimize it until we discuss the approach.

We should use an approach similar to chunkToEmittedAssetsMap, where we index the corresponding asset in a Map as we process the chunks, so that we can efficiently retrieve them here.

An alternative is to simply replace the fileName in the chunk beforehand, but I'm not sure if the processing order would allow that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since multiple chunks can have the same name, this can result in wrong mapping if two entry assets have the same name. e.g. logo.svg and logo.png

Copy link
Contributor Author

@ElMassimo ElMassimo Jan 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we would be comparing resolved names, which include the file extension, and entrypoint assets always include the file extension in their name, so there wouldn't be a collision, correct?


function createChunk(chunk: OutputChunk): ManifestChunk {
const manifestChunk: ManifestChunk = {
file: chunk.fileName
Expand Down Expand Up @@ -83,6 +96,9 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
if (isEmptyChunk(chunk)) {
replaceFileWithAsset(chunk)
}
manifest[getChunkName(chunk)] = createChunk(chunk)
}
}
Expand Down