1.0.2 • Published 4 years ago

webpack-bundle-optimizer v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Table of Contents

Install

npm i --save-dev webpack-bundle-optimizer

Usage

// webpack.config.js
const Optimizer = require("webpack-bundle-optimizer");
module.exports = {
  entry: //...,
  output: //...,
  plugins: [
    new Optimizer()
  ]
}

Options

  • whiteList: if defined, file name found in this array will be optimized.
  • BlackList: if defined, file name not found in this array will be optimized.
  • predicate: predicate function which will be used to find filename in array.

Default predicate function

function (pName, cName) {
    return pName === cName
};

where:

  • pName is name provided in whiteList/blackList array.
  • cName is current filename in processing

Example

This example assume fileName are generated with hash (which is common pattern now): filename : main-98becfc0b54581da9521.js

// webpack.config.js
const Optimizer = require("webpack-bundle-optimizer");
module.exports = {
  entry: //...,
  output: //...,
  plugins: [
    new Optimizer({
    whiteList: ['vendor-react', 'vendor-all', 'runtime', 'main'],
    predicate: function (pName, cName) {
      const fileNameParts = cName.split('-');
      fileNameParts.pop();
      const fileName = fileNameParts.join('-');
      return pName === fileName;
    }
  })
  ]
}

Here as I am removing hash and extension I am passing name in whiteList array without extension.

Please note: If you have serviceworker with importScript, it should be added in blackList. For more info go here.

Documentation

Optimisation is one place which make many web developers to bang their head on wall. As mobile user are increasing, initial load time plays also a major factor in you web-app performance.

One of JavaScript’s heaviest costs is the time for a JS engine to parse/compile/optimize JS code. There is very good article provides insight of JS execution and parsing cost: The cost of JavaScript in 2019.

Modern JS engine V8 (others) do a pre-parse most functions before doing a full parse. This step checks for syntax errors. This saves cost of full parse if there exist any error. However, there are use cases (like webpack output bundle) where we want to execute our JS code as soon as possible. In cases (which include common module formats like UMD/Browserify/Webpack/etc.), the browser will actually parse the function twice, first as a pre-parse and second as a full parse.

Benchmark overview


ToolTypical speed boost/regression using webpack-bundle-optimizerold scorenew score
google page speed score15.15%3338

Reports are available inside reports folder.

How pagespeed insight works

Acc. to google, lighthouse pagespeed score are calculated on following metric:

MetricCategory Weighting
first-contentful-paint20.00%
first-meaningful-paint6.70%
speed-index26.70%
interactive33.30%
first-cpu-idle13.30%
max-potential-fid0.00%

Based on the above, plugin targets speed-index and interactive. Following were impactful zone for optimisation:

  • Script Evaluation
  • JS execution and parsing

For which following were done as follows:

With optimization we were able to reduce significant time as follows (all time in seconds):

Page Speed Optimisation:

FCP (second)First CPU IdleTTI (second)Score
Unoptimized1.112.313.233
Optimized111.612.438
Diff-0.1-0.7-0.85

This is a 15% gain in performance score.

Script EvaluationJS execution & parsingMain Thread work
Unoptimized5.5260.5378
Optimized4.9530.4937.1
Diff0.5750.10.9

We are saving approx. 1 sec on JS execution time overall after optimisation.

TODOS:

  • Whitelisting file: Only optimize files mentioned.

  • Blacklist file: Optimize all but these files.

Support

If you find any problems or bugs, please open a new issue.

Ref

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago