Angular 6 universal, .net template , unable to publish due to uglifyjs-webpack-plugin

632 Views Asked by At

I'm using Angular 6 universal with .net template and unable to publish the site. I'm getting the following error:

C:\Program Files\dotnet\sdk\2.1.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(140,5): error MSB3030: Could not copy the file "C:\projects\stash\serversiderendering\serverSideRendering\ClientApp\node_modules.cache\uglifyjs-webpack-plugin\content-v2\sha512\12\98\ca644f17078ac9e4c0fcda9c49f1ce74cd50192b7a404d45d26adabcc8f8fa22ca38476a913d2b79930a104b1d8fe9d2400a1dbc65e6951943067e92ebc5" because it was not found. [C:\projects\stash\serversiderendering\serverSideRendering\serverSideRendering.csproj]

Something with copying uglifyjs-webpack-plugin in .cache folder.

I went through the tutorial how to enable universal: Use the Angular project template with ASP.NET Core. The template comes with Angular 5 I updated Angular and cli to 6 so I had to do some changes:

My angular.json file looks like:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "serverSideRendering": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "progress": true,
            "polyfills": "src/polyfills.ts",
            "assets": [
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/server",
            "main": "src/main.server.ts",
            "tsConfig": "src/tsconfig.server.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "serverSideRendering:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "serverSideRendering:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "scripts": [],
            "styles": [
              "src/styles.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "assets": [
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "serverSideRendering-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "serverSideRendering:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "e2e/tsconfig.e2e.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "ssr": {
      "root": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/server",
            "main": "src/main.server.ts",
            "tsConfig": "src/tsconfig.server.json"
          },  "configurations": {
            "dev": {
                "sourceMap": false
            },
            "production": {
                "optimization": true,
                "outputHashing": "none",
                "sourceMap": false,
                "namedChunks": false,
                "extractLicenses": true,
                "vendorChunk": false,
                "fileReplacements": [
                    {
                        "replace": "src/environments/environment.ts",
                        "with": "src/environments/environment.prod.ts"
                    }
                ]
            }
        }
        }
      }
    }
  },
  "defaultProject": "serverSideRendering",
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "app",
      "styleext": "css"
    },
    "@schematics/angular:directive": {
      "prefix": "app"
    }
  }
}

and my package.json looks like:

{
  "name": "serverSideRendering",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build:ssr": "npm run build -- --project=ssr",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.5",
    "@angular/cdk": "6.0.1",
    "@angular/common": "6.0.1",
    "@angular/compiler": "6.0.1",
    "@angular/core": "6.0.1",
    "@angular/forms": "6.0.1",
    "@angular/http": "6.0.1",
    "@angular/platform-browser": "6.0.1",
    "@angular/platform-browser-dynamic": "6.0.1",
    "@angular/platform-server": "6.0.1",
    "@angular/router": "6.0.1",
    "@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5",
    "aspnet-prerendering": "^3.0.1",
    "bootstrap": "^3.3.7",
    "core-js": "2.5.5",
    "rxjs": "6.1.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.6.1",
    "@angular/cli": "6.0.1",
    "@angular/compiler-cli": "6.0.1",
    "@angular/language-service": "6.0.1",
    "@angularclass/hmr": "latest",
    "@types/jasmine": "^2.8.6",
    "@types/jasminewd2": "^2.0.3",
    "@types/mongodb": "^3.0.5",
    "@types/node": "~6.0.60",
    "codelyzer": "latest",
    "jasmine": "latest",
    "jasmine-spec-reporter": "latest",
    "karma": "latest",
    "karma-chrome-launcher": "latest",
    "karma-coverage-istanbul-reporter": "latest",
    "karma-jasmine": "latest",
    "karma-jasmine-html-reporter": "latest",
    "mongodb": "3.1.0-beta4",
    "protractor": "latest",
    "ts-node": "latest",
    "tslint": "latest",
    "typescript": ">=2.7.2 and <2.8.0",
    "uglifyjs-webpack-plugin": "^1.2.5"
  },
  "optionalDependencies": {
    "node-sass": "^4.9.0"
  }
}
2

There are 2 best solutions below

0
On

I was only able to fix my issue by turning off optimization on server side in angular.js

     "server": {
      "builder": "@angular-devkit/build-angular:server",
      "options": {
        "outputPath": "dist/server",
        "main": "src/main.server.ts",
        "tsConfig": "src/tsconfig.server.json",
        "optimization": false
      }
    }
2
On

I was able to fix the issue by excluding the .cache folder from publishing in csproj task PublishRunWebpack like this:

<PropertyGroup>
    ...
    <SpaRoot>Client</SpaRoot>
    ...
</PropertyGroup>

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
  <DistFiles Include="$(SpaRoot)\dist\**" />
  <DistFiles Include="$(SpaRoot)\node_modules\**; $(SpaRoot)\dist-server\**" 
             Exclude="$(SpaRoot)\node_modules\.cache\**" 
             Condition="'$(UseServerSideRenderer)' == 'true'" />
  <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')"
                         Exclude="@(ResolvedFileToPublish)">
    <RelativePath>%(DistFiles.Identity)</RelativePath>
    <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </ResolvedFileToPublish>
</ItemGroup>