Skip to content

feat: support AST for template compile #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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