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)
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
options: CodegenOptions
): CodegenContext {
const context: CodegenContext = {
- module: true,
+ module: false,
filename: `template.vue.html`,
...options,
source: ast.loc.source,
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'
-// 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'
}
}
},
+ 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)) {
},
"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"
}
}
// 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__) {