发布核心构建环境控制

时间:2018-10-25 05:20:31

标签: msbuild .net-core msdeploy

当调用“发布”触发构建时,是什么控制传递给webpack的环境?看这个:

1>------ Build started: Project: MeshCut, Configuration: Debug Any CPU ------
1>MeshCut -> D:\MeshCut\bin\Debug\netcoreapp2.1\MeshCut.dll
2>------ Publish started: Project: MeshCut, Configuration: Debug Any CPU ------
MeshCut -> D:\MeshCut\bin\Debug\netcoreapp2.1\MeshCut.dll
npm install
audited 11025 packages in 7.787s
found 0 vulnerabilities

node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod
Hash: dadea967a4be0fa40482
Version: webpack 4.22.0

. . .
    Publish Succeeded.

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Publish: 1 succeeded, 0 failed, 0 skipped ==========

第一行显然是调试版本,但这一行

node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod

显然已通过env.prod,这使得无法找出为什么从IDE运行服务器时不会发生生产错误的原因。

我尝试将环境变量ASPNETCORE_ENVIRONMENT设置为“开发”,但这没有帮助,可能是因为这是运行时。

最终这是msbuild的事情。 pubxml文件和csproj文件的组合是msbuild的输入。

csproj文件中的env.prod是文字。如果有某种方法可以使此值取决于构建类型(发布/调试),则可以解决问题。

还有另一个问题是从根本上相同的问题启发而来的,但提问者对发现自己可以通过编辑csproj来控制它感到满意。这对遵守发布/调试没有帮助。

这是MSBUILD的东西。这意味着在csproj文件中查找,并确保我们可以找到

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

,很容易在第二个和第三个Exec语句中找到罪魁祸首。

剩下的问题是如何有条件地指定参数值,或者如何有条件地指定完全不同的命令行。

This Microsoft documentation似乎描述了第二种选择,但是msbuild不喜欢我用choosewhen元素包装exec元素的尝试,我仍然可以使用一些帮助。

1 个答案:

答案 0 :(得分:0)

这是MSBUILD的东西。这意味着在csproj文件中查找,并确保我们可以找到

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

,很容易在第二条Exec语句中找到罪魁祸首。

Exec支持Condition属性。幸运的是,在CSPROJ文件的其他位置,目标之一已经取决于我们需要的确切条件。稍加复制/粘贴和调整,代码就变成...

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
  <Exec Command="npm install" />
  <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.dev" 
        Condition=" '$(Configuration)' == 'Debug' " />
  <Exec Command="node node_modules/webpack/bin/webpack.js --env.dev" 
        Condition=" '$(Configuration)' == 'Debug' " />

  <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" 
        Condition=" '$(Configuration)' == 'Release' " />
  <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" 
        Condition=" '$(Configuration)' == 'Release' " />