Bundling and minifying modular JavaScript (RequireJS / AMD) with ASP.NET MVC

后端 未结 4 1336
轮回少年
轮回少年 2020-12-24 01:35

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

4条回答
  •  抹茶落季
    2020-12-24 02:23

    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

    
    

    to

    
    <%: System.Web.Optimization.Scripts.Render("~/bundles/signup") %>
    
    

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

提交回复
热议问题