Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4 "size exceeds the recommended limit (244 KiB)"

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 the size of it?

src file index.js

import _ from 'lodash'; import printMe from './print.js';     function component() {     var element = document.createElement('div');     var btn = document.createElement('button');      // Lodash, now imported by this script     element.innerHTML = _.join(['Hello', 'webpack'], ' ');      btn.innerHTML = 'click and check console';     btn.onclick = printMe;      element.appendChild(btn);      return element;   }    document.body.appendChild(component()); 

src file print.js

export default function printMe() {   consoe.log('Called from print.js'); } 

webpack.config.js

const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanWebpackPlugin = require('clean-webpack-plugin');  module.exports = {   entry: {     app: './src/index.js',     print:'./src/print.js'   },   devtool: 'inline-source-map',   plugins: [     new CleanWebpackPlugin(['dist']),     new HtmlWebpackPlugin({         title: 'Output Management'     })   ],   output: {     filename: '[name].bundle.js',     path: path.resolve(__dirname, 'dist')   } }; 

package.json

{   "name": "my-webpack-4-proj",   "version": "1.0.0",   "description": "",   "main": "index.js",   "mode": "development",   "scripts": {     "dev": "webpack --mode development",     "build": "webpack --mode production",     "watch": "webpack --watch",     "test": "echo \"Error: no test specified\" && exit 1"   },   "keywords": [],   "author": "",   "license": "ISC",   "devDependencies": {     "clean-webpack-plugin": "^0.1.19",     "css-loader": "^0.28.11",     "csv-loader": "^2.1.1",     "file-loader": "^1.1.11",     "html-webpack-plugin": "^3.0.6",     "style-loader": "^0.20.3",     "webpack": "^4.1.1",     "webpack-cli": "^2.0.12",     "xml-loader": "^1.2.1"   },   "dependencies": {     "express": "^4.16.3",     "lowdash": "^1.2.0"   } } 

Warning message:

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance. Assets: app.bundle.js (964 KiB)

like image 430
Michael Nelles Avatar asked Mar 18 '18 13:03

Michael Nelles


People also ask

How do I reduce the size of a Webpack?

Using Production Mode in Webpack. Running your project in production mode reduces the bundle sizes compared to the development mode. Webpack provides a separate flag ( -p ) to enable this feature and removes all the white spaces and newlines in the code.

How do I reduce react bundle size?

Every React developer should add compression to their bundling process. One of the most impactful techniques to reduce the bundle size of a React application is compression. compression is a process in which the size of a file is reduced by re-encoding the file data to use fewer bits of storage than the original file.

How do you analyze Webpack bundle size?

If you want to analyze packages, you can simply search for that package name. It will give you all the details, including file size, download time, dependency information, and statistics for various plugin versions. To analyze bundle size, you just need to upload the package. json file to the website.


1 Answers

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     } }); 
like image 71
Riyadh Ahmed Avatar answered Sep 28 '22 01:09

Riyadh Ahmed