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

npm - Correct way to use minimize javascript in webpack.config.js - Stack Overflow

programmeradmin1浏览0评论

I'm following a tutorial about webpack, but it seems that the tutorial is making use of an older version of webpack. I'm trying to minimize the .js files but every time I run npm run webpack I get this error message in the console:

webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

How do I use that config.optimization.minimize ? I've been googling for some time but with no success... What do I need to change in my webpack.config.js?

This is my webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('allstyles.css');

module.exports = {
  entry: './wwwroot/source/app.js',
  output: {
    path: path.resolve(__dirname, 'wwwroot/dist'),
    filename: 'bundle.js'
  },
  plugins: [
    extractCSS,
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery',
      Popper: ['popper.js', 'default']
    }),
    new webpack.optimize.UglifyJsPlugin()
  ],
  module: {
    rules: [
      {test: /\.css$/, use: extractCSS.extract(['css-loader?minimize'])},
      {test: /\.js$/,//      
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env']
          }
        }
      }
    ]
  }
};

package.json:

{
  "name": "WebpackBlogExample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "wbp": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.3",
    "babel-preset-env": "^1.6.1",
    "bootstrap": "^4.0.0-beta.2",
    "css-loader": "^0.28.10",
    "extract-text-webpack-plugin": "^3.0.2",
    "jquery": "^3.3.1",
    "popper.js": "^1.12.9",
    "style-loader": "^0.20.2",
    "uglifyjs-webpack-plugin": "^1.2.2",
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.9"
  },
  "dependencies": {}
}

I'm following a tutorial about webpack, but it seems that the tutorial is making use of an older version of webpack. I'm trying to minimize the .js files but every time I run npm run webpack I get this error message in the console:

webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

How do I use that config.optimization.minimize ? I've been googling for some time but with no success... What do I need to change in my webpack.config.js?

This is my webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('allstyles.css');

module.exports = {
  entry: './wwwroot/source/app.js',
  output: {
    path: path.resolve(__dirname, 'wwwroot/dist'),
    filename: 'bundle.js'
  },
  plugins: [
    extractCSS,
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery',
      Popper: ['popper.js', 'default']
    }),
    new webpack.optimize.UglifyJsPlugin()
  ],
  module: {
    rules: [
      {test: /\.css$/, use: extractCSS.extract(['css-loader?minimize'])},
      {test: /\.js$/,//      
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env']
          }
        }
      }
    ]
  }
};

package.json:

{
  "name": "WebpackBlogExample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "wbp": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.3",
    "babel-preset-env": "^1.6.1",
    "bootstrap": "^4.0.0-beta.2",
    "css-loader": "^0.28.10",
    "extract-text-webpack-plugin": "^3.0.2",
    "jquery": "^3.3.1",
    "popper.js": "^1.12.9",
    "style-loader": "^0.20.2",
    "uglifyjs-webpack-plugin": "^1.2.2",
    "webpack": "^4.0.1",
    "webpack-cli": "^2.0.9"
  },
  "dependencies": {}
}
Share Improve this question asked Mar 1, 2018 at 1:11 AlexGHAlexGH 2,8276 gold badges48 silver badges84 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

https://medium./webpack/webpack-4-mode-and-optimization-5423a6bc597a

If you look at the url it will explain all optimization options.

By default in dev mode webpack 4 won't minimize js, this is to speed up development. As soon as you switch mode to production or use the -p while running webpack it will automatically minimize your JS there is no need for uglifyjs setup anymore in webpack 4.

Webpack 4.X and minifying JS

UglifyJSPlugin:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

...

optimization: {
  minimizer: [
    new UglifyJSPlugin({
      uglifyOptions: { ... }
    })
  ]
}

Other options:

Note that while the UglifyJSPlugin is a great place to start for minification, there are other options out there. Here are a few more popular ones:

[BabelMinifyWebpackPlugin][1]
[ClosureCompilerPlugin][1]

If you decide to try another, just make sure your new choice also drops dead code as described in the tree shaking guide.

source: https://webpack.js/guides/production/

Look at: https://stackoverflow./a/49767690/6200607

发布评论

评论列表(0)

  1. 暂无评论