最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - webpack 4 uglifyjs not minifying and compressing - Stack Overflow

programmeradmin3浏览0评论

I am using webpack 4 and I am unsure if my code is being compressed and minified. I am using React as well.

My first problem is using the Webpack UglifyJS plugin in the webpack plugin property or the optimization property. When I use the plugin property it seems to compress at least but not to a single line. I am still unsure if it is minifying. When I use optimization it does not even compress. When I take a look at my bundled js file, it seems to be bundling things in node_modules such as webpack.

//works with plugin

module.exports = {
    ...
    plugins: [new UglifyJsPlugin({
        test: /\.js$/,
        exclude: /node_modules/,
        sourceMap: true,
        uglifyOptions: {
            compress: {},
            mangle: true,
        }
}],

//does not work with optimization

module.exports = {
    ...
    optimization: {
        minimize: true,
        minimizer: [new UglifyJsPlugin({
            test: /\.js$/,
            exclude: /node_modules/,
            sourceMap: true,
            uglifyOptions: {
                compress: {},
                mangle: true,
             }
       }],
    }

With the first example, the code gets compressed at least but not into one single line.

//Example

!*** ./node_modules/scheduler/index.js ***!
  \*****************************************/
/*! no static exports found */function(module,exports,__webpack_require__){"use strict";eval('\n\nif (false) {} else {\n...

 !*** ./node_modules/scheduler/tracing.js ***!
  \*******************************************/
/*! no static exports found */function(module,exports,__webpack_require__){"use strict";eval('\n\nif (false) {...

Also not sure if it being minified. I wrote a function in my React Component

someFunc(one, two) {
    return one + two
}

According to the npm uglifyjs docs, this should be minified into

someFunc(a, b) { \n return a+b\n}

but it is being output as

someFunc(one, two) { \n return one + two\n}

Is this minifying?

I am using webpack 4 and I am unsure if my code is being compressed and minified. I am using React as well.

My first problem is using the Webpack UglifyJS plugin in the webpack plugin property or the optimization property. When I use the plugin property it seems to compress at least but not to a single line. I am still unsure if it is minifying. When I use optimization it does not even compress. When I take a look at my bundled js file, it seems to be bundling things in node_modules such as webpack.

//works with plugin

module.exports = {
    ...
    plugins: [new UglifyJsPlugin({
        test: /\.js$/,
        exclude: /node_modules/,
        sourceMap: true,
        uglifyOptions: {
            compress: {},
            mangle: true,
        }
}],

//does not work with optimization

module.exports = {
    ...
    optimization: {
        minimize: true,
        minimizer: [new UglifyJsPlugin({
            test: /\.js$/,
            exclude: /node_modules/,
            sourceMap: true,
            uglifyOptions: {
                compress: {},
                mangle: true,
             }
       }],
    }

With the first example, the code gets compressed at least but not into one single line.

//Example

!*** ./node_modules/scheduler/index.js ***!
  \*****************************************/
/*! no static exports found */function(module,exports,__webpack_require__){"use strict";eval('\n\nif (false) {} else {\n...

 !*** ./node_modules/scheduler/tracing.js ***!
  \*******************************************/
/*! no static exports found */function(module,exports,__webpack_require__){"use strict";eval('\n\nif (false) {...

Also not sure if it being minified. I wrote a function in my React Component

someFunc(one, two) {
    return one + two
}

According to the npm uglifyjs docs, this should be minified into

someFunc(a, b) { \n return a+b\n}

but it is being output as

someFunc(one, two) { \n return one + two\n}

Is this minifying?

Share Improve this question asked Nov 25, 2018 at 8:10 henhenhenhen 1,2054 gold badges20 silver badges42 bronze badges 2
  • 1 Maybe you should switch to terser-webpack-plugin. uglify-js doesn't support ES6+ features and uglify-es is no longer maintained. So if you will run into issues, you are on your own. – Hardik Modha Commented Nov 25, 2018 at 8:17
  • I am aiming to minify the transpiled JS code that runs through my webpack loaders. Basically I want to minify the bundle js file which should be es5 – henhen Commented Nov 25, 2018 at 8:34
Add a comment  | 

2 Answers 2

Reset to default 16

Webpack 4 does optimization and minification by default in production mode.

So if my guess is right, your configuration is development configuration.

You can remove your explicit UglifyJsPlugin definition and set the mode to production and Webpack will take care of everything.

mode: 'production',
plugins: [...],
optimization: ...

You can customize your optimizations though if you must. But setting the mode to production will yield you your expected results.

More info here

Webpack 4 mode usage

I was also facing the same issue. It started working after providing mode config value as production.

module.exports = {
    // webpack config
    mode: process.env.NODE_ENV || 'development',
};

// command NODE_ENV=production webpack
发布评论

评论列表(0)

  1. 暂无评论