|
|
@@ -1,13 +1,14 @@
|
|
|
/* @flow */
|
|
|
|
|
|
-import { extend } from 'shared/util'
|
|
|
import { parse } from './parser/index'
|
|
|
import { optimize } from './optimizer'
|
|
|
import { generate } from './codegen/index'
|
|
|
-import { detectErrors } from './error-detector'
|
|
|
-import { createCompileToFunctionFn } from './to-function'
|
|
|
+import { createCompilerCreator } from './create-compiler'
|
|
|
|
|
|
-function baseCompile (
|
|
|
+// `createCompilerCreator` allows creating compilers that use alternative
|
|
|
+// parser/optimizer/codegen, e.g the SSR optimizing compiler.
|
|
|
+// Here we just export a default compiler using the default parts.
|
|
|
+export const createCompiler = createCompilerCreator(function baseCompile (
|
|
|
template: string,
|
|
|
options: CompilerOptions
|
|
|
): CompiledResult {
|
|
|
@@ -19,51 +20,4 @@ function baseCompile (
|
|
|
render: code.render,
|
|
|
staticRenderFns: code.staticRenderFns
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-export function createCompiler (baseOptions: CompilerOptions) {
|
|
|
- function compile (
|
|
|
- template: string,
|
|
|
- options?: CompilerOptions
|
|
|
- ): CompiledResult {
|
|
|
- const finalOptions = Object.create(baseOptions)
|
|
|
- const errors = []
|
|
|
- const tips = []
|
|
|
- finalOptions.warn = (msg, tip) => {
|
|
|
- (tip ? tips : errors).push(msg)
|
|
|
- }
|
|
|
-
|
|
|
- if (options) {
|
|
|
- // merge custom modules
|
|
|
- if (options.modules) {
|
|
|
- finalOptions.modules = (baseOptions.modules || []).concat(options.modules)
|
|
|
- }
|
|
|
- // merge custom directives
|
|
|
- if (options.directives) {
|
|
|
- finalOptions.directives = extend(
|
|
|
- Object.create(baseOptions.directives),
|
|
|
- options.directives
|
|
|
- )
|
|
|
- }
|
|
|
- // copy other options
|
|
|
- for (const key in options) {
|
|
|
- if (key !== 'modules' && key !== 'directives') {
|
|
|
- finalOptions[key] = options[key]
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- const compiled = baseCompile(template, finalOptions)
|
|
|
- if (process.env.NODE_ENV !== 'production') {
|
|
|
- errors.push.apply(errors, detectErrors(compiled.ast))
|
|
|
- }
|
|
|
- compiled.errors = errors
|
|
|
- compiled.tips = tips
|
|
|
- return compiled
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- compile,
|
|
|
- compileToFunctions: createCompileToFunctionFn(compile)
|
|
|
- }
|
|
|
-}
|
|
|
+})
|