msbuild - 在构建服务器上将 Google Closure Compiler 与 MS Build 集成

标签 msbuild continuous-integration minify google-closure-compiler google-closure

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

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

我对 Google Closure 编译器很感兴趣,我遇到过这个 .Net MSBuild Google Closure Compiler Task ,但它似乎并没有被广泛使用。是否有更好的选择与 MSBuild 一起使用,使用 Closure 或替代缩小工具?

最佳答案

我们在一个基于 .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 是一个快速发展的项目,因此最好不要过度依赖任何第三方构建系统,尤其是看起来没有维护的系统(您链接的任务)。

现在,我一直在说过去时,因为我们的构建系统已经迁移了一点。具体来说,随着我们的项目不断增长,将脚本代码的不同部分划分为模块变得越来越重要。使用开箱即用的 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 还支持其他一些很酷的功能,例如尺寸报告和模块图,但即使没有这些,我们也对我们当前的设置非常非常满意。

关于msbuild - 在构建服务器上将 Google Closure Compiler 与 MS Build 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5765496/

相关文章:

css - 无法取消 Webpack CSS 输出

asp.net-mvc - MVC ASP.NET 缩小 View .cshtml

c# - 在csproj中使用通配符时如何在xaml文件下嵌套xaml.cs

.net - 使用 MSBuild 签署 ClickOnce 或程序集会导致错误 MSB3321

continuous-integration - Gitlab CI 部署失败 : "bash: pm2: command not found" during gitlab-ci. yml 运行

github - Jenkins Pull Request builder 忽略带有 ${sha1} 的分支

javascript - 缩小并删除所有未使用的功能,使 js 文件大小尽可能小,web pack 2

msbuild - 在CruiseControl.NET中使用MSTest

msbuild - 如何在不安装 IDE 的情况下在构建服务器上安装 VS2017 版本的 msbuild?

build - Electron 应用程序Travis CI构建失败