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

feat: include duplicate assets in the manifest #9928

Merged
merged 6 commits into from Sep 22, 2022
Merged
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Expand Up @@ -5,6 +5,7 @@ import { Buffer } from 'node:buffer'
import * as mrmime from 'mrmime'
import type {
NormalizedOutputOptions,
OutputAsset,
OutputOptions,
PluginContext,
PreRenderedAsset,
Expand All @@ -25,6 +26,8 @@ const urlRE = /(\?|&)url(?:&|$)/

const assetCache = new WeakMap<ResolvedConfig, Map<string, string>>()

const duplicateAssets: OutputAsset[] = []

const assetHashToFilenameMap = new WeakMap<
ResolvedConfig,
Map<string, string>
Expand Down Expand Up @@ -182,6 +185,10 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
},

generateBundle(_, bundle) {
duplicateAssets.forEach((asset) => {
bundle[asset.name!] = asset
})
timacdonald marked this conversation as resolved.
Show resolved Hide resolved

// do not emit assets for SSR build
if (config.command === 'build' && config.build.ssr) {
for (const file in bundle) {
Expand Down Expand Up @@ -470,15 +477,23 @@ async function fileToBuiltUrl(
map.set(contentHash, fileName)
}
const emittedSet = emittedHashMap.get(config)!
const name = normalizePath(path.relative(config.root, file))
if (!emittedSet.has(contentHash)) {
const name = normalizePath(path.relative(config.root, file))
pluginContext.emitFile({
name,
fileName,
type: 'asset',
source: content
})
emittedSet.add(contentHash)
} else {
timacdonald marked this conversation as resolved.
Show resolved Hide resolved
duplicateAssets.push({
name,
fileName,
type: 'asset',
source: content,
isAsset: true
})
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}` // TODO_BASE
Expand Down