vue-template-compiler

This package is auto-generated. For pull requests please see src/platforms/web/entry-compiler.js.

This package can be used to pre-compile Vue 2.0 templates into render functions to avoid runtime-compilation overhead and CSP restrictions. In most cases you should be using it with vue-loader, you will only need it separately if you are writing build tools with very specific needs.

Installation

npm install vue-template-compiler
const compiler = require('vue-template-compiler')

API

compiler.compile(template, [options])

Compiles a template string and returns compiled JavaScript code. The returned result is an object of the following format:

{
  ast: ?ASTElement, // parsed template elements to AST
  render: string, // main render function code
  staticRenderFns: Array<string>, // render code for static sub trees, if any
  errors: Array<string> // template syntax errors, if any
}

Note the returned function code uses with and thus cannot be used in strict mode code.

Options


compiler.compileToFunctions(template)

Similar to compiler.compile, but directly returns instantiated functions:

{
  render: Function,
  staticRenderFns: Array<Function>
}

This is only useful at runtime with pre-configured builds, so it doesn't accept any compile-time options. In addition, this method uses new Function() so it is not CSP-compliant.


compiler.ssrCompile(template, [options])

2.4.0+

Same as compiler.compile but generates SSR-specific render function code by optimizing parts of the template into string concatenation in order to improve SSR performance.

This is used by default in vue-loader@>=12 and can be disabled using the optimizeSSR option.


compiler.ssrCompileToFunctions(template)

2.4.0+

Same as compiler.compileToFunction but generates SSR-specific render function code by optimizing parts of the template into string concatenation in order to improve SSR performance.


compiler.parseComponent(file, [options])

Parse a SFC (single-file component, or *.vue file) into a descriptor (refer to the SFCDescriptor type in flow declarations). This is used in SFC build tools like vue-loader and vueify.


compiler.generateCodeFrame(template, start, end)

Generate a code frame that highlights the part in template defined by start and end. Useful for error reporting in higher-level tooling.

Options

pad

pad is useful when you are piping the extracted content into other pre-processors, as you will get correct line numbers or character indices if there are any syntax errors.