在构建服务器上将 Google Closure 编译器与 MS Build 集成

发布于 2024-11-02 21:33:50 字数 355 浏览 0 评论 0 原文

我正在研究缩小 javascript 文件的方法,作为 CI 流程的一部分,以便我们可以在开发中使用未缩小的文件,并在部署到临时服务器和实时服务器时自动压缩它们。

这是针对 ASP.NET 站点的;我们使用 Hudson 作为构建服务器。

我对 Google Closure 编译器很感兴趣,并且遇到了这个 .Net MSBuild Google Closure 编译器Task,但似乎使用得不是很广泛。是否有更好的选项可与 MSBuild 一起使用,使用 Closure 或替代缩小工具?

I'm looking into ways of minifying javascript files as part of our CI process, so that we can use the un-minified files in development and have them automatically compressed when deployed to staging and live servers.

This is for an ASP.NET site; we use Hudson as a build server.

I'm intrigued by the Google Closure compiler, and I've come across this .Net MSBuild Google Closure Compiler Task, but it doesn't seem to be very widely used. Are there better options for use with MSBuild, using either Closure or alternative minification tools?

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

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

发布评论

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

评论(4

只为一人 2024-11-09 21:33:50

我们在基于 .NET 的项目中使用 Closure Compiler 一段时间了。

最初,我们使用一个简单的 MSBuild .proj 文件,它直接调用 Python 脚本。例如,我们将使用如下内容来制作 deps.js:

<PropertyGroup>
  <ScriptDirectory>yourprojectname</ScriptDirectory>
  <ClosureLibrary>closure</ClosureLibrary>
  <CalcDeps>$(ClosureLibrary)\bin\calcdeps.py</CalcDeps>
</PropertyGroup>

<Target Name="Deps">
  <Exec Command="$(CalcDeps) -o deps -p $(ScriptDirectory) -d $(ClosureLibrary) --output_file=$(ScriptDirectory)\deps.js" />
</Target>

实际构建更复杂,但仍然相对简单(假设您精通 MSBuild)。我们只是为脚本调用的每个相关部分使用了不同类型的项目组。

<Target Name="Build" DependsOnTargets="Init;FindCompiler">
  <PropertyGroup Condition="'@(Extern)' != ''">
    <Externs>-f --externs=@(Extern, ' -f --externs=')</Externs>
  </PropertyGroup>
  <PropertyGroup Condition="'@(Define)' != ''">
    <Defines>-f --define=@(Define, ' -f --define=')</Defines>
  </PropertyGroup>
  <PropertyGroup Condition="'@(Compile)' != ''">
    <Compile>-i @(Compile, ' -i ')</Compile>
  </PropertyGroup>
  <Exec Command="$(CalcDeps) $(Compile) -o compiled -c $(ClosureCompiler) -p $(ClosureLibrary) -p $(ScriptDirectory) $(Externs) $(Defines) -f @(CompilerOption, ' -f ') --output_file $(OutputFile)" />
</Target>

这很简单,我们不需要费心去寻找任务,或者尝试投资构建我们自己的任务。 Closure 是一个快速发展的项目,因此最好不要过度依赖任何第三方构建系统,尤其是那些看起来没有维护的系统(您链接的任务)。

现在,我一直用过去时态说话,因为我们的构建系统已经迁移了一些。具体来说,随着我们的项目不断增长,将脚本代码的不同部分划分为模块变得越来越重要。使用开箱即用的闭包脚本来执行此操作将是一场噩梦。因此,我们决定转向 plovr (http://plovr.com/),这使得将代码划分为模块变得非常简单。 plovr 的维护非常活跃,由 Michael Bolin 创建,他确实写了一本关于 Closure 的书(也强烈推荐)。

我们仍然使用相同的 MSBuild 文件来包装它。基本上,我们在项目组中定义的内容会移动到 plovr-config.js 文件中,并且调用也变得更加简单:

<Target Name="Build" DependsOnTargets="Init;FindPlovr">
  <Exec Command="$(Plovr) build plovr-config.js" />
</Target>

plovr 支持其他一些很酷的功能,例如大小报告和模块图,但甚至如果没有这些,我们对当前的设置非常非常满意。

We've been using Closure Compiler for some time now in a .NET-based project.

Initially, we used a simple MSBuild .proj file which directly invoked the Python scripts. For example, we would make deps.js with something like the following:

<PropertyGroup>
  <ScriptDirectory>yourprojectname</ScriptDirectory>
  <ClosureLibrary>closure</ClosureLibrary>
  <CalcDeps>$(ClosureLibrary)\bin\calcdeps.py</CalcDeps>
</PropertyGroup>

<Target Name="Deps">
  <Exec Command="$(CalcDeps) -o deps -p $(ScriptDirectory) -d $(ClosureLibrary) --output_file=$(ScriptDirectory)\deps.js" />
</Target>

The actual build was more complex, but still relatively straightforward (assuming you're MSBuild savvy). We simply used different types of item groups for each relevant part of the script invocation.

<Target Name="Build" DependsOnTargets="Init;FindCompiler">
  <PropertyGroup Condition="'@(Extern)' != ''">
    <Externs>-f --externs=@(Extern, ' -f --externs=')</Externs>
  </PropertyGroup>
  <PropertyGroup Condition="'@(Define)' != ''">
    <Defines>-f --define=@(Define, ' -f --define=')</Defines>
  </PropertyGroup>
  <PropertyGroup Condition="'@(Compile)' != ''">
    <Compile>-i @(Compile, ' -i ')</Compile>
  </PropertyGroup>
  <Exec Command="$(CalcDeps) $(Compile) -o compiled -c $(ClosureCompiler) -p $(ClosureLibrary) -p $(ScriptDirectory) $(Externs) $(Defines) -f @(CompilerOption, ' -f ') --output_file $(OutputFile)" />
</Target>

This was simple enough that we didn't bother looking for a task, or trying to invest in building our own. Closure is quite a fast moving project, so it's good to be in a situation where you're not overly dependent upon any third party build systems, especially one that looks to be unmaintained (the task you linked).

Now, I've been speaking in past tense because our build system has migrated a bit. Specifically, as our project kept growing it became increasingly important to partition different parts of our script code into modules. Doing this with the out-of-the-box Closure scripts would be quite a nightmare. Thus, we decided to move to plovr (http://plovr.com/), which makes partitioning code into modules very simple. plovr is very actively maintained and was created by Michael Bolin, who literally wrote the book on Closure (also highly recommended).

We still wrap this using the same MSBuild file. Basically, the stuff that we were defining in the item groups moves to a plovr-config.js file, and the invocation becomes much simpler as well:

<Target Name="Build" DependsOnTargets="Init;FindPlovr">
  <Exec Command="$(Plovr) build plovr-config.js" />
</Target>

There are some other cool features supported by plovr, like size reports and module graphs, but even without those we're very, very pleased with our current setup.

乜一 2024-11-09 21:33:50

最明显的选择是 YUI Compressor,它稳定可靠。

它有一个 .Net 端口: Yahoo! UI 库:.Net 的 YUI 压缩器
附带了 mbsbuild 任务。

另外,使用原始(Java)是 就像使用Exec任务一样简单,唯一的缺点是它具有java依赖性。

The most obvious choice is YUI Compressor, it's stable and reliable.

It has a .Net port: Yahoo! UI Library: YUI Compressor for .Net
The ships with an mbsbuild task.

Also, using the original (Java) is just as easy using Exec task, the only drawback is that it has java dependency.

毁梦 2024-11-09 21:33:50

SquishIt 详细信息 这是一个运行时压缩器,但做得相当好。

There's SquishIt Details which is a runtime compressor, but done quite well.

沙与沫 2024-11-09 21:33:50

您应该看到我在 将 Microsoft AJAX Minifier 与 Visual Studio 2010 一键发布结合使用。您应该能够使用此处的详细信息来解决您的问题。

You should see a very similar question that I answered at Using Microsoft AJAX Minifier with Visual Studio 2010 1-click publish. You should be able to use the details there to solve your problems here.

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