With sprockets:
//= require
...in application.js seems to add a <script src=...>
tag to the head of my HTML. However, with browserify-rails
:
window.mymodule = require('mymodule'); // mymodule is a dependency in package.json
I don't see a <script>
tag for mymodule
. However I AM able to use code defined inside mymodule
. What is browserify doing here?
In short, Browserify is wrapping each module inside of a function, giving the module its own scope. This keeps the things you do not export private to the module. Each of these functions is stored in a map so they can be looked up later on require
.
This is a pretty good article explaining, at a high level, how it all works: http://benclinkinbeard.com/posts/how-browserify-works/
Sprockets just includes everything in the manifest in the global scope. In development each script's file is included. In production the scripts are all concatenated and minified, but it's all global.