瀏覽代碼

feat: expose compiler APIs

Evan You 6 年之前
父節點
當前提交
d7aab859a3

+ 1 - 1
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)

+ 2 - 1
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,

+ 8 - 2
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'

+ 43 - 5
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'

+ 8 - 0
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)) {

+ 1 - 0
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"
   }
 }

+ 7 - 1
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__) {