Reputation: 639
I'm very new to both bundling and RequireJS, but after doing some reading, it seems like bundling takes care of multiple requests to load several js files. Are there any other things I might be missing out on with regards to using RequireJS with bundling?
Upvotes: 2
Views: 597
Reputation: 2706
RequireJS is suitable and some might say necessary for projects that implement JavaScript heavy UIs.
Multiple requests aren't always a bad thing. What's most important is that the solution you choose scales as your project grows.
Upvotes: 2
Reputation: 21365
If you are going to use MVC 4, I would recommend you to use Bundles.
With bundles, you can define set of scripts that will be rendered on the browser depending if you are debugging or in production.
In order to detect if you are in debug or not, you have to change the following setting in the web.config file
<compilation debug="true">
If you are debugging, the Bundle engine will render all scripts as individual tags.
If you are NOT debugging, the Bundle engine will render just one tag and all the scripts referenced by the current bundle will be merged in an individual JS file
Also bundles use conventions in order to detect the script file that should be rendered
For example, in the case of JQuery
The JQuery script files look like:
jquery-1.8.2.js
jquery-1.8.2.min.js
When you declare your bundles, you do it like this:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
Note that you are not specifying the version nor the .min part of the file name, this is because those are special tokens that the bundle engine will take into consideration to choose the correct file
Whenever you reference your bundles using the specified path, the bundle engine will process them, and it will choose the correct scripts files, for example:
If debugging, the jquery-1.8.2.js file will be rendered
If not debugging, the jquery-1.8.2.min.js will be rendered
In this example, the bundle is named: ~/bundles/jquery
and you will referenced on your pages like this:
@Scripts.Render("~/bundles/jquery")
If the bundle would have more scripts, the same rules apply to all of them.
Consider this bundle registration
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
If debugging this will be the rendered HTML
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
If not debugging, this will be rendered:
<script src="/bundles/jqueryval?v=6Fqs6ZHMM_nFyDgv5mxz89PzsVLAnRNKOhqrK-mI5yU1"></script>
And that link will contain all the script files merged causing the page to load faster since all the scripts will be downloaded by the browser in just one hit
For more information, Please see the ASP.NET 4.5 new features
Upvotes: 2