|
| 1 | +import type { Manifest } from 'vite' |
| 2 | +import { isJS, isCSS } from './utils' |
| 3 | + |
| 4 | +// Comment out in dev mode for better type support |
| 5 | +// const type = Symbol('type') |
| 6 | +// type As<T, L> = T & { [type]: L } |
| 7 | +type Identifier = string // & As<string, 'Identifier'> |
| 8 | +type OutputPath = string // & As<string, 'OutputPath'> |
| 9 | + |
| 10 | +// Vue2 Webpack client manifest format |
| 11 | +export interface LegacyClientManifest { |
| 12 | + publicPath: string |
| 13 | + all: Array<OutputPath> |
| 14 | + initial: Array<OutputPath> |
| 15 | + async: Array<OutputPath> |
| 16 | + modules: Record<Identifier, Array<number>> |
| 17 | + hasNoCssVersion?: { [file: string]: boolean } |
| 18 | +} |
| 19 | + |
| 20 | +export function isLegacyClientManifest (clientManifest: Manifest | LegacyClientManifest): clientManifest is LegacyClientManifest { |
| 21 | + return 'all' in clientManifest && 'initial' in clientManifest |
| 22 | +} |
| 23 | + |
| 24 | +function getIdentifier (output: OutputPath): Identifier |
| 25 | +function getIdentifier (output?: undefined): null |
| 26 | +function getIdentifier (output?: OutputPath): null | Identifier { |
| 27 | + return output ? `_${output}` as Identifier : null |
| 28 | +} |
| 29 | + |
| 30 | +export function normalizeClientManifest (manifest: Manifest | LegacyClientManifest = {}): Manifest { |
| 31 | + if (!isLegacyClientManifest(manifest)) { |
| 32 | + return manifest |
| 33 | + } |
| 34 | + |
| 35 | + // Upgrade legacy manifest |
| 36 | + // https://github.com/nuxt-contrib/vue-bundle-renderer/issues/12 |
| 37 | + const clientManifest: Manifest = {} |
| 38 | + |
| 39 | + // Initialize with all keys |
| 40 | + for (const outfile of manifest.all) { |
| 41 | + if (isJS(outfile)) { |
| 42 | + clientManifest[getIdentifier(outfile)] = { |
| 43 | + file: outfile |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Prepare first entrypoint to receive extra data |
| 49 | + const first = getIdentifier(manifest.initial.find(isJS)!) |
| 50 | + if (first) { |
| 51 | + if (!(first in clientManifest)) { |
| 52 | + throw new Error( |
| 53 | + `Invalid manifest - initial entrypoint not in \`all\`: ${manifest.initial.find(isJS)}` |
| 54 | + ) |
| 55 | + } |
| 56 | + clientManifest[first].css = [] |
| 57 | + clientManifest[first].assets = [] |
| 58 | + clientManifest[first].dynamicImports = [] |
| 59 | + } |
| 60 | + |
| 61 | + for (const outfile of manifest.initial) { |
| 62 | + if (isJS(outfile)) { |
| 63 | + clientManifest[getIdentifier(outfile)].isEntry = true |
| 64 | + } else if (isCSS(outfile) && first) { |
| 65 | + clientManifest[first].css!.push(outfile) |
| 66 | + } else if (first) { |
| 67 | + clientManifest[first].assets!.push(outfile) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + for (const outfile of manifest.async) { |
| 72 | + if (isJS(outfile)) { |
| 73 | + const identifier = getIdentifier(outfile) |
| 74 | + if (!(identifier in clientManifest)) { |
| 75 | + throw new Error(`Invalid manifest - async module not in \`all\`: ${outfile}`) |
| 76 | + } |
| 77 | + clientManifest[identifier].isDynamicEntry = true |
| 78 | + clientManifest[first].dynamicImports!.push(identifier) |
| 79 | + } else if (first) { |
| 80 | + // Add assets (CSS/JS) as dynamic imports to first entrypoints |
| 81 | + // as a workaround so can be prefetched. |
| 82 | + const key = isCSS(outfile) ? 'css' : 'assets' |
| 83 | + const identifier = getIdentifier(outfile) |
| 84 | + clientManifest[identifier] = { |
| 85 | + file: '' as OutputPath, |
| 86 | + [key]: [outfile] |
| 87 | + } |
| 88 | + clientManifest[first].dynamicImports!.push(identifier) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Map modules to virtual entries |
| 93 | + for (const [moduleId, importIndexes] of Object.entries(manifest.modules)) { |
| 94 | + const jsFiles = importIndexes.map(index => manifest.all[index]).filter(isJS) |
| 95 | + jsFiles.forEach((file) => { |
| 96 | + const identifier = getIdentifier(file) |
| 97 | + clientManifest[identifier] = { |
| 98 | + ...clientManifest[identifier], |
| 99 | + file |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + const mappedIndexes = importIndexes.map(index => manifest.all[index]) |
| 104 | + clientManifest[moduleId as Identifier] = { |
| 105 | + file: '' as OutputPath, |
| 106 | + imports: jsFiles.map(id => getIdentifier(id)), |
| 107 | + css: mappedIndexes.filter(isCSS), |
| 108 | + assets: mappedIndexes.filter(i => !isJS(i) && !isCSS(i)) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return clientManifest |
| 113 | +} |
0 commit comments