feat: expose compiler APIs
This commit is contained in:
parent
3b5ef483a9
commit
d7aab859a3
@ -4,7 +4,7 @@ import { SourceMapConsumer, RawSourceMap } from 'source-map'
|
|||||||
describe('compiler: codegen', () => {
|
describe('compiler: codegen', () => {
|
||||||
test('basic source map support', async () => {
|
test('basic source map support', async () => {
|
||||||
const ast = parse(`hello {{ world }}`)
|
const ast = parse(`hello {{ world }}`)
|
||||||
const { code, map } = generate(ast, { module: false })
|
const { code, map } = generate(ast)
|
||||||
expect(code).toBe(`["hello ", world]`)
|
expect(code).toBe(`["hello ", world]`)
|
||||||
|
|
||||||
const consumer = await new SourceMapConsumer(map as RawSourceMap)
|
const consumer = await new SourceMapConsumer(map as RawSourceMap)
|
||||||
|
@ -15,6 +15,7 @@ import { advancePositionWithMutation } from './utils'
|
|||||||
export interface CodegenOptions {
|
export interface CodegenOptions {
|
||||||
// Assume ES module environment. If true, will generate import statements for
|
// Assume ES module environment. If true, will generate import statements for
|
||||||
// runtime helpers; otherwise will grab the helpers from global `Vue`.
|
// runtime helpers; otherwise will grab the helpers from global `Vue`.
|
||||||
|
// default: false
|
||||||
module?: boolean
|
module?: boolean
|
||||||
// Filename for source map generation.
|
// Filename for source map generation.
|
||||||
filename?: string
|
filename?: string
|
||||||
@ -68,7 +69,7 @@ function createCodegenContext(
|
|||||||
options: CodegenOptions
|
options: CodegenOptions
|
||||||
): CodegenContext {
|
): CodegenContext {
|
||||||
const context: CodegenContext = {
|
const context: CodegenContext = {
|
||||||
module: true,
|
module: false,
|
||||||
filename: `template.vue.html`,
|
filename: `template.vue.html`,
|
||||||
...options,
|
...options,
|
||||||
source: ast.loc.source,
|
source: ast.loc.source,
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
export { parse, ParserOptions, TextModes } from './parse'
|
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 { generate, CodegenOptions, CodegenResult } from './codegen'
|
||||||
export { ErrorCodes } from './errors'
|
export { ErrorCodes, CompilerError, createCompilerError } from './errors'
|
||||||
|
|
||||||
export * from './ast'
|
export * from './ast'
|
||||||
|
@ -1,8 +1,46 @@
|
|||||||
// TODO
|
import {
|
||||||
export * from '@vue/compiler-core'
|
parse,
|
||||||
|
transform,
|
||||||
|
generate,
|
||||||
|
CompilerError,
|
||||||
|
Transform,
|
||||||
|
CodegenResult
|
||||||
|
} from '@vue/compiler-core'
|
||||||
import { parserOptionsMinimal } from './parserOptionsMinimal'
|
import { parserOptionsMinimal } from './parserOptionsMinimal'
|
||||||
import { parserOptionsStandard } from './parserOptionsStandard'
|
import { parserOptionsStandard } from './parserOptionsStandard'
|
||||||
|
|
||||||
export const parserOptions = __BROWSER__
|
const parserOptions = __BROWSER__ ? parserOptionsMinimal : parserOptionsStandard
|
||||||
? 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'
|
||||||
|
@ -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 {
|
set(target: ComponentInternalInstance, key: string, value: any): boolean {
|
||||||
const { data, renderContext } = target
|
const { data, renderContext } = target
|
||||||
if (data !== EMPTY_OBJ && hasOwn(data, key)) {
|
if (data !== EMPTY_OBJ && hasOwn(data, key)) {
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/vue#readme",
|
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/vue#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@vue/compiler-dom": "3.0.0-alpha.1",
|
||||||
"@vue/runtime-dom": "3.0.0-alpha.1"
|
"@vue/runtime-dom": "3.0.0-alpha.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,13 @@
|
|||||||
|
|
||||||
// TODO hook up the runtime to compile templates on the fly
|
// 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'
|
export * from '@vue/runtime-dom'
|
||||||
|
|
||||||
if (__BROWSER__ && __DEV__) {
|
if (__BROWSER__ && __DEV__) {
|
||||||
|
Loading…
Reference in New Issue
Block a user