0

When building a Vue project including arrow functions, webpack's UglifJsPlugin gives this error:

Unexpected token: operator (>)

example:

app.js

import Vue from 'vue';
import HelloWorldComponent from "./HelloWorld.vue";

new Vue({
  el: '#app'
  ,render: r => r(HelloWorldComponent)
});

HelloWorld.vue

<template>
    <div>{{message}}</div>
</template>

<script>
const data = { message: "Hello World" }
export default {
    data() { return data }
}
</script>

webpack.config.js

const webpack = require('webpack');
const path = require("path");
const HtmlPlugin = require("html-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

const output = {
    path: path.resolve(__dirname, 'build'),
    filename: 'app.js'
};
const vueLoaderRule = {
    test: /\.vue$/,
    loader: 'vue-loader'
};
const uglifyJsPlugin = new UglifyJsPlugin({
    include: /\.js$/,
    minimize: true
});

module.exports = {
    entry: './app.js'
    ,output: output
    ,module: {rules: [ vueLoaderRule ]}
    ,plugins: [ uglifyJsPlugin ]
}

note: my question was marked as duplicate of this: Arrow Function syntax not working with webpack?
1) it has nothing to do with Vue
2) it is about using arrow functions as class member, wheres my question is not

nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95
  • 1
    Possible duplicate of [Arrow Function syntax not working with webpack?](https://stackoverflow.com/questions/42063854/arrow-function-syntax-not-working-with-webpack) – Roy J Sep 29 '17 at 21:14

1 Answers1

0

Right now, UglifyJsPlugin doesn't support ES6 feature (arrow function) so you must use babel first to compile your ES6 code to ES5 then use UglifyJsPlugin in it

Quoc-Anh Nguyen
  • 4,798
  • 1
  • 22
  • 34
  • How can I use babel with vue-loader? – nagy.zsolt.hun Sep 30 '17 at 16:18
  • First, you have to install `babel-loader` and create its config. Then in your webpack config, create new rule for `js` to use `babel` (you doesn't need to edit vue-loader config because it will auto-detect the presence `babel-loader` and use it – Quoc-Anh Nguyen Sep 30 '17 at 16:21