'ng build' with all scripts in subfolder

2020-02-13 01:38发布

问题:

It is now possible, to get not flat structure of project with ng build, so instead of this:

index.html  
main.bundle.js  
styles.bundle.js  
...

I want scripts to be in subfolder

*index.html  
scripts/main.bundle.js  
scripts/styles.bundle.js  
...*

I found a topic about this: 'ng build' move scripts to subfolder but hire the solutions is to eject project from cli into webpack but with Angular 7 this is not longer available and ng eject is now disabled:

The 'eject' command has been disabled and will be removed completely in 8.0. The new configuration format provides increased flexibility to modify the configuration of your workspace without ejecting.

There are several projects that can be used in conjuction with the new configuration format that provide the benefits of ejecting without the maintenance overhead. One such project is ngx-build-plus found here: https://github.com/manfredsteyer/ngx-build-plus

Angular provide very poor documentation of angular.json file hire https://github.com/angular/angular-cli/wiki/angular-workspace so it's difficult to use this config file in real project problems.

回答1:

You cannot do that by using the ng build command alone, you can do it by using the following the commands one after the other.

ng build --output-path="dist/scripts" --deployUrl="scripts/"

and

move "dist\scripts\index.html" "dist"

The last command works considering that you are using windows (is just a copy/paste, the magic is done by the --deployUrl command).