Uglify SyntaxError: Unexpected token: punc ())

前端 未结 8 978
甜味超标
甜味超标 2020-12-01 07:28

I am trying to use gulp in order to minify a folder containing JS files. However, one of the files has the above error, preventing it from being minified.

I managed

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 07:35

    // Update

    From the comments ~ @imolit

     v2.0.0 (2018-09-14) - BREAKING CHANGES (link)

    Switch back to uglify-js (uglify-es is abandoned, if you need uglify ES6 code please use terser-webpack-plugin).


    Original answer before the update...

    I hope you can get inspired by this solution which works with webpack. (link below)

    Simply teach UglifyJS ES6

    There are two versions of UglifyJS - ES5 and ES6 (Harmony), see on git
    ES5 version comes by default with all the plugins, but if you install a Harmony version explicitly, those plugins will use it instead.

    package.json

    "uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"
    

    or

    npm install --save uglify-js@github:mishoo/UglifyJS2#harmony
    
    yarn add git://github.com/mishoo/UglifyJS2#harmony --dev
    

    Webpack

    To use it with webpack install also the webpack plugin

    npm install uglifyjs-webpack-plugin --save-dev
    
    yarn add uglifyjs-webpack-plugin --dev
    

    then import the manually installed plugin

    var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
    

    and replace it in code

    -  new webpack.optimize.UglifyJsPlugin({ ... })
    +  new UglifyJSPlugin({ ... })
    

    For more webpack info (Installation/Usage) see https://github.com/webpack-contrib/uglifyjs-webpack-plugin#install

提交回复
热议问题