用webpack的uglifyJS压缩ES6代码报错

不能用webpack压缩ES6的代码吗?去掉ES6代码就正常了
图片描述

出错的代码处,都是对象属性简写,箭头函数等ES6代码

module.exports = {
    mounted(){this.ready = true} //出错代码行
}

补充:
下载的npm包都是刚下的

再次补充:
发现问题出在babel没把代码转成ES5

"babel-core": "^6.17.0",
"babel-loader": "^6.2.5",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.16.0",
"babel-runtime": "^6.11.6",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.16.2"

babel的配置,配置在webpack.config.js的loader

loaders: [
        { test: /\.vue$/, loader: 'vue' },
        { test: /\.less$/, loader: "style!css!less" },
        { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=233&name=images/[name].[hash:6].[ext]' },
        { test: /\.js$/, exclude: /node_modules/, loader: 'babel', query: { presets: ['es2015']}},
    ]

uglifyJS的配置:

new webpack.optimize.UglifyJsPlugin({
    compress: {
        warnings: false,
        drop_console: true
    },
})
阅读 26.6k
6 个回答

webpack真是越来越搞不懂了,试着试着bug就被解决了
webpack.config.js改成这样就行了

module: {
        loaders: [
            { test: /\.js$/, exclude: /node_modules/, loader: 'babel'},
            { test: /\.vue$/, loader: 'vue' },
            { test: /\.less$/, loader: "style!css!less" },
            { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=233&name=images/[name].[hash:6].[ext]' },
        ],

    },
    babel: {
        babelrc: false,
        presets: [
            ['es2015'],
        ],
    }

你需要 babel 转义 ES6

新手上路,请多包涵

Vue loader 会自动读取项目下的 .babelrc 文件,获取到 presets 配置,从而自动编译箭头函数等语法糖。

毕竟不是所有用 Vue loader 的人,都在用 ES2015。

踩了一天这个坑,终于跳出来了,根据自己的webpack.base.config.js所在的目录,打包的时候相应的include需要babel的路径,路径一定要正确,且include 和 exclude不能共用,或者说不能同时作用于同一个目录吧。
按正常的逻辑。node_modules 里的文件应该不包含es6的代码才对
{

test: /\.js$/,
loader: 'happypack/loader?id=happybabel',
include: [resolve('../build'), resolve('../src'), resolve('../node_modules/iview/src'), resolve('../node_modules/iview/dist')]

}

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题