Skip to content

Commit

Permalink
fix(manifest): avoid chunks with same name overwriting one another
Browse files Browse the repository at this point in the history
close #1632
  • Loading branch information
yyx990803 committed Jan 22, 2021
1 parent 5d77665 commit cf81aa3
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions packages/vite/src/node/plugins/manifest.ts
@@ -1,15 +1,20 @@
import path from 'path'
import { ResolvedConfig } from '..'
import { Plugin } from '../plugin'

type Manifest = Record<string, ManifestEntry>

interface ManifestEntry {
file: string
facadeModuleId?: string
isEntry?: boolean
isDynamicEntry?: boolean
imports?: string[]
dynamicImports?: string[]
}

export function manifestPlugin(config: ResolvedConfig): Plugin {
const manifest: Record<
string,
{
file: string
imports?: string[],
dynamicImports?: string[]
}
> = {}
const manifest: Manifest = {}

let outputCount = 0

Expand All @@ -20,18 +25,41 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
if (chunk.isEntry || chunk.isDynamicEntry) {
const name =
let name =
format === 'system' && !chunk.name.includes('-legacy')
? chunk.name + '-legacy'
: chunk.name
manifest[name + '.js'] = {
let dedupeIndex = 0
while (name + '.js' in manifest) {
name = `${name}-${++dedupeIndex}`
}
const entry: ManifestEntry = {
isEntry: chunk.isEntry,
isDynamicEntry: chunk.isDynamicEntry,
file: chunk.fileName,
imports: chunk.imports,
dynamicImports: chunk.dynamicImports
}

if (
chunk.facadeModuleId &&
chunk.facadeModuleId.startsWith(config.root)
) {
entry.facadeModuleId = chunk.facadeModuleId.slice(
config.root.length + 1
)
}

manifest[name + '.js'] = entry
}
} else if (chunk.name) {
manifest[chunk.name] = { file: chunk.fileName }
const ext = path.extname(chunk.name) || ''
let name = chunk.name.slice(0, -ext.length)
let dedupeIndex = 0
while (name + ext in manifest) {
name = `${name}-${++dedupeIndex}`
}
manifest[name + ext] = { file: chunk.fileName }
}
}

Expand Down

0 comments on commit cf81aa3

Please sign in to comment.