forked from vitejs/vite
/
index.ts
163 lines (144 loc) · 4.14 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
163
try {
require.resolve('@vue/compiler-sfc')
} catch (e) {
throw new Error(
'@vitejs/plugin-vue requires @vue/compiler-sfc to be present in the dependency ' +
'tree.'
)
}
import fs from 'fs'
import { Plugin, ViteDevServer } from 'vite'
import { createFilter } from '@rollup/pluginutils'
import {
SFCBlock,
SFCScriptCompileOptions,
SFCStyleCompileOptions,
SFCTemplateCompileOptions
} from '@vue/compiler-sfc'
import { parseVueRequest } from './utils/query'
import { getDescriptor } from './utils/descriptorCache'
import { getResolvedScript } from './script'
import { transformMain } from './main'
import { handleHotUpdate } from './handleHotUpdate'
import { transformTemplateAsModule } from './template'
import { transformStyle } from './style'
// extend the descriptor so we can store the scopeId on it
declare module '@vue/compiler-sfc' {
interface SFCDescriptor {
id: string
}
}
export { parseVueRequest, VueQuery } from './utils/query'
export interface Options {
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
ssr?: boolean
isProduction?: boolean
// options to pass on to @vue/compiler-sfc
script?: SFCScriptCompileOptions
template?: SFCTemplateCompileOptions
style?: SFCStyleCompileOptions
}
export interface ResolvedOptions extends Options {
root: string
devServer?: ViteDevServer
}
export default function vuePlugin(rawOptions: Options = {}): Plugin {
let options: ResolvedOptions = {
ssr: false,
isProduction: process.env.NODE_ENV === 'production',
...rawOptions,
root: process.cwd()
}
const filter = createFilter(
rawOptions.include || /\.vue$/,
rawOptions.exclude
)
return {
name: 'vite:vue',
handleHotUpdate(ctx) {
if (!filter(ctx.file)) {
return
}
return handleHotUpdate(ctx)
},
config(config) {
// provide default values for vue runtime esm defines
config.define = {
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
...config.define
}
},
configResolved(config) {
options = {
...options,
root: config.root,
isProduction: config.isProduction
}
},
configureServer(server) {
options.devServer = server
},
async resolveId(id, importer) {
// serve subpart requests (*?vue) as virtual modules
if (parseVueRequest(id).query.vue) {
return id
}
},
load(id) {
const { filename, query } = parseVueRequest(id)
// select corresponding block for subpart virtual modules
if (query.vue) {
if (query.src) {
return fs.readFileSync(filename, 'utf-8')
}
const descriptor = getDescriptor(filename)!
let block: SFCBlock | null | undefined
if (query.type === 'script') {
// handle <scrip> + <script setup> merge via compileScript()
block = getResolvedScript(descriptor, options.ssr)
} else if (query.type === 'template') {
block = descriptor.template!
} else if (query.type === 'style') {
block = descriptor.styles[query.index!]
} else if (query.index != null) {
block = descriptor.customBlocks[query.index]
}
if (block) {
return {
code: block.content,
map: block.map as any
}
}
}
},
transform(code, id) {
const { filename, query } = parseVueRequest(id)
if (!query.vue && !filter(filename)) {
return
}
if (!query.vue) {
// main request
return transformMain(code, filename, options, this)
} else {
// sub block request
const descriptor = getDescriptor(filename)!
if (query.type === 'template') {
return transformTemplateAsModule(code, descriptor, options, this)
} else if (query.type === 'style') {
return transformStyle(
code,
descriptor,
Number(query.index),
options,
this
)
}
}
}
}
}
// overwrite for cjs require('...')() usage
module.exports = vuePlugin
vuePlugin['default'] = vuePlugin