Skip to content Skip to sidebar Skip to footer

Bundling And Minifying Modular Javascript (requirejs / Amd) With Asp.net Mvc

Does anyone have any experience with or know any good solutions for bundling and minifying modular JavaScript like RequireJS / AMD in an ASP.NET MVC project? Is the best way to use

Solution 1:

I think the problem you'll hit is if you used anonymous defines. If you want a combined/bundled script file that contains all your defines, you'll have to name them.

eg.

define("someModule", ["jquery", "ko"], function($,ko) { ... });

instead of

define(["jquery", "ko"], function($,ko) { ... });

If you use the file names as the module names, you'll be able to load them asynchronously (not bundled) as well as preloaded (bundled). That way you can work in debug mode as well as release mode without having to change your requires.

I have no experience with the RequireJS optimizer to know if it takes care of anonymous defines or not.

UPDATE:

I recently had to do this and one of the problems I ran into was the data-main attribute of the script tag loading require.js. Since the main.js file had dependencies on the bundled modules, they need to be loaded before main.js but after require.js. So I had to ditch data-main and load that file (unbundled) last.

from

<scriptsrc="../JS/require-v2.1.2.js"type="text/javascript"data-main="main.js"></script>

to

<scriptsrc="../JS/require-v2.1.2.js"type="text/javascript"></script>
<%: System.Web.Optimization.Scripts.Render("~/bundles/signup") %>
<scriptsrc="main.js"type="text/javascript"></script>

I haven't looked, but if the bundle configuration could ensure main.js is last, then wouldn't even need that last script tag.

Solution 2:

These are the steps to get RequireJS bundled with main.js. You can get down to one line in the <head> tag. This does not address the issue with anonymous defines.

HTML

<head runat="server">
    <title></title>
    <%: System.Web.Optimization.Scripts.Render("~/bundles/scripts") %>
</head>

BundleConfig.cs

using System.Web;
using System.Web.Optimization;

publicclassBundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862publicstaticvoidRegisterBundles(BundleCollection bundles){
        bundles.Add(newScriptBundle("~/bundles/scripts").Include(
                    "~/scripts/libs/require.js",
                    "~/scripts/main.js"));
   }
}

main.js must work without data-main (I needed to set baseUrl)

require.config({
    baseUrl: "scripts"
});

Solution 3:

I'm not sure if this is an acceptable solution or not, but Visual Studio 2012 has a NuGet package (Microsoft.Web.Optimization) which supports native minification and bundling. I'm not sure if it's available for 2010

It's one line of code in application_start

Microsoft.Web.Optimization.BundleTable.Bundles.EnableDefaultBundles();

http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetandvisualstudio_topic5

Post a Comment for "Bundling And Minifying Modular Javascript (requirejs / Amd) With Asp.net Mvc"