JS Bundler

Simple frontend JavaScript code bundler and testing framework.

It uses the Mocha test runner and Chai assert library.

Usage

  • Create a file to hold your bundled source code. It can have any file name, for example hello.js (could be foo).
  • Set the file of the hello.js file to js-bundler.boomla.net/v1 or whatever your install location is.
  • Add your main code in hello.js, for example:

    let hello = require('/hello.js');
    console.log(hello.sum(1, 2));
    
  • Create a module by spreading your code across children of the hello.js file, for example hello.js/sum.js could hold the sum() function. File names must end in .js and not _test.js to be included in the bundle.

  • Within the module files, use the CommonJS module format. Anything the module exports shall be assigned to module.exports or exports, for example:

    let sum = function(a, b) {
        return a + b;
    };
    module.exports.sum = sum;
    // or exports.sum = sum;
    

The only difference to CommonJS modules is that you can spread your code across multiple files, and not the file itself but its parent need to be imported.

  • You can also simply use exports.sum = sum.
  • Make sure to NOT reassign the module.exports variable, simply set its properties.
  • Require modules with let hello = require('/hello.js');. You must specify the full path relative to the domain.
  • The /hello.js file can import any module, not only itself.
  • To test the sum() function stored in /hello.js/sum.js, place the test code in /hello.js/sum_test.js (the important part is that it ends in _test.js). Files ending in _test.js are only included in the test bundle.
  • The file names within the module, like /hello.js/sum.js bear no significance except for the .js and _test.js ending. They are only for keeping order.
  • Test files will be part of the same namespace as your module code within your bundle, so you don’t need to require modules within tests.
  • To get the bundle, visit /hello.js.
  • To run the tests, visit /hello.js?test=1.
  • Currently, modules can not require other modules.
  • To keep files in nice order, you may want to sort them as desired. Also, you may want to create files like --------VIEWS-------- as visual separators.
  • Strict mode is enabled by default ('use strict';). To disable it, set the useStrict int32 attribute of the /hello.js file to 0.

Demo

Look at the source of the /hello file for an example.

The bundled code is available at /hello/hello.js.

The tests can be run by visiting /hello/hello.js?test=1.

Live results: