1
1
/* @flow */
2
2
3
+ import { extend } from 'shared/util'
3
4
import { parse } from './parser/index'
4
5
import { optimize } from './optimizer'
5
6
import { generate } from './codegen/index'
6
7
import { detectErrors } from './error-detector'
7
- import { extend , noop } from 'shared/util'
8
- import { warn , tip } from 'core/util/debug'
8
+ import { createCompileToFunctionFn } from './to-function'
9
9
10
10
function baseCompile (
11
11
template : string ,
@@ -21,20 +21,7 @@ function baseCompile (
21
21
}
22
22
}
23
23
24
- function makeFunction ( code , errors ) {
25
- try {
26
- return new Function ( code )
27
- } catch ( err ) {
28
- errors . push ( { err, code } )
29
- return noop
30
- }
31
- }
32
-
33
24
export function createCompiler ( baseOptions : CompilerOptions ) {
34
- const functionCompileCache : {
35
- [ key : string ] : CompiledFunctionResult ;
36
- } = Object . create ( null )
37
-
38
25
function compile (
39
26
template : string ,
40
27
options ?: CompilerOptions
@@ -75,85 +62,8 @@ export function createCompiler (baseOptions: CompilerOptions) {
75
62
return compiled
76
63
}
77
64
78
- function compileToFunctions (
79
- template : string ,
80
- options ?: CompilerOptions ,
81
- vm ?: Component
82
- ) : CompiledFunctionResult {
83
- options = options || { }
84
-
85
- /* istanbul ignore if */
86
- if ( process . env . NODE_ENV !== 'production' ) {
87
- // detect possible CSP restriction
88
- try {
89
- new Function ( 'return 1' )
90
- } catch ( e ) {
91
- if ( e . toString ( ) . match ( / u n s a f e - e v a l | C S P / ) ) {
92
- warn (
93
- 'It seems you are using the standalone build of Vue.js in an ' +
94
- 'environment with Content Security Policy that prohibits unsafe-eval. ' +
95
- 'The template compiler cannot work in this environment. Consider ' +
96
- 'relaxing the policy to allow unsafe-eval or pre-compiling your ' +
97
- 'templates into render functions.'
98
- )
99
- }
100
- }
101
- }
102
-
103
- // check cache
104
- const key = options . delimiters
105
- ? String ( options . delimiters ) + template
106
- : template
107
- if ( functionCompileCache [ key ] ) {
108
- return functionCompileCache [ key ]
109
- }
110
-
111
- // compile
112
- const compiled = compile ( template , options )
113
-
114
- // check compilation errors/tips
115
- if ( process . env . NODE_ENV !== 'production' ) {
116
- if ( compiled . errors && compiled . errors . length ) {
117
- warn (
118
- `Error compiling template:\n\n${ template } \n\n` +
119
- compiled . errors . map ( e => `- ${ e } ` ) . join ( '\n' ) + '\n' ,
120
- vm
121
- )
122
- }
123
- if ( compiled . tips && compiled . tips . length ) {
124
- compiled . tips . forEach ( msg => tip ( msg , vm ) )
125
- }
126
- }
127
-
128
- // turn code into functions
129
- const res = { }
130
- const fnGenErrors = [ ]
131
- res . render = makeFunction ( compiled . render , fnGenErrors )
132
- const l = compiled . staticRenderFns . length
133
- res . staticRenderFns = new Array ( l )
134
- for ( let i = 0 ; i < l ; i ++ ) {
135
- res . staticRenderFns [ i ] = makeFunction ( compiled . staticRenderFns [ i ] , fnGenErrors )
136
- }
137
-
138
- // check function generation errors.
139
- // this should only happen if there is a bug in the compiler itself.
140
- // mostly for codegen development use
141
- /* istanbul ignore if */
142
- if ( process . env . NODE_ENV !== 'production' ) {
143
- if ( ( ! compiled . errors || ! compiled . errors . length ) && fnGenErrors . length ) {
144
- warn (
145
- `Failed to generate render function:\n\n` +
146
- fnGenErrors . map ( ( { err, code } ) => `${ err . toString ( ) } in\n\n${ code } \n` ) . join ( '\n' ) ,
147
- vm
148
- )
149
- }
150
- }
151
-
152
- return ( functionCompileCache [ key ] = res )
153
- }
154
-
155
65
return {
156
66
compile,
157
- compileToFunctions
67
+ compileToFunctions : createCompileToFunctionFn ( compile )
158
68
}
159
69
}
0 commit comments