Splittable Bundler
Splittable is a JavaScript module bundler designed for efficient code splitting, optimized bundle sizes, and dead code elimination, leveraging Closure Compiler, Babel, and Browserify under the hood. Currently at version 4.0.0, the package appears to be abandoned, with its last known publication several years ago, indicating no ongoing development or maintenance. It differentiates itself by offering a "zero-configuration" approach to advanced optimizations, aiming to produce smaller code than contemporary alternatives like Webpack and Rollup (though Rollup at the time lacked code splitting and direct CommonJS support). It supports both ES6 and CommonJS modules (with some caveats) and includes experimental JSX support. While it aims for simplicity, its reliance on Java for Closure Compiler and specific Babel configurations are key operational considerations.
Common errors
-
Compilation failed
cause A general error indicating issues during the bundling process, potentially due to incorrect configuration, source code errors, or underlying Closure Compiler failures.fixCheck the console for any detailed error messages or warnings provided by Splittable or Closure Compiler. Ensure source paths are correct and all dependencies (like Java) are properly set up. Enable `warnings: true` in options for more verbose output. -
java.lang.NoClassDefFoundError: com/google/javascript/jscomp/CommandLineRunner
cause This error indicates that the Java runtime cannot find the Closure Compiler's main class, typically because the Closure Compiler JAR is missing or not accessible to Splittable. This usually points to an issue with the Java installation or Splittable's internal dependency resolution for Closure Compiler.fixVerify your Java installation and ensure it's correctly configured in your system's PATH. If `splittable` was globally installed, try reinstalling it. Check for any environment variables that `splittable` might use to locate the Closure Compiler JAR. -
ReferenceError: Promise is not defined
cause The `System.import` polyfill used by Splittable's generated bundles requires a global `Promise` object, which is missing in older JavaScript environments (e.g., IE11).fixInclude a Promise polyfill in your HTML before loading Splittable's `_base.js` bundle. A common choice is PJs or a polyfill from `core-js`. -
Error: Failed to load module 'module/path': 404 Not Found
cause The `System.import` loader could not find the requested module bundle at the specified path. This often happens if `System.baseURL` is incorrect or the module path itself is misspelled.fixDouble-check that `System.baseURL` is correctly set to the root directory where your Splittable bundles (including `_base.js`) are deployed. Verify the exact `module/path` string matches an entry module configured during bundling.
Warnings
- breaking Splittable relies on Java for the Google Closure Compiler. If Java is not installed or correctly configured in the system's PATH, Splittable will fail to run.
- gotcha When using Babel with Splittable, it is highly recommended to disable ES6 module transpilation (e.g., `modules: false` in `babel-preset-es2015` or similar presets). Splittable performs its own ES6 module compilation at a later stage, and pre-transpiling modules can lead to "very poor" (inefficient or bloated) generated code.
- gotcha The `System.import` polyfill provided by Splittable's `_base.js` bundle requires a global `Promise` implementation. For older browsers that lack native Promise support, you must supply your own Promise polyfill (e.g., PJS or core-js).
- gotcha When loading bundles with `System.import`, the `System.baseURL` global variable must be explicitly set to the deployed directory of your Splittable bundles. Failing to do so will prevent the loader from finding and importing modules.
- gotcha Splittable supports CommonJS modules but with 'some caveats'. Specifically, cyclic dependencies are not supported and some Node.js modules may not be compatible due to environment differences. Also, `require("module-name")` must be used for NPM modules, even inside ES6 modules.
Install
-
npm install splittable -
yarn add splittable -
pnpm add splittable
Imports
- splittable
import splittable from 'splittable';
const splittable = require('splittable');
Quickstart
const splittable = require('splittable');
const path = require('path');
const fs = require('fs');
// Create a dummy project structure for the example
const tempDir = path.join(__dirname, 'temp_splittable_project');
const libDir = path.join(tempDir, 'lib');
const outDir = path.join(tempDir, 'out');
fs.mkdirSync(libDir, { recursive: true });
fs.mkdirSync(outDir, { recursive: true });
fs.writeFileSync(path.join(libDir, 'a.js'), 'export function sayHello() { console.log("Hello from A!"); }');
fs.writeFileSync(path.join(libDir, 'b.js'), 'import { sayHello } from "./a"; sayHello(); console.log("From B");');
console.log('Starting Splittable bundling...');
splittable({
modules: [path.join(libDir, 'a.js'), path.join(libDir, 'b.js')],
writeTo: outDir,
warnings: true // Enable Closure Compiler warnings
})
.then(function(info) {
if (info.warnings) {
console.warn('Compilation successful with warnings:', info.warnings);
} else {
console.log('Compilation successful.');
}
console.log('Bundles written to:', outDir);
})
.catch(function(reason) {
console.error('Compilation failed:', reason);
})
.finally(() => {
// Clean up dummy project (optional)
// fs.rmSync(tempDir, { recursive: true, force: true });
// console.log('Cleaned up temporary project directory.');
});