项目压缩打包时,出现如下问题:
ERROR in views/index/index.js from UglifyJs
Unexpected token: [./node_modules/pingyin/lib/index.js]
思路一:
pinyin模块是es6编写的,index.js文件应转为es5。
es6转es5的配置方法如下:
1、安装webpack、babel-preset-es2015、label-loader模块;
2、.babelrc文件写入{ "presets": [ "es2015" ] };
3、webpack.config.js文件写入标红代码:
module.exports = {
entry: {
"views/index/index"
},
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: '[name].js'
},
module: {
loaders: [{
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/
}]
}
}
若以上均正确配置,依然报错,推测与UglifyJsPlugin有关。UglifyJsPlugin插件只能压缩打包es5文件
思路二:更换压缩打包模块
1、安装如下模块:
"uglify-js": "git://github.com/mishoo/UglifyJS2#harmony-v2.8.22",
"uglifyjs-webpack-plugin": “0.4.3",
注意:uglifyjs-webpack-plugin最新版本有问题,请安装0.4.4版本以下
2、webpack.config.js文件写入标红代码:
const UglifyJSPlugin = require(‘uglifyjs-webpack-plugin');
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new UglifyJSPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
码文不易,谢谢打赏