Skip to content

Commit

Permalink
fix: include duplicate assets in the bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Sep 1, 2022
1 parent 9ac5075 commit 68c87fd
Showing 1 changed file with 16 additions and 1 deletion.
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
})

// 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 {
duplicateAssets.push({
name,
fileName,
type: 'asset',
source: content,
isAsset: true
})
}

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

0 comments on commit 68c87fd

Please sign in to comment.