Webpack 4 "size exceeds the recommended limit (244 KiB)"
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.
- 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.
- Make sure to not include source maps on your prod build
- 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/
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
}
});