Skip to content

Commit

Permalink
fix: handle legacy chunks in manifest
Browse files Browse the repository at this point in the history
fix #1551
  • Loading branch information
yyx990803 committed Jan 19, 2021
1 parent 8fb2511 commit 123b6f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function resolveBuildPlugins(
...(options.minify && options.minify !== 'esbuild'
? [terserPlugin(options.terserOptions)]
: []),
...(options.manifest ? [manifestPlugin()] : []),
...(options.manifest ? [manifestPlugin(config)] : []),
...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
...(!config.logLevel || config.logLevel === 'info'
? [buildReporterPlugin(config)]
Expand Down
29 changes: 21 additions & 8 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ResolvedConfig } from '..'
import { Plugin } from '../plugin'

export function manifestPlugin(): Plugin {
export function manifestPlugin(config: ResolvedConfig): Plugin {
const manifest: Record<
string,
{
Expand All @@ -9,14 +10,20 @@ export function manifestPlugin(): Plugin {
}
> = {}

let outputCount = 0

return {
name: 'vite:manifest',
generateBundle(_options, bundle) {
generateBundle({ format }, bundle) {
for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
if (chunk.isEntry) {
manifest[chunk.name + '.js'] = {
const name =
format === 'system' && !chunk.name.includes('-legacy')
? chunk.name + '-legacy'
: chunk.name
manifest[name + '.js'] = {
file: chunk.fileName,
imports: chunk.imports
}
Expand All @@ -25,11 +32,17 @@ export function manifestPlugin(): Plugin {
manifest[chunk.name] = { file: chunk.fileName }
}
}
this.emitFile({
fileName: 'manifest.json',
type: 'asset',
source: JSON.stringify(manifest, null, 2)
})

outputCount++
const output = config.build.rollupOptions?.output
const outputLength = Array.isArray(output) ? output.length : 1
if (outputCount >= outputLength) {
this.emitFile({
fileName: `manifest.json`,
type: 'asset',
source: JSON.stringify(manifest, null, 2)
})
}
}
}
}

0 comments on commit 123b6f6

Please sign in to comment.