-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathvue2TemplateCompiler.ts
82 lines (72 loc) · 2.17 KB
/
vue2TemplateCompiler.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
import * as CompilerDom from '@vue/compiler-dom';
import * as CompilerCore from '@vue/compiler-core';
export function compile(
template: string,
options: CompilerDom.CompilerOptions = {}
): CompilerDom.CodegenResult {
const onError = options.onError;
options.onError = (error) => {
if (error.code === CompilerCore.ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT)
return; // :key binding allowed in v-for template child in vue 2
if (onError)
onError(error);
else
throw error;
};
return baseCompile(
template,
Object.assign({}, CompilerDom.parserOptions, options, {
nodeTransforms: [
...CompilerDom.DOMNodeTransforms,
...(options.nodeTransforms || [])
],
directiveTransforms: Object.assign(
{},
CompilerDom.DOMDirectiveTransforms,
options.directiveTransforms || {}
),
})
);
}
export function baseCompile(
template: string,
options: CompilerCore.CompilerOptions = {}
): CompilerCore.CodegenResult {
const onError = options.onError || ((error) => { throw error; });
const isModuleMode = options.mode === 'module';
const prefixIdentifiers = options.prefixIdentifiers === true || isModuleMode;
if (!prefixIdentifiers && options.cacheHandlers) {
onError(CompilerCore.createCompilerError(CompilerCore.ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED));
}
if (options.scopeId && !isModuleMode) {
onError(CompilerCore.createCompilerError(CompilerCore.ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED));
}
const ast = CompilerCore.baseParse(template, options);
const [nodeTransforms, directiveTransforms] = CompilerCore.getBaseTransformPreset(prefixIdentifiers);
// v-for > v-if in vue 2
const transformIf = nodeTransforms[1];
const transformFor = nodeTransforms[3];
nodeTransforms[1] = transformFor;
nodeTransforms[3] = transformIf;
CompilerCore.transform(
ast,
Object.assign({}, options, {
prefixIdentifiers,
nodeTransforms: [
...nodeTransforms,
...(options.nodeTransforms || []) // user transforms
],
directiveTransforms: Object.assign(
{},
directiveTransforms,
options.directiveTransforms || {} // user transforms
)
})
);
return CompilerCore.generate(
ast,
Object.assign({}, options, {
prefixIdentifiers
})
);
}