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 Aug 31, 2022
1 parent 9ac5075 commit c0bc411
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -8,7 +8,8 @@ import type {
OutputOptions,
PluginContext,
PreRenderedAsset,
RenderedChunk
RenderedChunk,
OutputAsset
} from 'rollup'
import MagicString from 'magic-string'
import colors from 'picocolors'
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,12 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
},

generateBundle(_, bundle) {
duplicateAssets.forEach((asset) => {
if (asset.name) {
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 +479,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 c0bc411

Please sign in to comment.