DEV Community

JuniorTour
JuniorTour

Posted on

Use Babel to parse Vue.js SFC

Github Repo: vue-template-babel-compiler

Enable Optional Chaining(?.), Nullish Coalescing(??) and many new ES syntax for Vue.js SFC based on Babel.

Downloads Version LastCommit

DEMO

DEMO

Features

Usage

1. Install

npm install vue-template-babel-compiler --save-dev
Enter fullscreen mode Exit fullscreen mode

2. Config

1. Vue-CLI

DEMO project for Vue-CLI

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

2. Nuxt.js

DEMO project for Nuxt.js

// nuxt.config.js
export default {
  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    loaders: {
      vue: {
        compiler: require('vue-template-babel-compiler')
      }
    },
  },
  // ...
}
Enter fullscreen mode Exit fullscreen mode

3. Webpack

// your webpack.config.js where config vue-loader
module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
            compiler: require('vue-template-babel-compiler')
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

API

Welcome for Issues && PR.

Top comments (0)