简体   繁体   English

Angular 4. 在生产环境中删除文件

[英]Angular 4. Remove file in production environment

I have Angular 4 project and use some of my files(or classes) only in dev environment.我有 Angular 4 项目并且只在开发环境中使用我的一些文件(或类)。 But how to remove them from the build for production environment(ng build --env=prod)?但是如何从生产环境的构建中删除它们(ng build --env=prod)?

Thanks.谢谢。

The production environnement uses the AOT Compilation therefore you can customize the build options by creating a file with the name tsconfig-aot.json in your src folder (for more infos check Ahead of Time Compilation ) then you can use the exclude array to specify the excluded files for example: 生产环境使用AOT编译,因此您可以通过在src文件夹中创建一个名称为tsconfig-aot.json的文件来自定义构建选项(有关更多信息,请tsconfig-aot.json of Time Compilation ),然后可以使用exclude数组指定排除文件,例如:

"exclude": [
  "test.ts",
   "**/*.spec.ts"
 ]

You can also use "fileReplacements" in angular.json for this. 您也可以在angular.json中使用"fileReplacements" You have to add that file in build and then replace it with nothing, so it will remove that file. 您必须在构建中添加该文件,然后再将其替换为任何内容,因此它将删除该文件。

For example, if I want to add robots.txt in only production environment but not in staging environment, then you can write like - 例如,如果我只想在生产环境中而不是在暂存环境中添加robots.txt ,则可以这样写-

"build": {
    "builder": "@angular-devkit/build-angular:browser",
    "options": {
        "outputPath": "dist/Build",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.app.json",
        "assets": [
            "src/favicon.ico",
            "src/robots.txt",
            "src/assets"
        ],
        "styles": [
            "node_modules/ionicons/dist/css/ionicons.css",
            "node_modules/ngx-lightbox/lightbox.css",
            "src/styles.css"
        ],
        "scripts": [
            "node_modules/prismjs/components/prism-typescript.js",
            "src/assets/js/customerUserMenu.js"
        ]
    },
    "configurations": {
        "production": {
            "outputPath": "dist/Production",
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                }
            ]
        },
        "staging": {
            "outputPath": "dist/MyprojectStaging",
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.staging.ts"
                },
                {
                    "replace": "src/robots.txt",
                    "with": ""
                }
            ]
        }
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM