简体   繁体   中英

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 the RequireJS optimizer (perhaps in a post build action?) -- or is there something better out there for ASP.NET MVC?

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

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

to

<script src="../JS/require-v2.1.2.js" type="text/javascript"></script>
<%: System.Web.Optimization.Scripts.Render("~/bundles/signup") %>
<script src="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.

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;

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/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"
});

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

I know this is an old question, but you can take a look at RequireJS.NET Compressor (uses YUI compressor, not ASP.NET bundling - at least for the moment) which is part of RequireJS.NET NuGet package.

References:

Compressor - http://requirejsnet.veritech.io/compressor.html

General Documentation - http://requirejsnet.veritech.io/

Github Project - https://github.com/vtfuture/RequireJSDotNet

NuGet - https://www.nuget.org/packages/RequireJsNet/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM