Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a "ng build" from within a dotnetcore web-api project with msbuild but keep the angular project in a separate folder?

I’ve an angular app build with angular cli. I want my web-api to serve the static files from the wwwroot folder. I want also to keep both project nicely separated inside their own folders. This is the structure of my soltution:

.\angular-app
.\angular-app\angular-cli.json
.\web-api-app
.\web-api-app\wwwroot

Given this structure of my app, what I want is to configure my msbuild script web-api-app.csproj to somehow trigger a “ng build” from within the angular-app and output the build inside the wwwroot folder.

Thus I modified the “outDir” parameter inside .angular-cli.json to: “../web-api-app/wwwroot” so that when I call “ng build”, from within the angular-app, webpack outputs the result in the wwwroot folder.

How do I setup my MsBuild script so that when I invoke "dotnet build -c Release" the angular is packaged nicely inside the wwwroot folder?

like image 751
Geo Avatar asked Oct 24 '17 20:10

Geo


2 Answers

I found a way to be able to call the « ng build » from within my msbuild. The trick is to use an npm script inside the root of the DotNetCore app that “cd” into the angular app and then call “ng build” . These are the steps I took: 1) To be able to use npm script commands edit the script section of the package.json of the angular-app: (here I also define a start script to easily start my local dev environment & build to be able to build my scripts in debug mode)

"scripts": {
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "build-prd": "ng build --prod --env=prod",
 }

2) From with the Web-Api app, add a package.json inside the root of the web-api app containing:

"scripts": {
        "install": "cd ../angular-app & npm install",
        "start": "cd ../angular-app & npm start",
        "build": "cd ../angular-app & npm run-script build",
        "build-prd": "cd ../angular-app & npm run-script build-prd"
}

3) Finally, configure your web-api MsBuild script to invoke the ng build script when running a release build. Therefore, add following targets into the csproj file:

<Target Name="EnsureNode">
  <Exec Command="node --version" ContinueOnError="true">
    <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
  </Exec>
  <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
</Target>
<Target Name="ReleaseRunNgBuild" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Release' ">
  <CallTarget Targets="EnsureNode" />
  <Message Importance="high" Text="Install packages..." />
  <Exec Command="npm install" />
  <Message Importance="high" Text="Performing ng build for prd build..." />
  <Exec Command="npm run-script build-prd" />
</Target>

I created an example app where you can find the entire source code on my github project core-angular-example and this blog post also describe every step in detail.

like image 53
Geo Avatar answered Nov 04 '22 00:11

Geo


A simple solution that works for me with .NET Core

  <Target Name="Build Angular" BeforeTargets="Build">
    <Message Text="Build in process" Importance="high" />
    <Exec Command="cd angular-app &amp;&amp; ng build --prod --aot"  />
  </Target>
like image 20
Jairo Andres Velasco Romero Avatar answered Nov 04 '22 01:11

Jairo Andres Velasco Romero