Excluding node_modules folder from ASP.NET compilation

We started to make use of using Github URLs as npm dependencies (make sure to reference specific commits to ensure repeatable builds) to replace some JavaScript files that we had manually added to the project. Unfortunately one of the repositories we added contains an invalid razor view file (.cshtml), which causes razor view compilation to fail with the following error.

/temp/node_modules/ace-builds/demo/kitchen-sink/docs/razor.cshtml(6): error ASPPARSE: Encountered end tag "a" with no matching start tag.  Are your start/end tags properly balanced?

Razor view compilation failure caused by the node_modules folder is a bit of a recurring issue I have had. In the past I had been able to solve the problem by upgrading npm, but that was not going to work this time. What I needed to do was to get the ASP.NET compiler to ignore the node_modules folder.

The view compilation was configured in the .csproj file using the AspNetCompiler task e.g.

This results in a command like the following being executed during the build process.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v temp -p D:\repos\onemodel\webapp

If you run aspnet_compiler.exe with the help toggle you will see that there is a flag (-x) that can be used to exclude directories. Frustratingly there is no way to provide values to this flag using the provided AspNetCompiler task. Instead you need to use the Exec task to call aspnet_compiler.exe so you can pass in a value for node_modules. Replacing the AspNetCompiler task with Exec ends up like the following example.

The downside of this solution is that it is reliant on the aspnet_compiler executable always being in the same location, however that could probably be dynamically determined with some more MSBuild configuration.

Advertisements
Excluding node_modules folder from ASP.NET compilation