asbundle
A minimalistic CommonJS bundler.
This module is a perfect ascjs companion to create CommonJS bundles.
Passing a single ESM/CJS source file as path name, it will produce a lightweight, optimized, and minifier friendly bundle,
to consume right away without needing global require
or runtime discovered CommonJS dependencies.
Goals
- be as simple as possible, but not simpler
- make creation of small modules written in ESM a no brainer
- enable
.js
files as ESM everywhere, following a simple folder convention - produce files compatible with most common bundlers and tools (Babel, Webpack, UglifyJS, etc)
Example of a basic module based on ascjs and asbundle.
Non-Goals
- replace Babel, Webpack, Rollup, or any other tool. Let them do complicated things when needed
- transpile anything else than ESM import/export declarations
How to
You can use asbundle as binary utility or as module.
npm install -g asbundle # to see what you can do asbundle --help
As executable, you can use asbundle to output, or save, a bundle entry point.
asbundle source-file.js # writes bundle contents to STDOUT asbundle source-file.js bundle.js # outputs to bundle.js
As module, you can require it and use it to obtain a bundle string.
const asbundle = ; ;
Features
- extremely lightweight, based on babylon for performance and reliability
- it uses ascjs to automatically transform, when needed, ES2015+ modules into CommonJS code
- understands both relative files and installed packages too (based on
require.resolve(...)
) - reproduces a modern and minimalistic CommonJS environments ideal for browsers
- compatible with Babel
__esModule
and.default
convention
Constrains
- same constrains of ascjs
- Node core modules are not brought to the bundle, if a module cannot be resolved as file name it throws
Example
This module can transform main.js
entry file via asbundle main.js out.js
:
// main.js;const val = 123; { console;};; // module.jsconst a = 1 b = 2; { console;};
into the following bundle:
// out.js => 266 bytes minified & gzipped { { return cachei || ; } { var exports = {} module = exports: exports; modulesi; return cachei = moduleexports; } var main = ; return main__esModule ? maindefault : main;} {// main.js'use strict';const func = m__esModule ? mdefault : m;const a b = ;const val = 123; { console;}Objectdefault = test;exportsfunc = func;exportsval = val;} {// module.js'use strict';const a = 1 b = 2;exportsa = a;exportsb = b;Object { console;};};
The main module is returned and executed as default entry so it becomes easy to publish as global variable for Web purposes too.
Add a const myModule =
prefix to the bundled code and use it right away.