12
votes

Is there a way to use the bundle functionality of ASP.NET MVC4 and the JavaScript module dependency feature of require.js

I want to load all my modules with ASP.NET MVC4 bundle features and use require.js to organize the relationships between them.

The problem is that require.js doesn't find the needed dependencies while all the modules are loaded via bundles.

I don't want require.js to go and try to load files asynchronous I just want it to use already loaded modules via bundles.

What should the main file look like in this case.

I Have the following structure in _Layout.chtml

@Scripts.Render("~/bundles/jquery",
                "~/bundles/jqueryval",
                "~/bundles/jslibs",
                "~/Scripts/require.js",
                "~/bundles/tjProductivity",
                "~/Scripts/tjpApp/main.js"
                )

@RenderSection("scripts", required: false)

And then in each scripts section I want to load all modules that will be used for that page via Bundle.config but let require.js manage dependencies between them.

2
Why not just use RequierJs and "bundle" scripts with r.js?user20140268

2 Answers

4
votes

It's possible, use almond.js, from the creator of RequireJS.

The only shortcoming is that you have to always add the module names on your modules...

0
votes

A good reference on integrating requirejs and mvc: http://www.codeproject.com/Articles/614507/Modular-Javascript-Using-Require-Js and http://www.stefanprodan.eu/2012/09/intro-requirejs-for-asp-net-mvc/ - note that it doesn't using bundling.

This article shows you how to integrate RequireJS in a ASP.NET MVC 4 project and structure the JavaScript code in such a manner that any C# programmer can understand and use it without advanced js programming skills.