Home / File/ to-function.ts — vue Source File

to-function.ts — vue Source File

Architecture documentation for to-function.ts, a typescript file in the vue codebase. 6 imports, 1 dependents.

File typescript VueCore GlobalAPI 6 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  fce21fb3_1100_1200_4d40_dbc0f8981911["to-function.ts"]
  e7aa288a_f5ad_28a9_75b4_4d0ad05fd70c["codeframe.ts"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> e7aa288a_f5ad_28a9_75b4_4d0ad05fd70c
  dd20ebf1_1620_a5f1_519f_f296f3f66e87["generateCodeFrame"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> dd20ebf1_1620_a5f1_519f_f296f3f66e87
  09aa5370_2caa_6b33_3f44_6ac5211bd11b["util"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> 09aa5370_2caa_6b33_3f44_6ac5211bd11b
  49aae190_0a6d_f5d1_c8de_aabde088509c["debug"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> 49aae190_0a6d_f5d1_c8de_aabde088509c
  64c87498_c46a_6944_ab9d_8e45519852a8["component"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> 64c87498_c46a_6944_ab9d_8e45519852a8
  a80b8e3b_d720_9146_3bf6_594d4ee5dd77["compiler"]
  fce21fb3_1100_1200_4d40_dbc0f8981911 --> a80b8e3b_d720_9146_3bf6_594d4ee5dd77
  ff1da07c_8f38_b75f_ad5a_42883a44a3df["create-compiler.ts"]
  ff1da07c_8f38_b75f_ad5a_42883a44a3df --> fce21fb3_1100_1200_4d40_dbc0f8981911
  style fce21fb3_1100_1200_4d40_dbc0f8981911 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { noop, extend } from 'shared/util'
import { warn as baseWarn, tip } from 'core/util/debug'
import { generateCodeFrame } from './codeframe'
import type { Component } from 'types/component'
import { CompilerOptions } from 'types/compiler'

type CompiledFunctionResult = {
  render: Function
  staticRenderFns: Array<Function>
}

function createFunction(code, errors) {
  try {
    return new Function(code)
  } catch (err: any) {
    errors.push({ err, code })
    return noop
  }
}

export function createCompileToFunctionFn(compile: Function): Function {
  const cache = Object.create(null)

  return function compileToFunctions(
    template: string,
    options?: CompilerOptions,
    vm?: Component
  ): CompiledFunctionResult {
    options = extend({}, options)
    const warn = options.warn || baseWarn
    delete options.warn

    /* istanbul ignore if */
    if (__DEV__) {
      // detect possible CSP restriction
      try {
        new Function('return 1')
      } catch (e: any) {
        if (e.toString().match(/unsafe-eval|CSP/)) {
          warn(
            'It seems you are using the standalone build of Vue.js in an ' +
              'environment with Content Security Policy that prohibits unsafe-eval. ' +
              'The template compiler cannot work in this environment. Consider ' +
              'relaxing the policy to allow unsafe-eval or pre-compiling your ' +
              'templates into render functions.'
          )
        }
      }
    }

    // check cache
    const key = options.delimiters
      ? String(options.delimiters) + template
      : template
    if (cache[key]) {
      return cache[key]
    }

    // compile
    const compiled = compile(template, options)

    // check compilation errors/tips
    if (__DEV__) {
      if (compiled.errors && compiled.errors.length) {
        if (options.outputSourceRange) {
          compiled.errors.forEach(e => {
            warn(
              `Error compiling template:\n\n${e.msg}\n\n` +
                generateCodeFrame(template, e.start, e.end),
              vm
            )
          })
        } else {
          warn(
            `Error compiling template:\n\n${template}\n\n` +
              compiled.errors.map(e => `- ${e}`).join('\n') +
              '\n',
            vm
          )
        }
      }
      if (compiled.tips && compiled.tips.length) {
        if (options.outputSourceRange) {
          compiled.tips.forEach(e => tip(e.msg, vm))
        } else {
          compiled.tips.forEach(msg => tip(msg, vm))
        }
      }
    }

    // turn code into functions
    const res: any = {}
    const fnGenErrors: any[] = []
    res.render = createFunction(compiled.render, fnGenErrors)
    res.staticRenderFns = compiled.staticRenderFns.map(code => {
      return createFunction(code, fnGenErrors)
    })

    // check function generation errors.
    // this should only happen if there is a bug in the compiler itself.
    // mostly for codegen development use
    /* istanbul ignore if */
    if (__DEV__) {
      if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {
        warn(
          `Failed to generate render function:\n\n` +
            fnGenErrors
              .map(
                ({ err, code }) => `${(err as any).toString()} in\n\n${code}\n`
              )
              .join('\n'),
          vm
        )
      }
    }

    return (cache[key] = res)
  }
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does to-function.ts do?
to-function.ts is a source file in the vue codebase, written in typescript. It belongs to the VueCore domain, GlobalAPI subdomain.
What functions are defined in to-function.ts?
to-function.ts defines 2 function(s): createCompileToFunctionFn, createFunction.
What does to-function.ts depend on?
to-function.ts imports 6 module(s): codeframe.ts, compiler, component, debug, generateCodeFrame, util.
What files import to-function.ts?
to-function.ts is imported by 1 file(s): create-compiler.ts.
Where is to-function.ts in the architecture?
to-function.ts is located at src/compiler/to-function.ts (domain: VueCore, subdomain: GlobalAPI, directory: src/compiler).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free