A simple tool to package many javascript files into one.
- CommonJS module specification (like NodeJS's
require()
) - Control which files to include
- Circular
require()
support - Relative module paths support
- Great for structuring client-side code
- Optional minimization
First, define a build script in the root of your JS source folder that includes all of your dependencies.
#!/usr/bin/env node
// build_app.js
var dependencies = [
// the entrypoint, a single file.
{name:"__main__", path:"./main.js"},
// a library from NPM
{name:"underscore", path:"./node_modules/underscore/underscore.js"},
// a whole directory, recursively.
{name:"lib", path:"./lib"},
];
// package all the files into a string
var demodule = require("demodule");
demodule(dependencies, {minify: false, debug:true, output:"build/app.js"});
Then, use require()
in your code to import modules.
// main.js
var rand = require("lib/rand");
console.log(rand.randId(12));
Check out the full example.
How does this compare to Browserify?
Browserify has a lot of magic to bring node.js server-side code over to the browser environment.
This is a much simpler tool that gives you more control.
For example, if two NPM libraries import different versions of another library, Browserify handles that by automatically packaging all of them.
This tool doesn't.
Why isn't there a command-line tool?
You need to declare the dependencies in a file anyways, so you might as well edit the build script.
How do I add dependencies from NPM?
Just call npm install packageName
, or add it into your package.json
's dependency list as normal.
Then, find the javascript files (or folders) you want to include and add them to your build script.
This tool doesn't automatically add sub-dependencies from NPM libraries -- you need to locate them yourself.
Requires NodeJS.
See the example app for usage.