Webpack how to build production code and how to use it

前端 未结 8 1083
梦谈多话
梦谈多话 2020-11-29 15:06

I am very new to webpack, I found that in production build we can able to reduce the size of overall code. Currently webpack builds around 8MB files and main.js around 5MB.

8条回答
  •  甜味超标
    2020-11-29 15:54

    In addition to Gilson PJ answer:

     new webpack.optimize.CommonsChunkPlugin('common.js'),
     new webpack.optimize.DedupePlugin(),
     new webpack.optimize.UglifyJsPlugin(),
     new webpack.optimize.AggressiveMergingPlugin()
    

    with

    "scripts": {
        "build": "NODE_ENV=production webpack -p --config ./webpack.production.config.js"
    },
    

    cause that the it tries to uglify your code twice. See https://webpack.github.io/docs/cli.html#production-shortcut-p for more information.

    You can fix this by removing the UglifyJsPlugin from plugins-array or add the OccurrenceOrderPlugin and remove the "-p"-flag. so one possible solution would be

     new webpack.optimize.CommonsChunkPlugin('common.js'),
     new webpack.optimize.DedupePlugin(),
     new webpack.optimize.UglifyJsPlugin(),
     new webpack.optimize.OccurrenceOrderPlugin(),
     new webpack.optimize.AggressiveMergingPlugin()
    

    and

    "scripts": {
        "build": "NODE_ENV=production webpack --config ./webpack.production.config.js"
    },
    

提交回复
热议问题