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

splittable

v4.0.0

Published

Module bundler with support for code splitting, ES6 & CommonJS modules.

Downloads

45

Readme

Build Status

Splittable

Splittable is a next-generation module bundler for JavaScript with support for

  • super simple code splitting.
  • ES6 modules.
  • CommonJS modules (with some caveats).
  • extremely efficient packing of modules.
  • dead code elimination (also sometimes called tree shaking).
  • smaller code for real world projects than all other known module bundlers.
  • JSX (Example)
  • NPM modules (See instructions)

Usage from the command line

Requires java for one dependency to run.

Installation

npm i -g splittable

Run

splittable path/to/module1.js path/to/module2.js

It is recommended to run splittable from the root directory of your project.

Options

  • write-to: Output directory for bundles. Default: ./out/
  • warnings: Whether to show closure compiler warnings. Default: false

Usage from JS


var splittable = require('splittable');

splittable({
  // Create bundles from 2 entry modules `./lib/a` and `./lib/b`.
  modules: ['./lib/a', './lib/b'],
  writeTo: 'out/',
}).then(function(info) {
  if (info.warnings) {
    console.warn(info.warnings);
    console.log('Compilation successful with warnings.');
  } else {
    console.log('Compilation successful.');
  }
}, function(reason) {
  console.error('Compilation failed', reason);
});

### Options

- `writeTo`: Output directory for bundles. Default: `./out/`
- `warnings`: Whether to show closure compiler warnings. Default: `false`
- `babel`: (Experimental, feedback appreciated) Allows specifying babel options. By default splittable also reads from your `.babelrc`. Please note, though. We highly recommend turning off transpilation of ES6 modules. Otherwise the generated code will be very poor (splittable compiles ES6 modules at a later stage).

#### Example

```js
splittable({
  modules: ['./test/module-regression/custom-babel-config.js'],
  writeTo: 'test-out/',
  babel: {
    presets: [
      ['es2015', { loose: true, modules: false /* IMPORTANT! */ }],
      'stage-0'
    ],
    plugins: [
      'transform-object-rest-spread',
      ['transform-react-jsx', { pragma:'h' }]
    ],
  }
})

Generated bundles

The above will write 3 files (plus sourcemaps) to the directory out.

  1. A bundle of ./lib/a and its dependencies.
  2. A bundle of ./lib/b and its dependencies.
  3. A bundle called _base.js that contains the shared dependencies of the entry modules.

Loading splittable bundles

By default bundles are generated into the out/ directory (can be overridden via writeTo option).

System.import

The generated _base.js bundle ships with a System.import polyfill. It can be used to load modules exposed as bundles via splittable. The System.import function returns a promise for an object that exposes the exports of the loaded module.

System.baseURL = '/path/to/bundles/';
System.import('module/path').then(function(module) {
  module.exportedFunction();
})

module/path must be a path to a module as used in your splittable command line config or JS calls. It is not a relative path with respect to the actual JS module you call this from.

System.baseURL = '/path/to/bundles/'; must be supplied, so the loader knows where to find the bundles. This should be the directory, where you deployed your splittable bundles.

Note: The polyfill requires promises. If you chose to use System.import and are targeting older browsers, please supply your own polyfill such as PJS.

Via async script tags

Loading splittable bundles is super straightforward with async script tags. You do not need to worry about the order in which they execute. Example:

<script async src="out/_base.js"></script>
<script async src="out/lib-a.js"></script>
<script async src="out/lib-b.js"></script>

For an example and advanced usage such as JS initiated loading see the sample.

Isn't this like

  • Rollup: Yes, but it supports code splitting, CommonJS modules, and significantly more aggressive tree shaking. File sizes should typically be much smaller.
  • Webpack: Webpack has way bigger scope, but could possibly use Splittable as a plugin. In general, Webpack will generate significantly less efficient and much bigger code. It does, however, support multiple layers of bundle dependencies which will often result in smaller portions of the application being downloaded in initial bundles. It also has way more features, though, and is more mature, so may still be a win.
  • Browserify: Similar to Webpack, browserify generates less efficient code and comes with an awesome ecosystem of plugins.

This section is for entertainment only. All of the above bundlers certainly have dozens of other features that makes them more useful than splittable and/or could use splittable as plugin to get the best of all worlds.

How does it work?

Splittable is a wrapper around both Browserify, Babel and Closure Compiler. It uses the former 2 to resolve modules and their dependencies, and then uses Closure Compiler for efficient compilation of code.

Splittable takes a list of entry modules as its input and then creates bundles for each of them, as well as an additional bundle with the share dependencies.

NPM modules

(May also apply to other common JS modules.

  • require("module-name") must be used to load NPM modules. This is even the case inside of ES6 modules.
  • Cyclic dependencies are not supported and cannot be supported by splittable.
  • Some node modules may not be compatible due to differences in the environment.

Possible improvements

  • Splittable currently pollutes the global scope with lots of symbols, so that they are visible across modules. This could be fixed with --rename_prefix_namespace at the trade off of slightly more verbose generated code.
  • Splittable only supports one layer of bundle hierarchy. This can lead to an extremely bloated base bundle. Multiple layers could be supported at the price of greater complexity in several dimensions.
  • Switch Closure Compiler to the JS-only version (cut Java dependency). This requires adding support for code splitting to the JS version.