问题
I am following the official Angular Universal guide available at
https://angular.io/guide/universal
but when I run ng build --prod
it does produce client bundles but doesn't produce server bundles.
Since my environment is ASP.Net Core, I have skipped Universal Webpack configuration
and server.ts
.
With previous version of Angular CLI we were able to build server bundle by specifying app while building ng build --app=ssr
Am I missing something? Can anyone please help?
回答1:
Fix: One thing though, the update process did not bring over the field deployUrl
, which caused my app to not be able find the bundles when running node dist/server.js. So I added it back in angular.json and it worked. Not sure if this is similar to your case.
Edit2: So, the updated build-angular:server is optimized to generate only the needed code for server-side rendering. So there isn't a need to generate bundles such as scripts.15e6d454b35fa67e195e.js
or polyfills.7f343fe54490ee5246c8.js
, they are embedded in the main.js
.
Edit: I just realized that due to the many changes of the compiling process, using build-angular:browser will not generate a server-side compatible bundle. (multiple reference of window is present)
In Angular 6, they opted for a different builder which uses:
"builder": "@angular-devkit/build-angular:server",
Using this builder these fields are somehow not available. I'm not sure whether it is a bug that the server build config doesn't extend from our main app or there are reasons that we're should not be specifying these fields for build-angular:server
.
"polyfills": "src/polyfills.ts",
"assets": [
// Your assets
],
"styles": [
// Your global styles
],
"scripts": [
// Your global scripts
]
A workaround to make Angular Universal work as before Angular 6 is to use the browser builder to bundle your server app.
You should be familiar with the process, simply copy your configurations from "architect/build"
and replace the fields that are related to server-side.
e.g.:
"outputPath": "dist/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json",
Note: You should disable output hashing either by CLI arguments or changing your configurations for server in angular.json
to that your server.ts can reference the entry
来源:https://stackoverflow.com/questions/50401415/angular-cli-6-ng-build-doesnt-produce-server-bundles