Webpack 4 “size exceeds the recommended limit (244 KiB)”

前端 未结 8 1600
旧巷少年郎
旧巷少年郎 2020-12-07 23:51

I have two files which are combined under 600 bytes (.6kb) as below.

So how is it that my app.bundle.js is so large (987kb) and more importantly how does one manage

相关标签:
8条回答
  • 2020-12-08 00:34

    Nuxt solution nuxt.config.js:

    module.exports = {
     build: {
      extend(config, { isDev , isClient }) {
        if (isClient && !isDev) {
          config.optimization.splitChunks.maxSize = 250000
        }
      }
     }
    }
    
    0 讨论(0)
  • I had the same problem. My bundle file was (1.15 MiB). In my webpack.config.js, replacing :

    devtool: 'inline-source-map'
    

    by this line:

    devtool: false,
    

    takes my bundle file size from 1.15 MiB to 275 Kib.

    Or create 2 separate webpack config files. One for dev and one for prod. In the prod webpack config file, delete the devtool option.

    0 讨论(0)
  • 2020-12-08 00:39

    Add the below line in your vue.config.js file. It is because of the size. We need to split into chunks.

    configureWebpack:{
        performance: {
          hints: false
        },
        optimization: {
          splitChunks: {
            minSize: 10000,
            maxSize: 250000,
          }
        }
    }
    

    This may help somebody.

    0 讨论(0)
  • 2020-12-08 00:42

    Simply use below code in webpack.config.js :

     performance: {
        hints: false,
        maxEntrypointSize: 512000,
        maxAssetSize: 512000
    }
    

    or follow

    You can create multiple config file for (development, production). In dev config file use devtool or others necessary dev configuration and vice versa .

    you have to use webpack-merge package and config package.json scripts code like

    "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "webpack --open --config webpack.dev.js",
     "dev": "webpack-dev-server --mode development --open",
     "build": "webpack --config webpack.prod.js"
     },
    

    For example :

    create a file webpack.common.js

    // webpack.common.js
    
      use your common configuration like entry, output, module, plugins,
    

    Create webpack.dev.js

    // webpack.dev.js
    const merge = require('webpack-merge');
    const common = require('./webpack.common.js');
    
    module.exports = merge(common, {
    mode: 'development',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist'
     }
    });
    

    Create webpack.prod.js

    const merge = require('webpack-merge');
    const common = require('./webpack.common.js');
    
    module.exports = merge(common, {
        mode: 'production',
        performance: {
            hints: false,
            maxEntrypointSize: 512000,
            maxAssetSize: 512000
        }
    });
    
    0 讨论(0)
  • 2020-12-08 00:45

    This happens because webpack is bundling all your code dependencies. And as you are using lodash, so lodash minified version will be added to your source code. Plus you are including the source maps:

    devtool: 'inline-source-map',
    

    While this should be fine for debug, there is no reason to include your source maps in a Prod build. So some things that you can do to reduce your bundle size.

    1. Make sure to set properly the mode: flag inside your webpack config. You can put either mode: 'development', or mode: 'production'. This will hint webpack about what kind of build you are doing so it will give you the proper warnings.
    2. Make sure to not include source maps on your prod build
    3. Avoid overusing external dependencies that you don't need and make.

    Sometimes even these things will not bring your bundle size to below 244kb, what you can do in these cases is to split your bundle and start to use logical chunks. First of all, you can easily separate your js from your styesheets by using the mini css extract plugin.

    Another technique that you can use are dynamic imports.

    Dynamic Imports: Split code via inline function calls within modules

    This will allow you to break your code logically into modules tied to the screens so only the required libraries will be loaded. For more info about dynamic imports, you can check the official documentation. https://webpack.js.org/guides/code-splitting/

    0 讨论(0)
  • 2020-12-08 00:45

    The main problem is devtool: 'inline-source-map' in webpack.common.js file in your case,in my case i use in development 'cheap-module-eval-source-map', this devtool is very nice for development mode but make my bundle.js to 4.2MiB,so,in production mode in webpack.prod.js file i change devtool like this module.exports = merge(common, { mode: 'production', performance: { hints: false }, devtool: 'source-map' });' and now from 4.2mb i 've got 732KiB of bundle.js. This devtool will slow down process of bundling for few more seconds but will remarkably reduce the size of the file,in my example from 4.18 MiB to 732 KiB .

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