Actual solution to UglifyJs error when packaging iview (detailed tutorial)

亚连
Release: 2018-06-01 09:09:30
Original
2450 people have browsed it

Now I will share with you an article to solve the problem of UglifyJs error when packaging iview. It has a good reference value and I hope it will be helpful to everyone.

The operation is ok when using npm run dev, but iview reports an error when npm run build is packaged,

is as follows:

The reason is that iview uses es6 syntax, but uglifyJs does not support it. Open our build/webpack.prod.conf.js file and you can see

// UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, sourceMap: config.build.productionSourceMap, parallel: true }),
Copy after login

It has been prompted that uglifyJs does not support es6.

Solution:

In webpack.base.conf.js, we first compile in js When adding the following:

{ test: /\.js$/, loader: 'babel-loader', include: [resolve('src'), resolve('test'),resolve('/node_modules/iview/src'),resolve('/node_modules/iview/packages')] },
Copy after login

First let iview's es6 syntax be converted by babel, then in build/webpack.prod.conf.js, comment out the original uglifyJs, and introduce external uglifyJs to compress js Obfuscated, the code is as follows:

// UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify // new webpack.optimize.UglifyJsPlugin({ // compress: { // warnings: false // }, // sourceMap: config.build.productionSourceMap, // parallel: true // }), new UglifyJsPlugin({ // 使用外部引入的新版本的js压缩工具 parallel: true, uglifyOptions: { ie8: false, ecma: 6, warnings: false, mangle: true, // debug false output: { comments: false, beautify: false, // debug true }, compress: { // 在UglifyJs删除没有用到的代码时不输出警告 warnings: false, // 删除所有的 `console` 语句 // 还可以兼容ie浏览器 drop_console: true, // 内嵌定义了但是只用到一次的变量 collapse_vars: true, // 提取出出现多次但是没有定义成变量去引用的静态值 reduce_vars: true, } } }),
Copy after login

Of course we need to introduce external plug-ins first:

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

A brief discussion on the solution to excessively large files after webpack packaging

Vue ElementUI implements dynamic rendering and visualization of forms Configuration method

Solution to error reporting after angularjs uses gulp-uglify compression

The above is the detailed content of Actual solution to UglifyJs error when packaging iview (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!