Skip to content

Commit ed44d6f

Browse files
kazuponhaoqunjiang
authored andcommitted
feat: support AST for template compile (#68)
* feat: support AST for template compile * fix: change ast type definition
1 parent 3dda72d commit ed44d6f

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ interface TemplateCompileOptions {
9898
}
9999

100100
interface TemplateCompileResult {
101+
ast: Object | undefined
101102
code: string
102103
source: string
103104
tips: string[]

lib/compileTemplate.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface TemplateCompileOptions {
2828
}
2929

3030
export interface TemplateCompileResult {
31+
ast: Object | undefined
3132
code: string
3233
source: string
3334
tips: (string | ErrorWithRange)[]
@@ -47,6 +48,7 @@ export function compileTemplate(
4748
)
4849
} else if (preprocessLang) {
4950
return {
51+
ast: {},
5052
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
5153
source: options.source,
5254
tips: [
@@ -124,13 +126,14 @@ function actuallyCompile(
124126
})
125127
}
126128

127-
const { render, staticRenderFns, tips, errors } = compile(
129+
const { ast, render, staticRenderFns, tips, errors } = compile(
128130
source,
129131
finalCompilerOptions
130132
)
131133

132134
if (errors && errors.length) {
133135
return {
136+
ast,
134137
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
135138
source,
136139
tips,
@@ -181,6 +184,7 @@ function actuallyCompile(
181184
}
182185

183186
return {
187+
ast,
184188
code,
185189
source,
186190
tips,

lib/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface VueTemplateCompiler {
3333
export interface VueTemplateCompilerOptions {
3434
modules?: Object[]
3535
outputSourceRange?: boolean
36+
whitespace?: 'preserve' | 'condense'
37+
directives?: { [key: string]: Function }
3638
}
3739

3840
export interface VueTemplateCompilerParseOptions {
@@ -46,7 +48,7 @@ export interface ErrorWithRange {
4648
}
4749

4850
export interface VueTemplateCompilerResults {
49-
ast: Object | void
51+
ast: Object | undefined
5052
render: string
5153
staticRenderFns: string[]
5254
errors: (string | ErrorWithRange)[]

test/compileTemplate.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test('should work', () => {
3434
expect(result.code).toMatch(`render._withStripped = true`)
3535
// should prefix bindings
3636
expect(result.code).toMatch(`_vm.render`)
37+
expect(result.ast).not.toBeUndefined()
3738
})
3839

3940
test('preprocess pug', () => {

0 commit comments

Comments
 (0)