|
1 | | -# createCompileToFunctionFn |
| 1 | +# 生成render函数 |
2 | 2 |
|
3 | | -```js |
| 3 | +```js {12-19,42,47-52} |
4 | 4 | /* @flow */ |
5 | 5 |
|
6 | 6 | import { noop, extend } from 'shared/util' |
@@ -33,85 +33,28 @@ export function createCompileToFunctionFn (compile: Function): Function { |
33 | 33 | const warn = options.warn || baseWarn |
34 | 34 | delete options.warn |
35 | 35 |
|
36 | | - /* istanbul ignore if */ |
37 | | - if (process.env.NODE_ENV !== 'production') { |
38 | | - // detect possible CSP restriction |
39 | | - try { |
40 | | - new Function('return 1') |
41 | | - } catch (e) { |
42 | | - if (e.toString().match(/unsafe-eval|CSP/)) { |
43 | | - warn( |
44 | | - 'It seems you are using the standalone build of Vue.js in an ' + |
45 | | - 'environment with Content Security Policy that prohibits unsafe-eval. ' + |
46 | | - 'The template compiler cannot work in this environment. Consider ' + |
47 | | - 'relaxing the policy to allow unsafe-eval or pre-compiling your ' + |
48 | | - 'templates into render functions.' |
49 | | - ) |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - // check cache |
| 36 | + // 如果有编译直接用之前的 |
55 | 37 | const key = options.delimiters |
56 | 38 | ? String(options.delimiters) + template |
57 | 39 | : template |
58 | 40 | if (cache[key]) { |
59 | 41 | return cache[key] |
60 | 42 | } |
61 | 43 |
|
62 | | - // compile |
| 44 | + // 调用编译 |
63 | 45 | const compiled = compile(template, options) |
64 | 46 |
|
65 | | - // check compilation errors/tips |
66 | | - if (process.env.NODE_ENV !== 'production') { |
67 | | - if (compiled.errors && compiled.errors.length) { |
68 | | - if (options.outputSourceRange) { |
69 | | - compiled.errors.forEach(e => { |
70 | | - warn( |
71 | | - `Error compiling template:\n\n${e.msg}\n\n` + |
72 | | - generateCodeFrame(template, e.start, e.end), |
73 | | - vm |
74 | | - ) |
75 | | - }) |
76 | | - } else { |
77 | | - warn( |
78 | | - `Error compiling template:\n\n${template}\n\n` + |
79 | | - compiled.errors.map(e => `- ${e}`).join('\n') + '\n', |
80 | | - vm |
81 | | - ) |
82 | | - } |
83 | | - } |
84 | | - if (compiled.tips && compiled.tips.length) { |
85 | | - if (options.outputSourceRange) { |
86 | | - compiled.tips.forEach(e => tip(e.msg, vm)) |
87 | | - } else { |
88 | | - compiled.tips.forEach(msg => tip(msg, vm)) |
89 | | - } |
90 | | - } |
91 | | - } |
92 | | - |
93 | 47 | // turn code into functions |
94 | 48 | const res = {} |
95 | 49 | const fnGenErrors = [] |
| 50 | + // 生成render函数 |
96 | 51 | res.render = createFunction(compiled.render, fnGenErrors) |
| 52 | + // 生成静态的render函数 |
97 | 53 | res.staticRenderFns = compiled.staticRenderFns.map(code => { |
98 | 54 | return createFunction(code, fnGenErrors) |
99 | 55 | }) |
100 | 56 |
|
101 | | - // check function generation errors. |
102 | | - // this should only happen if there is a bug in the compiler itself. |
103 | | - // mostly for codegen development use |
104 | | - /* istanbul ignore if */ |
105 | | - if (process.env.NODE_ENV !== 'production') { |
106 | | - if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) { |
107 | | - warn( |
108 | | - `Failed to generate render function:\n\n` + |
109 | | - fnGenErrors.map(({ err, code }) => `${err.toString()} in\n\n${code}\n`).join('\n'), |
110 | | - vm |
111 | | - ) |
112 | | - } |
113 | | - } |
114 | | - |
| 57 | + // 缓存并返回 |
115 | 58 | return (cache[key] = res) |
116 | 59 | } |
117 | 60 | } |
|
0 commit comments