Vue Template Compiler (Vue 2)

2.7.16 · abandoned · verified Sun Apr 19

The `vue-template-compiler` package is an internal component of the Vue 2 ecosystem, designed to pre-compile Vue 2.x template strings into JavaScript render functions. This process reduces runtime overhead, improves performance, and allows Vue applications to operate in environments with Content Security Policy (CSP) restrictions by avoiding `eval` usage. The current and final stable version is `2.7.16` ("Swan Song"), released as part of Vue 2.7, which aimed to backport some Vue 3 Composition API features to Vue 2. This package, like Vue 2 itself, reached its official End of Life (EOL) on December 31st, 2023, meaning it will no longer receive updates, bug fixes, or security patches. While direct usage is possible for custom build tools, it was primarily designed to be consumed by build integrations like `vue-loader` for Webpack or similar tools, providing the core compilation logic for `.vue` single file components.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates compiling a simple Vue 2 template into render functions using `vue-template-compiler`. It outputs the main render function and any static render functions, illustrating the core functionality of the package. It also shows an example of using the `whitespace` option.

import * as compiler from 'vue-template-compiler';

const template = `
<div id="app">
  <h1>{{ message }}</h1>
  <p v-if="showParagraph">This is a paragraph.</p>
  <button @click="toggleParagraph">Toggle</button>
</div>`;

const compiled = compiler.compile(template, {
  whitespace: 'condense' // Optimize for smaller output
});

if (compiled.errors.length) {
  console.error('Template compilation errors:\n' + compiled.errors.join('\n'));
  process.exit(1);
}

console.log('--- Render Function ---\n' + compiled.render);
console.log('\n--- Static Render Functions ---\n' + compiled.staticRenderFns.join('\n'));

// Example of how the render function would roughly be used at runtime (requires Vue 2)
// const app = new Vue({
//   el: '#app',
//   data: {
//     message: 'Hello, Vue 2!',
//     showParagraph: true
//   },
//   methods: {
//     toggleParagraph() {
//       this.showParagraph = !this.showParagraph;
//     }
//   },
//   render: new Function(compiled.render)() // DANGER: Uses eval-like Function constructor
// });

view raw JSON →