2022-11-25 14:42:26 +00:00
|
|
|
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)
|
2024-08-16 15:52:08 +01:00
|
|
|
s.replace(/<!--.*?-->/gs, '')
|
2022-11-25 14:42:26 +00:00
|
|
|
|
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
|
|
|
map: s.generateMap({ source: id, includeContent: true }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|