npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-prebundle-plugin

v1.0.2

Published

A webpack plugin for prebundling your vendors and common code on the fly. Powered by esbuild.

Downloads

20

Readme

Webpack-prebundle-plugin

workflow

Introduction

Webpack-prebundle-plugin is a Webpack plugin for fast prebundling with esbuild. Prebunding vendors and common code can lower the number of modules Webpack need to build and boost build speed.

Webpack-prebundle-plugin use external internally and is a more simple and fast replacement for DLLPlugin. ESbuild can bundle thousands of modules in a few seconds so you don't need to build the DLL bundle in advance(Or commit the bundle in Git repo). The plugin prebundles right before the Webpack build start. Brings seamlessly developer experience.

Usage

The usage is three simple steps:

Step1: Install package:

yarn add webpack-prebundle-plugin

Step2: Init plugin in Webpack config:

const webpackPrebundlePlugin = require("webpack-prebundle-plugin")
const path = require("path")

module.exports = {
    plugins: [
        new webpackPrebundlePlugin({
            vendors: {
                entries: ['react', 'react-dom', 'antd'],
                output: path.resolve(__dirname, "../public/vendors.js")
            },
        })
    ]
}

Step3: Add vendors script tag in HTML(public/index.html in CRA template for example):

<head>
  <title>React App</title>
  <script src="%PUBLIC_URL%/vendors.js"></script>
</head>

Then you are done! Enjoy the build performance boost.

Example project

Example of webpack-prebundle-plugin with react/@redux/toolkit/antd

Config

vendors

{
  vendors: {
    entries: ['react', 'react-dom', 'antd'], // vendors entries,  similar to entries in webpack 
    output: path.resolve(__dirname, "../public/vendors.js"), // output file's path
    esbuildOptions: {
        // extra esbuildOptions, doc: https://esbuild.github.io/api/#simple-options
    }
  },
}

commons

{
  commons: [
    // a common code bundle
    {
      entry: path.resolve(__dirname, '../src/services/index.ts'), // common code entry file path
      output: path.resolve(__dirname, '../src/prebuilt/services/index.js'), // output file's path
      watch: true, // config watch mode, pass in true will watch on entries, you can pass in arrays of paths to force wath specific paths like watch: [path.resolve(__dirname, './foo')]
      esbuildOptions: {
            external: ['src/utils/Url'],
            // extra esbuildOptions, doc: https://esbuild.github.io/api/#simple-options
      },
   },
}

watch options is passed to chokidar. Please Reference chokidar watch paths config for more advanced watch option.

View complete option schema in Schema.json

Why build this plugin?

Webpack runs faster when it builds less modules. We can make Webpack build faster by reduce the number of modules, like externalize all dependencies from node_modules or prebundle vendors code with DLLPLugin. But both external and DLLPlugin have some inconveniences. External requires modify HTML template when upgrade dependency version. DLLPlugin need to rebuild every time when dependency change. And we need to commit JS files built with DLLPlugin to Git repo, which require more disk space.

Compare with External method, DLLPlugin is better for large-scale Web apps that use hundreds of deps. Not all deps are UMD bundled and can be externalized. So DLLPlugin is kind of a universal solution for externalize modules. The way DLLPlugin works, is bundling less frequently changed code ahead of time. That's what prebundle means. DLLPlugin works fine, but it has the defect metioned above.

As the raise of front-end tooling written with compile-to-native language. We can integrate tool like esbuild with Webpack for pre-bundling. ESbuild bundle is so fast that we can build vendors on the fly right before webpack build starts.

This plugin integrate esbuild into Webpack compile flow, and allow user to pre-bundle two categories of files.

1. vendors

Files in node_modules are basicly immutable. So we assume these files will not change during the build. Pre-bunding vendors code will drastically improve build speed becase the number of files in node_modules in huge.

2. commons

We may have some auto-generated files in project, like Pont code(api request code generated from swagger api definition). Or we may have large amount of common util code like i18n. These files will not change that frequently.

We can pre-bundle commons codes as well, and webpack-prebundle-plugin provide watch mode for common code. common bundle will be rebuild when watch path has file change event.

Use in production

This plugin use Webpack externals internally, so theoretically it will work fine in production. You need to define process.env.NODE_ENV with '"production"'in vendors.esbuildOptions.define to get minified prod version of your deps. And set minify to true. Example:

{
    vendors: {
        esbuildOptions: {
            minify: true,
            define: "process.env.NODE_ENV": '"production"',
        }
    }
}                

Caveats

Since esbuild support ES module standard in a more strict way, unlike Webpack, which has build-in polyfill for nodejs core modules(Webpack 4) and backward compatible with many historical design. Some module may have issue built with esbuild but works fine under Webpack. The issue varies. To solve this, get to know why that module is not a standard es module, and search for esbuild doc and github issue for help.

You can tweak esbuild options in vendors.esbuildOptions and commons.esbuildOptions.

Or you can use DLLPlugin with Webpack-prebundle-plugin all together. Let DLLPlugin deal with module that has es module compatibility issue.