|
| 1 | +# createCompileToFunctionFn |
| 2 | + |
| 3 | +```js |
| 4 | +/* @flow */ |
| 5 | + |
| 6 | +import { noop, extend } from 'shared/util' |
| 7 | +import { warn as baseWarn, tip } from 'core/util/debug' |
| 8 | +import { generateCodeFrame } from './codeframe' |
| 9 | + |
| 10 | +type CompiledFunctionResult = { |
| 11 | + render: Function; |
| 12 | + staticRenderFns: Array<Function>; |
| 13 | +}; |
| 14 | +// 这里的new Function(code)就是用把模板字符串转换成函数 |
| 15 | +function createFunction (code, errors) { |
| 16 | + try { |
| 17 | + return new Function(code) |
| 18 | + } catch (err) { |
| 19 | + errors.push({ err, code }) |
| 20 | + return noop |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export function createCompileToFunctionFn (compile: Function): Function { |
| 25 | + const cache = Object.create(null) |
| 26 | + |
| 27 | + return function compileToFunctions ( |
| 28 | + template: string, |
| 29 | + options?: CompilerOptions, |
| 30 | + vm?: Component |
| 31 | + ): CompiledFunctionResult { |
| 32 | + options = extend({}, options) |
| 33 | + const warn = options.warn || baseWarn |
| 34 | + delete options.warn |
| 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 |
| 55 | + const key = options.delimiters |
| 56 | + ? String(options.delimiters) + template |
| 57 | + : template |
| 58 | + if (cache[key]) { |
| 59 | + return cache[key] |
| 60 | + } |
| 61 | + |
| 62 | + // compile |
| 63 | + const compiled = compile(template, options) |
| 64 | + |
| 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 | + // turn code into functions |
| 94 | + const res = {} |
| 95 | + const fnGenErrors = [] |
| 96 | + res.render = createFunction(compiled.render, fnGenErrors) |
| 97 | + res.staticRenderFns = compiled.staticRenderFns.map(code => { |
| 98 | + return createFunction(code, fnGenErrors) |
| 99 | + }) |
| 100 | + |
| 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 | + |
| 115 | + return (cache[key] = res) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +``` |
| 120 | + |
0 commit comments