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
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 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 @@ -20,6 +21,8 @@ import { FS_PREFIX } from '../constants'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g

export const duplicateAssets: OutputAsset[] = []
timacdonald marked this conversation as resolved.
Show resolved Hide resolved

const rawRE = /(\?|&)raw(?:&|$)/
const urlRE = /(\?|&)url(?:&|$)/

Expand Down Expand Up @@ -470,15 +473,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: map.get(contentHash)!,
type: 'asset',
source: content,
isAsset: true
})
}

url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}` // TODO_BASE
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/node/plugins/manifest.ts
Expand Up @@ -4,6 +4,7 @@ import type { ResolvedConfig } from '..'
import type { Plugin } from '../plugin'
import { normalizePath } from '../utils'
import { cssEntryFilesCache } from './css'
import { duplicateAssets } from './asset'

export type Manifest = Record<string, ManifestChunk>

Expand All @@ -16,6 +17,7 @@ export interface ManifestChunk {
isDynamicEntry?: boolean
imports?: string[]
dynamicImports?: string[]
isDuplicate?: true
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
}

export function manifestPlugin(config: ResolvedConfig): Plugin {
Expand Down Expand Up @@ -122,6 +124,12 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
}
}

duplicateAssets.forEach((asset) => {
const chunk = createAsset(asset)
chunk.isDuplicate = true
manifest[asset.name!] = chunk
})

outputCount++
const output = config.build.rollupOptions?.output
const outputLength = Array.isArray(output) ? output.length : 1
Expand Down