Created
September 6, 2024 20:36
-
-
Save thommyhh/8f63bffb69409472b22e114d07679327 to your computer and use it in GitHub Desktop.
Webpack like manifest.json for Vite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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