This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathtemplate.ts
94 lines (88 loc) · 2.56 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
import {
compileTemplate,
SFCDescriptor,
SFCTemplateCompileOptions,
} from '@vue/compiler-sfc'
import { TransformPluginContext } from 'rollup'
import { Options } from '.'
import { getResolvedScript } from './script'
import { getDescriptor } from './utils/descriptorCache'
import { createRollupError } from './utils/error'
import { TemplateBlockQuery } from './utils/query'
import { normalizeSourceMap } from './utils/sourceMap'
export function transformTemplate(
code: string,
request: string,
options: Options,
query: TemplateBlockQuery,
pluginContext: TransformPluginContext
) {
const descriptor = getDescriptor(query.filename)
const result = compileTemplate({
...getTemplateCompilerOptions(options, descriptor, query.id),
id: query.id,
source: code,
filename: query.filename,
})
if (result.errors.length) {
result.errors.forEach((error) =>
pluginContext.error(
typeof error === 'string'
? { id: query.filename, message: error }
: createRollupError(query.filename, error)
)
)
return null
}
if (result.tips.length) {
result.tips.forEach((tip) =>
pluginContext.warn({
id: query.filename,
message: tip,
})
)
}
return {
code: result.code,
map: normalizeSourceMap(result.map!, request),
}
}
export function getTemplateCompilerOptions(
options: Options,
descriptor: SFCDescriptor,
scopeId: string
): Omit<SFCTemplateCompileOptions, 'source'> | undefined {
const block = descriptor.template
if (!block) {
return
}
const isProd =
process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
const isServer = options.target === 'node'
const hasScoped = descriptor.styles.some((s) => s.scoped)
const preprocessLang = block.lang
const preprocessOptions =
preprocessLang &&
options.templatePreprocessOptions &&
options.templatePreprocessOptions[preprocessLang]
const resolvedScript = getResolvedScript(descriptor, isServer)
return {
id: scopeId,
scoped: hasScoped,
isProd,
filename: descriptor.filename,
inMap: block.src ? undefined : block.map,
preprocessLang,
preprocessOptions,
preprocessCustomRequire: options.preprocessCustomRequire,
compiler: options.compiler,
ssr: isServer,
ssrCssVars: descriptor.cssVars,
compilerOptions: {
...options.compilerOptions,
scopeId: hasScoped ? `data-v-${scopeId}` : undefined,
bindingMetadata: resolvedScript ? resolvedScript.bindings : undefined,
},
transformAssetUrls: options.transformAssetUrls,
}
}