CommonJS for Ruby on Rails
#= require and
window.Global mess, come under the cut.
Alternatives at hand
If you’d like to know other people’s opinion, explore the limitations of the AMD approach and learn about AMD advantages over CommonJS modules.
CommonJS by Alex MacCaw
As CommonJS is simply a specification, we have a bunch of different implementations. There is also a number of Ruby-based solutions, but I’ll promote one of them—sprockets-commonjs. Yep, it’s based on Sprockets, so don’t forget to install it unless you are riding Ruby on Rails v3.1 or higher.
The rest of this article is the CommonJS quick-start guide. It may be helpful if you’re not satisfied with the official documentation.
Starting with CommonJS
Go to Gemfile and add the following command.
If you are not using Ruby on Rails v3.1 or higher, you may also need this command.
module. For example,
widget.module.js.coffee. Now, you get two new keywords.
require()loads modules necessary for the current keyword.
module.exportsspecifies what should be returned if another module will require this keyword.
Let’s assume we have our
widget.module.js.coffee file inside of
To use it in another module (for example,
we just need to use the following command.
We can also use a dot.
An absolute path can be used, as well.
module extension wraps your code with this command.
This callback is executed only if the module is required somewhere. That’s why you can no longer worry about the correct
#= require chain. A single
#= require_tree ./modules in
application.js would be enough.
require is the only global variable exposed by
sprockets-commonjs. Except the module definition, it also allows for requiring modules from non-modules. In this case, don’t forget about the
#= require precedence.