1 |
|
2 |
|
3 | import { extend } from 'shared/util'
|
4 | import { detectErrors } from './error-detector'
|
5 | import { createCompileToFunctionFn } from './to-function'
|
6 |
|
7 | export function createCompilerCreator (baseCompile: Function): Function {
|
8 | return function createCompiler (baseOptions: CompilerOptions) {
|
9 | function compile (
|
10 | template: string,
|
11 | options?: CompilerOptions
|
12 | ): CompiledResult {
|
13 | const finalOptions = Object.create(baseOptions)
|
14 | const errors = []
|
15 | const tips = []
|
16 |
|
17 | let warn = (msg, range, tip) => {
|
18 | (tip ? tips : errors).push(msg)
|
19 | }
|
20 |
|
21 | if (options) {
|
22 | if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
|
23 |
|
24 | const leadingSpaceLength = template.match(/^\s*/)[0].length
|
25 |
|
26 | warn = (msg, range, tip) => {
|
27 | const data: WarningMessage = { msg }
|
28 | if (range) {
|
29 | if (range.start != null) {
|
30 | data.start = range.start + leadingSpaceLength
|
31 | }
|
32 | if (range.end != null) {
|
33 | data.end = range.end + leadingSpaceLength
|
34 | }
|
35 | }
|
36 | (tip ? tips : errors).push(data)
|
37 | }
|
38 | }
|
39 |
|
40 | if (options.modules) {
|
41 | finalOptions.modules =
|
42 | (baseOptions.modules || []).concat(options.modules)
|
43 | }
|
44 |
|
45 | if (options.directives) {
|
46 | finalOptions.directives = extend(
|
47 | Object.create(baseOptions.directives || null),
|
48 | options.directives
|
49 | )
|
50 | }
|
51 |
|
52 | for (const key in options) {
|
53 | if (key !== 'modules' && key !== 'directives') {
|
54 | finalOptions[key] = options[key]
|
55 | }
|
56 | }
|
57 | }
|
58 |
|
59 | finalOptions.warn = warn
|
60 |
|
61 | const compiled = baseCompile(template.trim(), finalOptions)
|
62 | if (process.env.NODE_ENV !== 'production') {
|
63 | detectErrors(compiled.ast, warn)
|
64 | }
|
65 | compiled.errors = errors
|
66 | compiled.tips = tips
|
67 | return compiled
|
68 | }
|
69 |
|
70 | return {
|
71 | compile,
|
72 | compileToFunctions: createCompileToFunctionFn(compile)
|
73 | }
|
74 | }
|
75 | }
|