Exclude assets in production build

风格不统一 提交于 2021-02-08 13:05:58

问题


I have some assets (images, styles) which I only need during development (ng serve). These assets must not be included in the production build. In production these assets are provided by a CDN.

I need:

  • ng serve should serve files contained in the folder ./assets-non-build

but:

  • ng build should not inculde the folder ./assets-non-build in the final build

I have worked through 10 similar questions here on SO and 5 issues on github, they all deal with excluding files, but none solved my situation.


回答1:


Inside your angular.json there is a configurations object projects.{project-name}.architect.build.configurations.

Set the assets inside the prod entry to []

"production": {
  //...
  "assets": []
}

This is untested though, but by judging from what I know from the configuration file, this should be possible.

This will make any build and serve with the production flag to exclude the assets. If you really want all the builds, no matter the environment, to build without assets, you move the assets array from projects.{project-name}.architect.build.options to the projects.{project-name}.architect.serve.options and set the one in build to an empty array.




回答2:


PS: Code not tried yet but for comment its getting too long so posting as answer here.

"architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                        ....
                        "tsConfig": "src/tsconfig.app.json",
                        "assets": [
                            "src/favicon.ico",
                            "src/assets"
                        ],
                        "styles": [
                             ...
                        ],
                        "scripts": []
                    }
                    }
                },
                "serve": {
                        "assets": [
                            "src/assets"
                        ],
                }



回答3:


It seems the answer from Poul Kruijt is correct for older versions of angular. On Angular 9 prod entry do not work for me. You have to use production instead. So that is what works for me in angular 9.



来源:https://stackoverflow.com/questions/53297722/exclude-assets-in-production-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!