My Knowledge Wiki
  • Introduction
  • My Stack
  • Sharing
  • Work
    • How I Want to Work
    • Hiring Process
  • Apps & Tools
    • Raycast
      • Extensions
    • Visual Studio Code
      • Theme
      • Extensions
    • Google Chrome
      • Theme
      • Extensions
    • iTerm 2
      • Theme
    • Fish
      • Plugins
    • Docker
      • Volumes
        • Mounting
    • Web Bundlers
      • Webpack
  • DevOps
    • Databases
      • Estimating Connections
    • Kubernetes
      • Terminology
      • kubectl Cheatsheet
      • Best Practices
      • Application Health Checks
      • Upgrades
      • Troubleshooting
      • Ruby on Rails
  • Awesome Products
  • Engineering Management
    • Overview
  • Software Architecture
    • Microservices
    • Event sourcing
    • Serverless
    • Centralized Authentication
  • Talks
    • Software Architecture
      • An Insider's Look at the Technology That Powers Shopify
      • Building Extensible Platforms
  • Documentation
  • Machine Learning
    • Terminology
    • Regression
    • Overfitting
  • Programming Languages
    • Constructs
    • Go
      • Syntax
    • Ruby
      • Ruby on Rails
        • Setup
        • Gotchas
        • Helpers
        • Libraries
        • Routing
        • Status Code Symbols
      • Debugging
        • Byebug
          • Cheatsheet
      • Libraries
      • Lazy Enumerators
      • Snippets
  • Version Control
    • Git
      • Conventions
        • Conventional Commits
        • gitmoji
  • Education
    • Programming & Computer Science
      • Courses
    • Design
      • Courses and Books
    • Frontend Development
  • HTTP
    • Status Codes
  • Design
    • Icons
      • Icon Sets
  • Arabic Content | محتوى عربي
    • Learning
      • مصادر لتعلم البرمجة و علوم الحاسب
    • Advice
      • إدارة المطورين: نبذة
      • نصائح تصحيح معتقدات خاطئة أمنيات للمبرمجين الجدد
  • Biology
    • Species
      • Ants
Powered by GitBook
On this page
  1. Apps & Tools
  2. Web Bundlers

Webpack

PreviousWeb BundlersNextDevOps

Last updated 6 years ago

Tips

Be very careful with importing large libraries

Trying to compile large libraries (like react-plotly or PDF libraries) can take your Webpack compile from seconds to 10 minutes+. If a package is slowing down your compile, consider using a CDN version. We simply used script tags, but there are Webpack plugins that can help with that too:

  • .

  • .

Try to find a webpack plugin for your dependencies

Just importing packages like or bring in a lot of bloat that you probably don’t need. Try to import what you need only, or better yet find a webpack plugin that removes the unused things from your bundle, because . As one example, there’s that removes a lot of the unnecessary bloat added by Moment.js.

Google actually has a listing some common problematic dependencies.

Inspect your bundle with Webpack bundle analyzer

Add es-check to your CI pipeline early on

Transpiling a node_module

In your webpack config, your babel loader’s exclude field probably looks like this: /node_modules/ . We need to make a regex that excludes node_modules except the specific ones that should be transpiled:

// Exclude all node modules except hex-rgb and another-package
/node_modules\/(?![hex\-rgb|another\-package])/

And once again, this might not be a good solution for large packages as it can drastically slow your build time and you might want to switch to a CDN version instead.

Use Browserslist to specify your target browsers

> 1%
ie >= 8

This simple configuration targets browsers with usage more than 1% global usage, and IE versions 8 and above.

Use babel.config.js over .babelrc (for Babel ≥ 7.0)

.babelrc can be overridden by another .babelrc belonging to a node_module that you’re transpiling and that can lead to all sorts of weird issues.

Make your webpack-dev-server logging output friendlier

devServer: {
  noInfo: true,
  stats: 'minimal'
}

Note: The first configuration is meant to be combined with Webpack Bundle Analyzer, as it suppresses console output for things related to your bundle that Webpack Bundle Analyzer already shows. If you’re not using Webpack Bundle Analyzer, don’t apply the first step.

is extremely helpful to see what exactly is going into your bundle. In the screenshot above, you’ll notice that moment.js has lots of localization files that your app probably doesn’t need. Webpack Bundle Analyzer can help you easily spot these issues.

will help you find out which ES version your bundle is using, it’s super useful to find out if you’re somehow suddenly not producing ES5 anymore. Even if you’re using Babel and browserslist, you might be importing a node module that’s not even meant to be used in browsers, or even a package that’s not being distributed as ES5. Add es-check to your continuous integration pipeline early on and it should help you find out if your bundle ever stops working with ES5, and that’ll help you find which package is the culprit so you can then transpile it.

We had imported a very simple package called that’s not even meant for browsers and this tiny package made our bundle not ES5-compatible anymore. Such packages should go through Babel and be transpiled.

Follow from the babel-loader repo to stay up to date on how to handle cases like this.

lets you specify which browsers to transpile for.

Favor using babel.config.js to over .babelrc . If you want to transpile node_modules (which is now becoming a very common case with webapps), then you should use babel.config.js .

Change your to this

2. Add to get much less-verbose, friendlier, and more concise output.

Webpack Bundle Analyzer
es-check
hex-rgb
this issue
Browserslist
configure Babel
webpack-dev-server config
WebpackBar
webpack-cdn-plugin
dynamic-cdn-webpack-plugin
moment.js
lodash
selective imports don’t always work
a webpack plugin
nice repository