CommonJS modules address all these problems by adding explicit requiring and exporting, allowing you to quickly see inter-dependencies between files.
The module format consists of two things:
- Loading in files using
- Explicitly exposing properties using
I’ve been working with Josh Peek to integrate CommonJS modules into the library behind Rail’s asset pipelining, Sprockets. The result is the gem sprockets-commonjs. Simply add the gem to your Gemfile, and you’re good to go.
To specify that a script should be using CommonJS, just rename it from
module in the filename will be automatically wrapped up as CommonJS modules, and can subsequently use the require and export API.
It’s worth noting that modules won’t be evaluated on page load, only when they’re first required. Subsequent calls to
require() return a cached representation of whatever the module exported.
The gem doesn’t do any static analysis of the AST, so you may have to double up require calls. However I think the average use case is using require_tree to concatenate files, so it won’t be too much of a problem.