1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-19 07:19:58 +00:00
elk/modules/purge-comments.ts

29 lines
652 B
TypeScript
Raw Normal View History

import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
import MagicString from 'magic-string'
export default defineNuxtModule({
meta: {
name: 'purge-comments',
},
setup() {
addVitePlugin({
name: 'purge-comments',
enforce: 'pre',
transform: (code, id) => {
if (!id.endsWith('.vue') || !code.includes('<!--'))
return
const s = new MagicString(code)
s.replace(/<!--(?:.*?)-->/sg, '')
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ source: id, includeContent: true }),
}
}
},
})
},
})