Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sprockets //=require and browserify-rails require()?

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?

like image 914
iftheshoefritz Avatar asked Jul 01 '26 00:07

iftheshoefritz


1 Answers

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.

like image 193
Todd Avatar answered Jul 02 '26 12:07

Todd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!