【问题标题】:vue webpack uglify - arrow functionsvue webpack uglify - 箭头函数
【发布时间】:2017-09-29 20:52:52
【问题描述】:

在构建包含箭头函数的 Vue 项目时,webpack 的 UglifJsPlugin 出现此错误:

Unexpected token: operator (>)

示例:

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 ]
}

注意:我的问题被标记为与此重复: Arrow Function syntax not working with webpack?
1) 与 Vue 无关
2)它是关于使用箭头函数作为类成员,我的问题不是

【问题讨论】:

标签: webpack vue.js uglifyjs arrow-functions


【解决方案1】:

目前UglifyJsPlugin 不支持 ES6 特性(箭头函数),所以你必须先使用 babel 将你的 ES6 代码编译为 ES5,然后在其中使用 UglifyJsPlugin

【讨论】:

  • 如何在 vue-loader 中使用 babel?
  • 首先,您必须安装babel-loader 并创建其配置。然后在您的 webpack 配置中,为 js 创建新规则以使用 babel(您不需要编辑 vue-loader 配置,因为它会自动检测存在 babel-loader 并使用它
猜你喜欢
  • 1970-01-01
  • 2021-02-13
  • 2020-06-26
  • 1970-01-01
  • 2018-01-29
  • 2019-05-15
  • 1970-01-01
相关资源
最近更新 更多