Skip to content

Commit

Permalink
feat: support AST for template compile (#68)
Browse files Browse the repository at this point in the history
* feat: support AST for template compile

* fix: change ast type definition
  • Loading branch information
kazupon authored and haoqunjiang committed Dec 8, 2019
1 parent 3dda72d commit ed44d6f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ interface TemplateCompileOptions {
}

interface TemplateCompileResult {
ast: Object | undefined
code: string
source: string
tips: string[]
Expand Down
6 changes: 5 additions & 1 deletion lib/compileTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface TemplateCompileOptions {
}

export interface TemplateCompileResult {
ast: Object | undefined
code: string
source: string
tips: (string | ErrorWithRange)[]
Expand All @@ -47,6 +48,7 @@ export function compileTemplate(
)
} else if (preprocessLang) {
return {
ast: {},
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
source: options.source,
tips: [
Expand Down Expand Up @@ -124,13 +126,14 @@ function actuallyCompile(
})
}

const { render, staticRenderFns, tips, errors } = compile(
const { ast, render, staticRenderFns, tips, errors } = compile(
source,
finalCompilerOptions
)

if (errors && errors.length) {
return {
ast,
code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
source,
tips,
Expand Down Expand Up @@ -181,6 +184,7 @@ function actuallyCompile(
}

return {
ast,
code,
source,
tips,
Expand Down
4 changes: 3 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface VueTemplateCompiler {
export interface VueTemplateCompilerOptions {
modules?: Object[]
outputSourceRange?: boolean
whitespace?: 'preserve' | 'condense'
directives?: { [key: string]: Function }
}

export interface VueTemplateCompilerParseOptions {
Expand All @@ -46,7 +48,7 @@ export interface ErrorWithRange {
}

export interface VueTemplateCompilerResults {
ast: Object | void
ast: Object | undefined
render: string
staticRenderFns: string[]
errors: (string | ErrorWithRange)[]
Expand Down
1 change: 1 addition & 0 deletions test/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test('should work', () => {
expect(result.code).toMatch(`render._withStripped = true`)
// should prefix bindings
expect(result.code).toMatch(`_vm.render`)
expect(result.ast).not.toBeUndefined()
})

test('preprocess pug', () => {
Expand Down

0 comments on commit ed44d6f

Please sign in to comment.