-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
template.ts
170 lines (155 loc) · 4.77 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
// @ts-ignore
import hash from 'hash-sum'
import type { SFCDescriptor, SFCTemplateCompileOptions } from 'vue/compiler-sfc'
import type { PluginContext, TransformPluginContext } from 'rollup'
import { getResolvedScript } from './script'
import { createRollupError } from './utils/error'
import type { ResolvedOptions } from '.'
import path from 'node:path'
import slash from 'slash'
import { HMR_RUNTIME_ID } from './utils/hmrRuntime'
export async function transformTemplateAsModule(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: TransformPluginContext,
ssr: boolean
): Promise<string> {
let returnCode = compile(code, descriptor, options, pluginContext, ssr)
if (
options.devServer &&
options.devServer.config.server.hmr !== false &&
!ssr &&
!options.isProduction
) {
returnCode += `\nimport __VUE_HMR_RUNTIME__ from "${HMR_RUNTIME_ID}"`
returnCode += `\nimport.meta.hot.accept((updated) => {
__VUE_HMR_RUNTIME__.rerender(${JSON.stringify(descriptor.id)}, updated)
})`
}
return returnCode + `\nexport { render, staticRenderFns }`
}
/**
* transform the template directly in the main SFC module
*/
export function transformTemplateInMain(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean
): string {
return compile(code, descriptor, options, pluginContext, ssr)
.replace(/var (render|staticRenderFns) =/g, 'var _sfc_$1 =')
.replace(/(render._withStripped)/, '_sfc_$1')
}
export function compile(
code: string,
descriptor: SFCDescriptor,
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean
): string {
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: typeof tip === 'string' ? tip : tip.msg
})
)
}
return transformRequireToImport(result.code)
}
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 } = descriptor
let preprocessOptions = block.lang && options.template?.preprocessOptions
if (block.lang === 'pug') {
preprocessOptions = {
doctype: 'html',
...preprocessOptions
}
}
const transformAssetUrls = options.template?.transformAssetUrls ?? true
let assetUrlOptions
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.server?.origin ?? '') +
options.devServer.config.base +
slash(path.relative(options.root, path.dirname(filename)))
}
}
} else if (transformAssetUrls !== false) {
// build: force all asset urls into import requests so that they go through
// the assets plugin for asset registration
assetUrlOptions = {
includeAbsolute: true
}
}
return {
transformAssetUrls,
...options.template,
filename,
isProduction: options.isProduction,
isFunctional: !!block.attrs.functional,
optimizeSSR: ssr,
transformAssetUrlsOptions: {
...assetUrlOptions,
...options.template?.transformAssetUrlsOptions
},
preprocessLang: block.lang,
preprocessOptions,
bindings: resolvedScript ? resolvedScript.bindings : undefined,
prettify: false,
compilerOptions: {
whitespace: 'condense',
outputSourceRange: true,
...options.template?.compilerOptions,
scopeId: hasScoped ? `data-v-${id}` : undefined
}
}
}
function transformRequireToImport(code: string): string {
const imports: Record<string, string> = {}
let strImports = ''
code = code.replace(
/require\(("(?:[^"\\]|\\.)+"|'(?:[^'\\]|\\.)+')\)/g,
(_, name): any => {
if (!(name in imports)) {
// #81 compat unicode assets name
imports[name] = `__$_require_${hash(name)}__`
strImports += `import ${imports[name]} from ${name}\n`
}
return imports[name]
}
)
return strImports + code
}