bdsl-webpack-plugin
A webpack plugin that automates generation of the differential script loading with browserslist and browserslist-useragent-regexp.
- 🦔 Declare environments in
.browserslistrc
config like this:
defaults
[modern]
last 2 versions and last 1 year and not safari 12.1
[actual]
last 2 years and not last 2 versions
- 📝 Create
webpack.config.js
for multiple outputs:
{ return name: env /* ... */ module: rules: test: /\.js$/ exclude: /node_modules/ loader: 'babel-loader' options: cacheDirectory: true presets: '@babel/preset-env' modules: false useBuiltIns: 'usage' corejs: 3 plugins: /* ... */ plugins: template: 'src/index.html' inject: 'head' ;}
- 🦄 Add
bdsl-webpack-plugin
:
const BdslWebpackPlugin getBrowserslistQueries getBrowserslistEnvList } = ; { return name: env /* ... */ module: rules: test: /\.js$/ exclude: /node_modules/ loader: 'babel-loader' options: cacheDirectory: true presets: '@babel/preset-env' /* ... */ targets: plugins: /* ... */ plugins: /* ... */ env ;} moduleexports = ... undefined // to use default .browserslistrc queries;
- 🎉 Done! Now
index.html
will contain differential script loading:
Example
Here you can see complete webpack.config.js
example.
Install
npm i -D bdsl-webpack-plugin# or yarn add -D bdsl-webpack-plugin
⚠️ Before you start ⚠️
bdsl-webpack-plugin
captures scripts only from<head>
section, so withhtml-webpack-plugin
you should useinject: 'head'
option;- By default scripts are loaded asynchronously and executed in "as they defined" order. To execute script in "load-first" order you should add
async
attribute to<script>
tag. For that you can usescript-ext-html-webpack-plugin
; defer
scripts are not supported, so you can use libraries likewhen-dom-ready
to bootstrap code whenDOM
ready;- Webpack configs must be in modern to legacy browser order, e.g.
['modern', 'actual', 'legacy']
; bdsl-webpack-plugin
also definesprocess.env.BDSL_ENV
variable with bundle's environment.
Why?
There is a differential script loading with module/nomodule trick, for this you can use webpack-module-nomodule-plugin
. But browsers that support type=module
already have new JS-features with different level of support. For example: optional chaining operator (for comparison browsers with type=module
support).
Plugin options
Option | Type | Default | Description |
---|---|---|---|
isModule | boolean |
— | Use type=module support check instead of RegExp. Should be used only on certain build. |
browsers | string \| string[] |
— | Manually provide a browserslist query (or an array of queries). It overrides the browserslist configuration specified in your project. |
env | string |
— | When multiple browserslist environments are specified, pick the config belonging to this environment. |
ignorePatch | boolean |
true |
Ignore the difference in patch browser numbers. |
ignoreMinor | boolean |
false |
Ignore the difference in minor browser versions. |
allowHigherVersions | boolean |
true |
For all browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. |
allowZeroSubverions | boolean |
true |
Ignore match of patch or patch and minor, if they are 0. |
withStylesheets | boolean |
false |
Enable differential stylesheets loading. |
unsafeUseDocumentWrite | boolean |
false |
Use document.write() to inject <script> . This variant supports defer scripts, but some browsers can restrict document.write() calls. |
JS API
Read docs here.
Examples
- Basic
isModule
option with@babel/preset-modules
- Differential stylesheet loading
- Transpile dependencies
unsafeUseDocumentWrite
option- Differential serving with
SsrBdslWebpackPlugin
BdslBuilder
API with@loadable/server
Metrics
You can get speed metrics from any site using devtool from this repo.
- Clone repo:
git clone git@github.com:TrigenSoftware/bdsl-webpack-plugin.git
- Install dependencies:
yarn
- Now you can run script:
yarn measure \"https://nodsl.site.com/" \"https://site.com/"
Output example
https://nodsl.site.com/ Average time: 1s 303msFastest time: 1s 203msSlowest time: 2s 432msEncoded size: 292 kBDecoded size: 1.08 MB https://site.com/ Average time: 1s 274msFastest time: 1s 140msSlowest time: 2s 284msEncoded size: 218 kBDecoded size: 806 kB
Parameters
Environment variables:
MEASURE_TIMES - number of site measurements, 10 by default
Options:
--good3g - enable "Good 3G" network preset
--regular4g - enable "Regular 4G" network preset
--cache - enable resource caching