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

@bit/bundler-splitter

v7.0.3

Published

bit-bundler plugin for splitting bundles up

Downloads

41

Readme

bit-bundler-splitter

Greenkeeper badge

bit-bundler plugin for splitting bundles up

This plugin helps slice and dice your application bundle into smaller parts which we refer to as bundle shards. The more common use case is to split out the vendor (3rd party) modules into a separate bundle. This is to maximize browsers' caching capabilities; generally speaking vendor bundles do not change frequently and browsers can cache them rather efficiently. Vendor bundles also tend to be larger than your more frequently changing application bundles, which generally translates to a reduction in traffic when properly cached by the browser.

Semantics

Bundle splitting works by defining matching rules for the modules you want in separate bundles. Any modules that match a rule become entry points for a new bundle. You can match modules by their filename, path, or any other piece of information available on them. The modules that are split into their own bundle actually bring their entire dependency tree, unless a dependency matches a rule in which case the dependency and its dependency tree become their own bundle. What bundle splitter ends up with is a tree of bundles with the corresponding modules that matched splitting rules at each node. And all corresponding module dependencies are tucked under each of the corresponding bundles.

The bundle splitter create two extra bundles - common and loader. The common bundle will contain all the modules that appear in more than one bundle as long as it does not match a slitting rule. This helps ensure that modules are not duplicated across different bundles. The other bundle that is created is called loader. This bundle constains the necessary bootstrapping to load bundles in the proper order in the browser. This is generally the file you want to use in your index.html, unless you have a custom setup for loading your bundles.

bit-bundler-splitter uses roolio to provide a flexible way to configure matching rules that control how bundles are split. More on this in the examples section.

Install

$ npm install --save-dev @bit/bundler-splitter

API

splitBundle( config: object )

bit-bundler-splitter exports a function that takes the following configuration.

  • @param {object} options - Configuration options explained below.
    • match, matching rules which is how we configure how bundles are split.
    • dest, which is where the bundle is written to; dest can be a string file path or a stream.
    • name, which is the name of the bundle. Other plugins can access these shards by name if need be.

Examples

This example shows a basic bit-bundler setup with bit-bundler-splitter splitting out vendor bundles. If a module bower_components or node_modules in its path then its pushed to the vendor bundle. We are also splitting out modules whose path contains src/renderer and writing that out as a separate bundle.

The code is available here.

var Bitbundler = require("@bit/bundler");

Bitbundler.bundle({
  src: "src/main.js",
  dest: "dist/main.js"
}, {
  loader: [
    "@bit/loader-babel"
  ],
  bundler: [
    ["@bit/bundler-splitter", [
      { name: "vendor", dest: "dest/vendor.js", match: ["/bower_components/", "/node_modules/"] },
      { name: "renderer", dest: "dest/renderer.js", match: "/src/renderer/" } ]
    ]
  ]
});

can run it with:

$ npm install
$ npm run build
$ cat dist/vendor.js dist/renderer.js dist/main.js | node

Matching rules

So bundles are split by matching rules and these rules can be a simple string, regex, or objects with properties matching different information found in modules. When you specify a string or a regex the splitter will match the path of the bundle. String patterns are converted to regexp internally, so you can definitely use regex syntax in the strings you define.

Below are different matching rule examples.

You can specify a regex to match modules with a path that contains "common".

splitBundle({
  match: {
    path: /common/
  }
});

You can use the short hand with regex

splitBundle({
  match: /common/
});

Or you can use the short hand with string

splitBundle({
  match: "common"
});

You can match modules with names that only has alpha numeric characters. e.g. "jquery".

splitBundle({
  match: {
    name: /^\w+/
  }
});

Or match modules with name three:

splitBundle({
  match: {
    name: "three"
  }
});

You can also specify multiple matching properties. In this case only module with name "jquery" that have "views" in its file path will match this.

splitBundle({
  match: {
    name: "jquery",
    path: /views/
  }
});

Now, let's specify a bundle name and a destination where the bundle is going to be written to.

splitBundle({
  name: "vendor",
  dest: "dest/vendor.js",
  match: {
    name: "jquery",
    path: /views/
  }
});

Destination can alternatively be a stream.

splitBundle({
  name: "vendor",
  dest: process.stdout,
  match: {
    name: "jquery",
    path: /views/
  }
});

And you can define multiple bundle splits...

splitBundle([
  { name: "vendor", dest: "dest/vendor.js", match: ["/bower_components/", "/node_modules/"] },
  { name: "renderer", dest: "dest/renderer.js", match: "/src/renderer/" }
]);