jwhitley / requirejs-rails

RequireJS support for your Rails 3 or 4 application
MIT License
592 stars 201 forks source link

Question about including `require.js` as a dependency #183

Closed rafaelrinaldi closed 8 years ago

rafaelrinaldi commented 10 years ago

On my current setup we have a bundled file called vendor.js with all vendor scripts minified. This file is generated via r.js as well. Right now when I load my application it makes 3 requests:

I want to know how to to add require.js to the bundled JavaScript file generated by r.js so I can reduce an HTTP request.

rafaelrinaldi commented 10 years ago

It looks like there's is an option that is not available via requirejs-rails. The gem automatically generates a script tag with require.js as source and will reference the main file using a data attribute by default.

Perhaps this should be more flexible. A flag in the configuration file specifying whether or not RequireJS should include itself might be a quick solution ("quick" as in this option already exists).

Any thoughts?

rafaelrinaldi commented 10 years ago

Any ideas here, guys? @carsomyr @jwhitley

wuservices commented 10 years ago

I was thinking about this too. I was considering having some bootstrapper.js that includes some async queue setup code kind of like var _gaq = _gaq || []; and stuff like that, but it could include the RequireJS config and require.js as well. This would allow the config to be minified and cached and as a bonus it would also obfuscate it so that everybody doesn't see all my modules in the source of every page.

rafaelrinaldi commented 10 years ago

@wuservices Yeah, that's exactly what I want.

In an Node/NPM based environment, usually on your main file, if you set require itself as a dependency, r.js will include it on the built file. I've tried the same thing without any success though.

rafaelrinaldi commented 10 years ago

@wuservices Want to talk some day to think about an implementation that solves this issue? Would be nice to have your help on this.

wuservices commented 10 years ago

I'd be happy to some time, but first I was curious to hear a little more about your use case. As I'm re-reading what you said, it sounds like we might be trying to do similar but different things.

I'm trying to figure out how to bundle the config into a JS file with require.js and some other scripts. I think the only change needed for this is to expose a helper that lets us output the build config. That could be added to requirejs_helper.rb by factoring out the logic to generate the first script tag within requirejs_include_tag. Then, there's no reason you need to use the requirejs_include_tag anywhere.

The link you referenced seems to be almost possible here too, but the issue again is getting the config. If we what I just mentioned so we're able to get the config, there's no reason you couldn't just create a module with the config, require.js and whatever else you want I think.

On the other hand, do you even need require.js on the page? If you really just want require.js, vendor.js, and application.js, you could just use almond if you don't need dynamic script loading and that would be even more optimal.

While in some situations combining everything like this could be better, I'd caution against this because it might increase your page's load time. If you overall goal is to have the fastest perceived load performance, you're probably better off loading a small require.js library (possibly with config in there), then having RequireJS asynchronously load all other resources. This way there's no slow, blocking script load on the page. Of course with this approach your script does execute later, so if your page does need JavaScript to initialize to work at all, the blocking approach might be better. Otherwise, you actually will hit DOM ready faster with require.js loading everything asynchronously.

rafaelrinaldi commented 8 years ago

@wuservices Thanks for helping me out with this. Closing this since I'm not using this gem anymore.