[READ-ONLY] Mirror of https://github.com/danielroe/roe.dev. This is the code and content for my personal website, built in Nuxt.
roe.dev
1import { addVitePlugin, defineNuxtModule, useNuxt } from 'nuxt/kit'
2import { findStaticImports } from 'mlly'
3import MagicString from 'magic-string'
4
5export default defineNuxtModule({
6 meta: {
7 name: 'tree-shake',
8 },
9 setup () {
10 const nuxt = useNuxt()
11 addVitePlugin({
12 name: 'tree-shake',
13 transform (code, id) {
14 if (nuxt.options.dev) return
15
16 if (!code.includes('Transition') && !code.includes('KeepAlive')) return
17
18 const s = new MagicString(code)
19 const imports = findStaticImports(code)
20
21 for (const i of imports) {
22 if (i.specifier !== 'vue') continue
23 const hasTransition = i.imports.includes('Transition')
24 const hasKeepAlive = i.imports.includes('KeepAlive')
25 if (hasTransition || hasKeepAlive) {
26 s.replace(
27 i.code,
28 i.code.replace('Transition, ', '').replace('KeepAlive, ', ''),
29 )
30 }
31 if (hasTransition) s.prepend('const Transition = {};\n')
32 if (hasKeepAlive) s.prepend('const KeepAlive = {};\n')
33 }
34
35 if (s.hasChanged()) {
36 return {
37 code: s.toString(),
38 map: s.generateMap({ source: id, includeContent: true }),
39 }
40 }
41 },
42 })
43 },
44})