-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.ts
162 lines (135 loc) · 5.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { defineNuxtModule, useNuxt } from '@nuxt/kit'
import VueMacros from 'unplugin-vue-macros/vite'
import {
type Options as OptionsShortVmodel,
transformShortVmodel,
} from '@vue-macros/short-vmodel'
import { transformBooleanProp } from '@vue-macros/boolean-prop'
import { type Options, resolveOptions } from 'unplugin-vue-macros'
import { type Plugin } from 'vite'
import type {} from '@nuxt/devtools'
import { type VolarOptions } from '@vue-macros/volar'
import { REGEX_SETUP_SFC } from '@vue-macros/common'
export type VueMacrosOptions = Options & {
booleanProp?: {} | false
shortVmodel?: OptionsShortVmodel | false
}
export default defineNuxtModule<VueMacrosOptions>({
meta: {
name: 'unplugin-vue-macros',
configKey: 'macros',
},
defaults: {},
setup(options) {
const nuxt = useNuxt()
const resolvedOptions = resolveOptions(options)
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
function findPluginAndRemove(name: string): Plugin | undefined {
const idx = config.plugins!.findIndex(
(plugin) => plugin && 'name' in plugin && plugin.name === name
)
if (idx === -1) return
const plugin = config.plugins![idx]
config.plugins!.splice(idx, 1)
return plugin as any
}
config.plugins ||= []
const vue = findPluginAndRemove('vite:vue')
const vueJsx = findPluginAndRemove('vite:vue-jsx')
config.plugins.push(
VueMacros({
...resolvedOptions,
plugins: { vue, vueJsx },
nuxtContext: { isClient },
})
)
})
nuxt.hook('prepare:types', (opts) => {
opts.references.push({ types: 'unplugin-vue-macros/macros-global' })
})
nuxt.hook('devtools:customTabs', (tabs) => {
tabs.push({
name: 'vue-macros',
title: 'Vue Macros',
icon: 'https://raw.githubusercontent.com/vue-macros/vue-macros/main/docs/public/favicon.svg',
view: {
type: 'iframe',
src: '/__vue-macros',
},
})
})
nuxt.options.typescript.tsConfig ||= {}
// @ts-expect-error https://github.com/unjs/pkg-types/pull/130
nuxt.options.typescript.tsConfig.vueCompilerOptions ||= {}
const vueCompilerOptions =
// @ts-expect-error
nuxt.options.typescript.tsConfig.vueCompilerOptions
vueCompilerOptions.vueMacros ||= {}
const volarOptions = vueCompilerOptions.vueMacros as VolarOptions
vueCompilerOptions.plugins ||= []
const volarPlugins = vueCompilerOptions.plugins
if (resolvedOptions.defineOptions)
volarPlugins.push('@vue-macros/volar/define-options')
if (resolvedOptions.defineSlots)
volarPlugins.push('@vue-macros/volar/define-slots')
if (resolvedOptions.defineModels)
volarPlugins.push('@vue-macros/volar/define-models')
if (resolvedOptions.defineProps)
volarPlugins.push('@vue-macros/volar/define-props')
if (resolvedOptions.definePropsRefs)
volarPlugins.push('@vue-macros/volar/define-props-refs')
if (resolvedOptions.exportProps)
volarPlugins.push('@vue-macros/volar/export-props')
if (resolvedOptions.jsxDirective)
volarPlugins.push('@vue-macros/volar/jsx-directive')
if (resolvedOptions.defineProp)
vueCompilerOptions.experimentalDefinePropProposal =
resolvedOptions.defineProp.edition || 'kevinEdition'
const viteVue = (nuxt.options.vite.vue ||= {})
if (resolvedOptions.setupSFC) {
viteVue.include ||= [/\.vue$/]
if (!Array.isArray(viteVue.include)) viteVue.include = [viteVue.include]
viteVue.include.push(REGEX_SETUP_SFC)
nuxt.hook('components:extend', (components) => {
for (const component of components) {
component.pascalName = component.pascalName.replace(/Setup$/, '')
component.kebabName = component.kebabName.replace(/-setup$/, '')
}
})
nuxt.hook('pages:extend', (pages) => {
for (const page of pages) {
if (!page.file || !REGEX_SETUP_SFC.test(page.file)) continue
if (page.name) page.name = page.name.replace(/\.setup$/, '')
if (page.path)
page.path = page.path
.replace(/\/index\.setup$/, '/')
.replace(/\.setup$/, '')
}
})
}
if (options.shortVmodel !== false || options.booleanProp) {
viteVue.template ||= {}
viteVue.template.compilerOptions ||= {}
viteVue.template.compilerOptions.nodeTransforms ||= []
const { nodeTransforms } = viteVue.template.compilerOptions
if (options.shortVmodel !== false) {
volarPlugins.push('@vue-macros/volar/short-vmodel')
nodeTransforms.push(transformShortVmodel(options.shortVmodel))
if (options.shortVmodel) {
volarOptions.shortVmodel = {
prefix: options.shortVmodel.prefix,
}
}
}
if (options.booleanProp) nodeTransforms.push(transformBooleanProp())
}
},
})
declare module '@nuxt/schema' {
interface NuxtConfig {
macros?: Options
}
interface NuxtOptions {
macros?: Options
}
}