捆绑并用ASP.NET MVC污染减量模块化的JavaScript(RequireJS / AMD) [英] Bundling and minifying modular JavaScript (RequireJS / AMD) with ASP.NET MVC

查看:189
本文介绍了捆绑并用ASP.NET MVC污染减量模块化的JavaScript(RequireJS / AMD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何经验或知道在ASP.NET MVC项目捆绑和污染减量模块化的JavaScript像RequireJS / AMD什么好的解决办法?

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?

时使用 RequireJS优化最好的办法(或许在后生成的行动?) - 或有ASP.NET MVC的更好的东西在那里?

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.

如:

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

而不是

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

如果您使用的文件名作为模块名,你就可以异步加载它们的(非绑定),以及preloaded(捆绑)。你可以在调试模式下工作,以及释放模式,而无需改变你的这种方式需要。

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.

我跟RequireJS优化没有经验知道,如果它需要匿名定义照顾与否。

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

更新:

最近,我不得不这样做,我跑进一个问题是脚本代码加载require.js的数据主要属性。由于main.js文件对捆绑模块的依赖关系,他们需要main.js之前但require.js之后被加载。所以我不得不抛弃数据主加载该文件(分拆)最后一次。

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.

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

<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>

我没有看过,但如果束配置可以确保main.js是最后的,那么甚至不需要去年脚本标记。

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

这篇关于捆绑并用ASP.NET MVC污染减量模块化的JavaScript(RequireJS / AMD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆