简体   繁体   中英

Angular 4. Remove file in production environment

I have Angular 4 project and use some of my files(or classes) only in dev environment. But how to remove them from the build for production environment(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:

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

You can also use "fileReplacements" in angular.json for this. 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 -

"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": ""
                }
            ]
        }
    }
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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