Skip to content

Instantly share code, notes, and snippets.

@malko
Last active September 26, 2024 15:56
Show Gist options
  • Save malko/1f2e188be716063d8526e36de42a7884 to your computer and use it in GitHub Desktop.
Save malko/1f2e188be716063d8526e36de42a7884 to your computer and use it in GitHub Desktop.
Rollup plugin to enable banner/footer when using vite
/* Include the following code inside your viteconfig then build.rollupOptions.output[banner|footer] will work as intended */
const RollupBannerPlugin = {
name: 'banner',
enforce: 'post',
generateBundle(options, bundle) {
const banner = options.banner() || ''
const footer = options.footer() || ''
for (const module of Object.values(bundle)) {
if (module.type === 'chunk') {
const shebang = module.code.match(/^#![^\n]*\n/)
if (shebang) {
module.code = shebang[0] + banner + module.code.slice(shebang[0].length) + footer
} else {
module.code = banner + module.code + footer
}
// should change source map if needed
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment