Vue Template Babel Compiler

2.0.0 · maintenance · verified Sun Apr 19

The `vue-template-babel-compiler` package enhances the capabilities of `vue-template-compiler` (for Vue 2 projects) by integrating Babel to allow the use of modern ECMAScript syntax within Vue Single File Component (SFC) templates. This includes features like Optional Chaining (`?.`), Nullish Coalescing (`??`), and BigInt, which are not natively supported by the standard `vue-template-compiler` in older Node.js environments. The current stable version is `2.0.0`, which notably requires Node.js v14 or higher due to how `vue-template-compiler` internally evaluates JavaScript expressions. While not frequently updated, the project is marked as maintained and provides sporadic, but critical, updates. Its key differentiator is providing a seamless way to leverage cutting-edge JavaScript features directly in Vue 2 template expressions, offering customization for Babel options, and plugging into existing Vue CLI and Nuxt.js build configurations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `vue-template-babel-compiler` in a Vue CLI project's `vue.config.js` to enable Optional Chaining (`?.`) and Nullish Coalescing (`??`) syntax directly within Vue 2 template expressions, which would otherwise cause a compile error.

{
  // package.json snippet for context
  "name": "my-vue2-app",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "@vue/cli-service": "^4.5.0",
    "vue-template-babel-compiler": "^2.0.0",
    "vue-template-compiler": "^2.6.0",
    "vue": "^2.6.11"
  }
}

// vue.config.js
module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        // Configure vue-loader to use vue-template-babel-compiler
        options.compiler = require('vue-template-babel-compiler')
        return options
      })
  }
}

// src/App.vue
<template>
  <div>
    <p>{{ user?.name ?? 'Guest' }}</p>
    <p v-if="data?.items?.length > 0">Items available!</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: null, // Simulate data that might be null initially
      data: { items: [1, 2] }
    }
  },
  mounted() {
    // Example of delayed data
    setTimeout(() => {
      this.user = { name: 'Alice' };
      this.data = null; // Simulate data becoming null
    }, 2000);
  }
}
</script>

view raw JSON →