UglifyJS throws unexpected token: keyword (const) with node_modules

后端 未结 7 1848
清歌不尽
清歌不尽 2020-12-13 16:31

A small project I started make use a node module (installed via npm) that declares const variables. Running and testing this project is well, b

相关标签:
7条回答
  • 2020-12-13 17:23

    I don't really think that this approach is good, but in my case I needed to do this once and forget about that, so I just went to babel's website , transpile es6 to es5 online and replaced the output!

    0 讨论(0)
  • 2020-12-13 17:25

    I had the same issue and the gulp plugin gulp-uglify-es resolved the problem.

    I think it's the simpliest decision.

    Just install:

    npm i gulp-uglify-es --save-dev
    

    after that in your code change only this line

    const uglify = require('gulp-uglify');
    

    to this:

    const uglify = require('gulp-uglify-es').default;
    

    N.B. property .default is crucial otherwise you'll have an error that uglify is not a function.

    As mentioned above and as being part of ES6 const operator can only be processed by more modern es6 gulp plugin "gulp-uglify-es"

    The rest of your code no need to be changed.

    Best regards!

    0 讨论(0)
  • 2020-12-13 17:25

    I have replaced UglifyJS with YUI Compressor JS inside the GUI of PHPStorm.. It works now.

    0 讨论(0)
  • 2020-12-13 17:30

    I just had this issue with a Gulp project I refactored and for some reason I was having trouble with the official Terser Gulp plugin. This one (gulp-terser) worked with no issues.

    0 讨论(0)
  • 2020-12-13 17:30

    Use uglify-es-webpack-plugin is better

        const UglifyEsPlugin = require('uglify-es-webpack-plugin')
    
    
    
        module.exports = {
        plugins: [
                new UglifyEsPlugin({
                    compress:{
                        drop_console: true
                    }
                }),
        ] 
        }
    
    0 讨论(0)
  • 2020-12-13 17:31

    UglifyJS does not support es6. const is an es6 declaration, so it throws an error.

    What is weird is that the package you use does not transpile its files to es5 to be used anywhere.

    If you want to still use UglifyJS (to re-use the configuration for example) use the ES6+ compatible version, uglify-es. (Warning: uglify-es is now abandoned.)

    And as Ser mentionned, you should now use terser-webpack-plugin.

    0 讨论(0)
提交回复
热议问题