diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index 33bde59f8b6e55..7a1141f378c4dc 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/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 + +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 @@ -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 } } }