-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
template.ts
185 lines (171 loc) · 5.15 KB
/
template.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
177
178
179
180
181
182
183
184
185
import path from 'path'
import slash from 'slash'
import type {
SFCDescriptor,
SFCTemplateCompileOptions,
SFCTemplateCompileResults,
CompilerOptions
} from 'vue/compiler-sfc'
import type { PluginContext, TransformPluginContext } from 'rollup'
import type { ResolvedOptions } from '.'
import { getResolvedScript } from './script'
import { createRollupError } from './utils/error'
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function transformTemplateAsModule(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: TransformPluginContext,
ssr: boolean
) {
const result = compile(code, descriptor, options, pluginContext, ssr)
let returnCode = result.code
if (
options.devServer &&
options.devServer.config.server.hmr !== false &&
!ssr &&
!options.isProduction
) {
returnCode += `\nimport.meta.hot.accept(({ render }) => {
__VUE_HMR_RUNTIME__.rerender(${JSON.stringify(descriptor.id)}, render)
})`
}
return {
code: returnCode,
map: result.map
}
}
/**
* transform the template directly in the main SFC module
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function transformTemplateInMain(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean
): SFCTemplateCompileResults {
const result = compile(code, descriptor, options, pluginContext, ssr)
return {
...result,
code: result.code.replace(
/\nexport (function|const) (render|ssrRender)/,
'\n$1 _sfc_$2'
)
}
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function compile(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean
) {
const filename = descriptor.filename
const result = options.compiler.compileTemplate({
...resolveTemplateCompilerOptions(descriptor, options, ssr)!,
source: code
})
if (result.errors.length) {
result.errors.forEach((error) =>
pluginContext.error(
typeof error === 'string'
? { id: filename, message: error }
: createRollupError(filename, error)
)
)
}
if (result.tips.length) {
result.tips.forEach((tip) =>
pluginContext.warn({
id: filename,
message: tip
})
)
}
return result
}
export function resolveTemplateCompilerOptions(
descriptor: SFCDescriptor,
options: ResolvedOptions,
ssr: boolean
): Omit<SFCTemplateCompileOptions, 'source'> | undefined {
const block = descriptor.template
if (!block) {
return
}
const resolvedScript = getResolvedScript(descriptor, ssr)
const hasScoped = descriptor.styles.some((s) => s.scoped)
const { id, filename, cssVars } = descriptor
let transformAssetUrls = options.template?.transformAssetUrls
// compiler-sfc should export `AssetURLOptions`
let assetUrlOptions //: AssetURLOptions | undefined
if (options.devServer) {
// during dev, inject vite base so that compiler-sfc can transform
// relative paths directly to absolute paths without incurring an extra import
// request
if (filename.startsWith(options.root)) {
assetUrlOptions = {
base:
options.devServer.config.base +
slash(path.relative(options.root, path.dirname(filename)))
}
}
} else {
// build: force all asset urls into import requests so that they go through
// the assets plugin for asset registration
assetUrlOptions = {
includeAbsolute: true
}
}
if (transformAssetUrls && typeof transformAssetUrls === 'object') {
// presence of array fields means this is raw tags config
if (Object.values(transformAssetUrls).some((val) => Array.isArray(val))) {
transformAssetUrls = {
...assetUrlOptions,
tags: transformAssetUrls as any
}
} else {
transformAssetUrls = { ...transformAssetUrls, ...assetUrlOptions }
}
} else {
transformAssetUrls = assetUrlOptions
}
let preprocessOptions = block.lang && options.template?.preprocessOptions
if (block.lang === 'pug') {
preprocessOptions = {
doctype: 'html',
...preprocessOptions
}
}
// if using TS, support TS syntax in template expressions
const expressionPlugins: CompilerOptions['expressionPlugins'] =
options.template?.compilerOptions?.expressionPlugins || []
const lang = descriptor.scriptSetup?.lang || descriptor.script?.lang
if (lang && /tsx?$/.test(lang) && !expressionPlugins.includes('typescript')) {
expressionPlugins.push('typescript')
}
return {
...options.template,
id,
filename,
scoped: hasScoped,
slotted: descriptor.slotted,
isProd: options.isProduction,
inMap: block.src ? undefined : block.map,
ssr,
ssrCssVars: cssVars,
transformAssetUrls,
preprocessLang: block.lang,
preprocessOptions,
compilerOptions: {
...options.template?.compilerOptions,
scopeId: hasScoped ? `data-v-${id}` : undefined,
bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined,
expressionPlugins,
sourceMap: options.sourceMap
}
}
}