This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
index.ts
176 lines (151 loc) · 4.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
import fs from 'fs'
import type { SFCBlock, TemplateCompileOptions } from '@vue/component-compiler-utils'
import { createFilter } from '@rollup/pluginutils'
import type { Plugin, ViteDevServer } from 'vite'
import { normalizeComponentCode } from './utils/componentNormalizer'
import { vueHotReloadCode } from './utils/vueHotReload'
import { parseVueRequest } from './utils/query'
import { transformMain } from './main'
import { compileSFCTemplate } from './template'
import { getDescriptor } from './utils/descriptorCache'
import { transformStyle } from './style'
import { handleHotUpdate } from './hmr'
import { transformVueJsx } from './jsxTransform'
export const vueComponentNormalizer = '\0/vite/vueComponentNormalizer'
export const vueHotReload = '\0/vite/vueHotReload'
// extend the descriptor so we can store the scopeId on it
declare module '@vue/component-compiler-utils' {
interface SFCDescriptor {
id: string
}
}
export interface VueViteOptions {
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
/**
* The options for `@vue/component-compiler-utils`.
*/
vueTemplateOptions?: Partial<TemplateCompileOptions>
/**
* The options for jsx transform
* @default false
*/
jsx?: boolean
/**
* The options for `@vue/babel-preset-jsx`
*/
jsxOptions?: Record<string, any>
/**
* The options for esbuild to transform script code
* @default 'esnext'
* @example 'esnext' | ['esnext','chrome58','firefox57','safari11','edge16','node12']
*/
target?: string | string[]
}
export interface ResolvedOptions extends VueViteOptions {
root: string
devServer?: ViteDevServer
isProduction: boolean
target?: string | string[]
}
export function createVuePlugin(rawOptions: VueViteOptions = {}): Plugin {
const options: ResolvedOptions = {
isProduction: process.env.NODE_ENV === 'production',
...rawOptions,
root: process.cwd(),
}
const filter = createFilter(options.include || /\.vue$/, options.exclude)
return {
name: 'vite-plugin-vue2',
config() {
if (options.jsx) {
return {
esbuild: {
include: /\.ts$/,
exclude: /\.(tsx|jsx)$/,
},
}
}
},
handleHotUpdate(ctx) {
if (!filter(ctx.file))
return
return handleHotUpdate(ctx, options)
},
configResolved(config) {
options.isProduction = config.isProduction
options.root = config.root
},
configureServer(server) {
options.devServer = server
},
async resolveId(id) {
if (id === vueComponentNormalizer || id === vueHotReload)
return id
// serve subpart requests (*?vue) as virtual modules
if (parseVueRequest(id).query.vue)
return id
},
load(id) {
if (id === vueComponentNormalizer)
return normalizeComponentCode
if (id === vueHotReload)
return vueHotReloadCode
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')
block = descriptor.script!
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,
}
}
}
},
async transform(code, id) {
const { filename, query } = parseVueRequest(id)
if (/\.(tsx|jsx)$/.test(id))
return transformVueJsx(code, id, options.jsxOptions)
if ((!query.vue && !filter(filename)) || query.raw)
return
if (!query.vue) {
// main request
return await transformMain(code, filename, options, this as any)
}
const descriptor = getDescriptor(
query.from ? decodeURIComponent(query.from) : filename,
)!
// sub block request
if (query.type === 'template') {
return compileSFCTemplate(
code,
descriptor.template!,
filename,
options,
this as any,
)
}
if (query.type === 'style') {
return await transformStyle(
code,
filename,
descriptor,
Number(query.index),
this as any,
)
}
},
}
}