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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can I use babel with vue-loader?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.