From d7aab859a35dc6daa4eb531452ec59722c52b060 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 20 Sep 2019 00:12:37 -0400 Subject: [PATCH] feat: expose compiler APIs --- .../compiler-core/__tests__/codegen.spec.ts | 2 +- packages/compiler-core/src/codegen.ts | 3 +- packages/compiler-core/src/index.ts | 10 +++- packages/compiler-dom/src/index.ts | 48 +++++++++++++++++-- .../src/componentPublicInstanceProxy.ts | 8 ++++ packages/vue/package.json | 1 + packages/vue/src/index.ts | 8 +++- 7 files changed, 70 insertions(+), 10 deletions(-) diff --git a/packages/compiler-core/__tests__/codegen.spec.ts b/packages/compiler-core/__tests__/codegen.spec.ts index 03a00b67..40908535 100644 --- a/packages/compiler-core/__tests__/codegen.spec.ts +++ b/packages/compiler-core/__tests__/codegen.spec.ts @@ -4,7 +4,7 @@ import { SourceMapConsumer, RawSourceMap } from 'source-map' describe('compiler: codegen', () => { test('basic source map support', async () => { const ast = parse(`hello {{ world }}`) - const { code, map } = generate(ast, { module: false }) + const { code, map } = generate(ast) expect(code).toBe(`["hello ", world]`) const consumer = await new SourceMapConsumer(map as RawSourceMap) diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index 06753c36..52b348b5 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -15,6 +15,7 @@ import { advancePositionWithMutation } from './utils' export interface CodegenOptions { // Assume ES module environment. If true, will generate import statements for // runtime helpers; otherwise will grab the helpers from global `Vue`. + // default: false module?: boolean // Filename for source map generation. filename?: string @@ -68,7 +69,7 @@ function createCodegenContext( options: CodegenOptions ): CodegenContext { const context: CodegenContext = { - module: true, + module: false, filename: `template.vue.html`, ...options, source: ast.loc.source, diff --git a/packages/compiler-core/src/index.ts b/packages/compiler-core/src/index.ts index 9fcb1534..250bebcf 100644 --- a/packages/compiler-core/src/index.ts +++ b/packages/compiler-core/src/index.ts @@ -1,5 +1,11 @@ export { parse, ParserOptions, TextModes } from './parse' -export { transform, TransformOptions, Transform } from './transform' +export { + transform, + createDirectiveTransform, + TransformOptions, + Transform +} from './transform' export { generate, CodegenOptions, CodegenResult } from './codegen' -export { ErrorCodes } from './errors' +export { ErrorCodes, CompilerError, createCompilerError } from './errors' + export * from './ast' diff --git a/packages/compiler-dom/src/index.ts b/packages/compiler-dom/src/index.ts index 5c052d2f..47c58e09 100644 --- a/packages/compiler-dom/src/index.ts +++ b/packages/compiler-dom/src/index.ts @@ -1,8 +1,46 @@ -// TODO -export * from '@vue/compiler-core' +import { + parse, + transform, + generate, + CompilerError, + Transform, + CodegenResult +} from '@vue/compiler-core' import { parserOptionsMinimal } from './parserOptionsMinimal' import { parserOptionsStandard } from './parserOptionsStandard' -export const parserOptions = __BROWSER__ - ? parserOptionsMinimal - : parserOptionsStandard +const parserOptions = __BROWSER__ ? parserOptionsMinimal : parserOptionsStandard + +export interface CompilerOptions { + module?: boolean + onError?(err: CompilerError): void + transforms?: Transform[] +} + +export function compile( + template: string, + options: CompilerOptions = {} +): CodegenResult { + const { + module = false, + onError = (err: CompilerError) => { + throw err + }, + transforms = [] + } = options + const ast = parse(template, { + ...parserOptions, + onError + }) + transform(ast, { + transforms: [ + // TODO include core transforms + // TODO include DOM transforms + ...transforms // user transforms + ], + onError + }) + return generate(ast, { module }) +} + +export * from '@vue/compiler-core' diff --git a/packages/runtime-core/src/componentPublicInstanceProxy.ts b/packages/runtime-core/src/componentPublicInstanceProxy.ts index 409cf496..6494028e 100644 --- a/packages/runtime-core/src/componentPublicInstanceProxy.ts +++ b/packages/runtime-core/src/componentPublicInstanceProxy.ts @@ -77,6 +77,14 @@ export const PublicInstanceProxyHandlers = { } } }, + has(target: ComponentInternalInstance, key: string): boolean { + const { renderContext, data, props } = target + return ( + (data !== EMPTY_OBJ && hasOwn(data, key)) || + hasOwn(renderContext, key) || + hasOwn(props, key) + ) + }, set(target: ComponentInternalInstance, key: string, value: any): boolean { const { data, renderContext } = target if (data !== EMPTY_OBJ && hasOwn(data, key)) { diff --git a/packages/vue/package.json b/packages/vue/package.json index c53ab2d1..9b6eac6d 100644 --- a/packages/vue/package.json +++ b/packages/vue/package.json @@ -28,6 +28,7 @@ }, "homepage": "https://github.com/vuejs/vue/tree/dev/packages/vue#readme", "dependencies": { + "@vue/compiler-dom": "3.0.0-alpha.1", "@vue/runtime-dom": "3.0.0-alpha.1" } } diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 11fdece3..5db370eb 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -4,7 +4,13 @@ // TODO hook up the runtime to compile templates on the fly -export * from '@vue/compiler-dom' +import { compile as baseCompile, CompilerOptions } from '@vue/compiler-dom' + +export function compile(template: string, options?: CompilerOptions): Function { + const { code } = baseCompile(template, options) + return new Function(`with(this){return ${code}}`) +} + export * from '@vue/runtime-dom' if (__BROWSER__ && __DEV__) {