Skip to content

Instantly share code, notes, and snippets.

@thommyhh
Created September 6, 2024 20:36
Show Gist options
  • Save thommyhh/8f63bffb69409472b22e114d07679327 to your computer and use it in GitHub Desktop.
Save thommyhh/8f63bffb69409472b22e114d07679327 to your computer and use it in GitHub Desktop.
Webpack like manifest.json for Vite
import * as path from 'node:path';
import * as fs from "node:fs";
const ViteWebpackManifest = () => {
let base = ''
return {
configResolved(config) {
// Remove leading "/" from base URL
base = config.base.replace(/^\//, '')
},
writeBundle(outputOptions, bundle) {
const manifest = {}
for (const [fileName, chunkInfo] of Object.entries(bundle)) {
if (chunkInfo.type === 'asset' && chunkInfo.name) {
manifest[base + chunkInfo.name] = base + chunkInfo.fileName
} else if (chunkInfo.type === 'chunk') {
manifest[base + chunkInfo.name + '.js'] = base + chunkInfo.fileName
}
}
const manifestPath = path.resolve(outputOptions.dir, 'manifest.json')
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4))
}
}
}
export default ViteWebpackManifest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment