From f72bb41b099d3c285fc15312069043f63ebb78fa Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 31 Jul 2019 12:09:06 +0900 Subject: [PATCH 1/2] feat: support AST for template compile --- README.md | 1 + lib/compileTemplate.ts | 6 +++++- lib/types.ts | 2 ++ test/compileTemplate.spec.ts | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 471abd8..a332759 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ interface TemplateCompileOptions { } interface TemplateCompileResult { + ast: Object | void code: string source: string tips: string[] diff --git a/lib/compileTemplate.ts b/lib/compileTemplate.ts index 3ba5faa..858394c 100644 --- a/lib/compileTemplate.ts +++ b/lib/compileTemplate.ts @@ -28,6 +28,7 @@ export interface TemplateCompileOptions { } export interface TemplateCompileResult { + ast: Object | void code: string source: string tips: (string | ErrorWithRange)[] @@ -47,6 +48,7 @@ export function compileTemplate( ) } else if (preprocessLang) { return { + ast: {}, code: `var render = function () {}\n` + `var staticRenderFns = []\n`, source: options.source, tips: [ @@ -127,13 +129,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, @@ -178,6 +181,7 @@ function actuallyCompile( } return { + ast, code, source, tips, diff --git a/lib/types.ts b/lib/types.ts index f2542bb..0e26632 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -33,6 +33,8 @@ export interface VueTemplateCompiler { export interface VueTemplateCompilerOptions { modules?: Object[] outputSourceRange?: boolean + whitespace?: 'preserve' | 'condense' + directives?: { [key: string]: Function } } export interface VueTemplateCompilerParseOptions { diff --git a/test/compileTemplate.spec.ts b/test/compileTemplate.spec.ts index de29064..762a3ff 100644 --- a/test/compileTemplate.spec.ts +++ b/test/compileTemplate.spec.ts @@ -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', () => { From ade7d351a03da16225968178ccd4517f45a2c2ef Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 1 Aug 2019 02:00:24 +0900 Subject: [PATCH 2/2] fix: change ast type definition --- README.md | 2 +- lib/compileTemplate.ts | 2 +- lib/types.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a332759..6ef98b4 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ interface TemplateCompileOptions { } interface TemplateCompileResult { - ast: Object | void + ast: Object | undefined code: string source: string tips: string[] diff --git a/lib/compileTemplate.ts b/lib/compileTemplate.ts index 858394c..2074c80 100644 --- a/lib/compileTemplate.ts +++ b/lib/compileTemplate.ts @@ -28,7 +28,7 @@ export interface TemplateCompileOptions { } export interface TemplateCompileResult { - ast: Object | void + ast: Object | undefined code: string source: string tips: (string | ErrorWithRange)[] diff --git a/lib/types.ts b/lib/types.ts index 0e26632..c534544 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -48,7 +48,7 @@ export interface ErrorWithRange { } export interface VueTemplateCompilerResults { - ast: Object | void + ast: Object | undefined render: string staticRenderFns: string[] errors: (string | ErrorWithRange)[]