Vue Template Compiler (Vue 2)
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
-
Cannot find module 'vue-template-compiler'
cause The package has not been installed or is not correctly resolved by your module system.fixRun `npm install vue-template-compiler` or `yarn add vue-template-compiler` in your project directory. -
SyntaxError: 'with' statement is not allowed in strict mode
cause The compiled render function, which uses the `with` statement, is being executed in a JavaScript environment that enforces strict mode.fixAvoid running generated render functions directly in strict mode contexts. This package is intended to be used by build tools and the Vue runtime, which manage the execution context correctly. If you're manually executing, ensure the function context is non-strict. -
TypeError: Cannot read properties of undefined (reading 'compile')
cause You are trying to call `compile` on an undefined `compiler` object, likely because the `require()` or `import` failed or returned an unexpected value.fixVerify that `vue-template-compiler` is correctly installed and imported. Ensure that `const compiler = require('vue-template-compiler')` or `import * as compiler from 'vue-template-compiler'` correctly assigns the module exports to the `compiler` variable.
Warnings
- breaking Vue 2 and its entire ecosystem, including `vue-template-compiler`, reached End of Life (EOL) on December 31st, 2023. There will be no further updates, bug fixes, or security patches. It is strongly recommended to migrate to Vue 3.
- gotcha The JavaScript code generated by `vue-template-compiler` for render functions uses the `with` statement. This means the compiled render functions cannot be executed in strict mode JavaScript, which is the default for ES Modules and often enforced in modern environments and build setups.
- gotcha Using custom compiler `modules` or `directives` can lead to incompatibility with standard build tools like `vue-loader`. These custom hooks alter the compilation process, making the templates non-standard and potentially unprocessable by other tools built on the default modules.
- deprecated While `vue-template-compiler` v2.7.16 is the final release, the package itself is functionally deprecated for new Vue projects due to the EOL of Vue 2. New development should focus on Vue 3, which uses `@vue/compiler-sfc` and `@vue/compiler-dom`.
Install
-
npm install vue-template-compiler -
yarn add vue-template-compiler -
pnpm add vue-template-compiler
Imports
- compiler
const compiler = require('vue-template-compiler') - compile
const { compile } = require('vue-template-compiler') - compiler (TypeScript/ESM)
import { compile } from 'vue-template-compiler'import * as compiler from 'vue-template-compiler'
Quickstart
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
// });