paranamix2
paranamix2

Reputation: 347

Adding prepublish section to asp.net Core *.csproj file

I use webpack in my asp.net Core MVC to bundgle js/css files and put it into the wwwroot folder after.

My folder structure looks like this on the screen:

enter image description here

I added "after-build" section into FrontEnd.csproj:

<Target Name="BuildClientAssets" BeforeTargets="Build">
  <Exec Command="npm install"/>
  <Exec Command="npm run build"/>
</Target>
  1. FrontEnd/node_modules contains npm modules loaded by "npm install" command;
  2. Web/wwwroot/dist and Web/wwwroot/fonts contains bundled js/css/fonts files, generated by "npm run build" command;

It works as expected. But it would be great if I had an oppurtunity to exec this command on publish web project.

But when I try to add prepublish section into the Web project and start publish

<Target Name="BuildClientAssets" BeforeTargets="Publish">
  <Exec Command="npm install"/>
  <Exec Command="npm run build"/>
</Target>

I get this error: The command "npm run build" exited with code -4058

I supposed, that it is because of the package.json located in FrontEnd project (not in the Web project), so I looked for a suitable script to start command in right way and overrrided the section:

<Target Name="BuildClientAssets" BeforeTargets="Publish">
  <Exec Command="npm install" />
  <Exec Command="webpack --config ../FrontEnd/webpack.config.js" />
</Target>

But after I get errors such as:

Can't resolve './src/components/app/file1.ts' ERROR in Entry module not found 0

Error Can't resolve './src/components/app/file2.ts'
ERROR in Entry module not found 0

Error Can't resolve 'source-map-loader' Module not found 0

The command is started but modules are searched still in the Web, not the FrontEnd project.

Any idea?

Upvotes: 3

Views: 1281

Answers (1)

Matheus Ferreira
Matheus Ferreira

Reputation: 387

The npm install isn't hitting the right folder either, you need run these commands inside the right location - probably worked in your machine because it already have the node-modules. You can keep both commands (ǹpm install / npm run build) and try to target the right location. How you can do that? You could inform the command the folder he will run.

First, set in the PropertyGroup section the front-end folder location

<FrontRoot>../FrontEnd/</FrontRoot>

and pass the property in the command lines using the WorkingDirectory property. It will looks like this:

<Target Name="BuildClientAssets" BeforeTargets="Publish">
  <Exec WorkingDirectory="$(FrontRoot)" Command="npm install"/>
  <Exec WorkingDirectory="$(FrontRoot)" Command="npm run build"/>
</Target>

There is a similar situation here

Upvotes: 2

Related Questions