Here is my solution:
Keep an empty index.html
Run ng build to generate index.html with scripts and styles links injected
- Create a Gulp/Grunt task to extract all the scripts and styles links from index.html to a json file say manifest.json.(Here I use Cheerio and Gulp-asset-manifest )
- Write logic to read manifest.json and output to your MVC views with Razor syntax
It works perfectly for my ASP.net MVC 5 project.
However it cannot work with route lazy load until publicPath is supported, because the url for chunk js files is loaded from root folder not dist folder.
For example:
The correct chunk js files should be:
www.example.com/MyApp/dist/[id].chunk.js
But it will load from:
www.example.com/MyApp/[id].chunk.js
I already created a PR to add pulishPath for angular-cli to solve this issue.
https://github.com/angular/angular-cli/pull/3285
Update:
The PR for publicPath has been merged, lazy loading is no long an issue.