在构建过程中缩小后更新 javascript 引用

发布于 2024-07-13 01:40:23 字数 228 浏览 7 评论 0原文

缩小 JavaScript 引用后更新它们的最佳方法是什么(我使用 YUI 压缩机)?

我正在使用 ASP.NET mvc,并且在开发过程中每个文件中有一堆或 javascript 引用。 在构建时(使用 MSBuild),我将所有文件缩小为单个文件。 同时我想用单个引用替换所有引用。 这应该是一件很常见的事情,但我找不到一个很好的干净的自动化方法来做到这一点。

有人有主意吗? 谢谢

What are the best ways of updating javascript references after minifying them (I use YUI Compressor)?

I am using ASP.NET mvc and have a bunch or javascript references in each file during dev. At build time (using MSBuild) I minify all the files into single file. At the same time I want to replace all the references with the single reference. This shoudl be quite a common thing and yet I can't find a nice clean automated way to do it.

Anyone have any ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-07-20 01:40:23

我们使用自定义脚本管理器。 在我们的 msbuild 过程中,YUI Compressor 用于创建缩小版本,但也用于将多个较小的 .js 文件合并为一个较大的文件。 这减少了请求数量,从而减少了加载时间。 缩小版本和组合版本与可调试版本同时创建。

在运行时,控件调用脚本管理器来注册特定脚本,这些脚本通过其未最小化的名称来引用。 然后,脚本管理器将在 DEBUG 模式下包含这些脚本(尽管我们执行了一个技巧,将脚本包含在 HEAD 中,而不是像 ClientScriptManager 那样包含在正文中)。 在 RELEASE 模式下,脚本管理器将用缩小的对应脚本替换已知的某些脚本。 如果脚本组合成一个文件,则管理器会被多次调用,每次都会注册同一个组合的缩小文件。 有些文件是外部托管的:这也由脚本管理器处理。

缺点:

  • 关于缩小哪些文件的知识是重复的:脚本管理器和构建脚本都必须同步。
  • 添加新脚本时,它们不会自动缩小(我个人认为这是一个好点;我希望缩小成为一个选择)

顺便说一句:我们不使用 C# 端口在 MSBuild 中运行。 当我第一次尝试时,它有太多错误。 我们只是用 Exec 任务调用 YUI Compressor。

We use a custom script manager. In our msbuild process, YUI Compressor is used to create minified versions, but also to combine several smaller .js files into one larger. This reduces the number of requests and thus load time. The minified and combined versions are created side-by-side with the debuggable versions.

During runtime, the scriptmanager is called by controls to register specific scripts, which are referred to by their unminimized name. The script manager will then just include these scripts in DEBUG mode (although we perform a trick that includes scripts in the HEAD, not the body as the ClientScriptManager does). In RELEASE mode, the scriptmanager will replace certain scripts known to it by their minified counterparts. In the case of scripts that are combined into one file, the manager is called multiple times, each time registering the same combined, minified file. Some files are hosted externally: this is handled by the scriptmanager too.

Downsides:

  • the knowledge of what files are minified is duplicated: both the scriptmanager and the build script must be in sync.
  • When new scripts are added, they will not automatically be minified (I personally see this as a good point; I want minification to be a choice)

By the way: we don't use the C# port for running in MSBuild. It had too many bugs when I first tried it. We just call YUI Compressor with an Exec task.

绝不放开 2024-07-20 01:40:23

我想到了几个想法:

1) 利用发布的修补程序来允许 .js 文件的调试/智能感知版本:KB958502

这将允许您编写(尽管是一个)大型、完全可读的 js 文件,但请参考脚本 src 中可以生成的完整版本在构建期间。 hofix 使 VS 能够查找名为“-vsdoc.js”或“.debug.js”的文件来代替引用的文件(因此您在 src 属性中引用“/scripts/myscript.js”,但 IDE 会首先查找对于“/scripts/myscript-vsdoc.js”,然后是“/scripts/myscript.debug.js”,最后它将查找“/scripts/myscript.js” - 更多信息可以在 Visual Web 开发人员团队博客。2

) 其他人已在 他们编写的自定义脚本管理器,将在调试期间引用主版本,但在发布模式下将请求最小化版本。

3)我确信我在某处读过,虽然我现在找不到它,关于某人编写了自己的 HTML 帮助器扩展方法,该方法将根据 buid 模式适当地生成脚本调用 - 与 Rick 的过程类似想法,但允许您以更“通常”的方式工作,尽管我不确定它与智能感知的效果如何。

我目前倾向于 1 - 使用类似于 Nick Berardi 的流程: 如何创建 YUI 压缩机 MSBuild 任务来生成最小化的 js。

A couple of ideas spring to mind:

1) Take advantage of the hotfix released to allow debug/intellisense versions of .js files: KB958502

This will allow you to write (albeit one) large, fully readable js file, but refer to the full version in your script src, that can be generated during the build. The hofix enables VS to find files named "-vsdoc.js" or ".debug.js" in place of the referenced files (so you reference "/scripts/myscript.js" in your src attribute, but the IDE will first look for "/scripts/myscript-vsdoc.js", then "/scripts/myscript.debug.js", and finally it will look for "/scripts/myscript.js" - a bit more info can be found on the Visual Web Developer team blog.

2) Others have posted on an custom script managers they wrote, that would reference the main version during debug, but in release mode would request the minimized version.

3) I'm sure I read somewhere, although I can't find it at the moment, about someone who had written their own HTML helper extension method, that would generate the script call appropriately depending on the buid mode - similar process to Rick's idea, but allowing you to work in a more "usual" way, although I'm not sure how well it played out with intellisense.

I'm leaning towards 1 at the moment - using a process similar to Nick Berardi's here: How to create a YUI Compressor MSBuild Task to generate the minimised js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文