Angular CLI (7.0.5) different assets for developme

2019-05-26 13:12发布

问题:

Is it possible to have different assets for development and production with Angular CLI (7.0.5)?

For production i want the assets:

"assets": [
   "projects/example/src/favicon.ico"
]

For development i want the assets:

"assets": [
   "projects/example/src/favicon.ico",
   "projects/example/src/assets/development.css"
]

Thank you in advance.

回答1:

We have an option called fileReplacements under production configurations in angular.json. There you can replace the files.

Note : I have not tested.



回答2:

I have found a way to make it work, it ain't pretty but it works.

In your angular.json file you can copy your current project, in my case my project is named "example". I rename the copied project to "example-dev". See the code below:

{
...
"projects": {
    "example": {
        "root": "projects/example/",
        "sourceRoot": "projects/example/src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {},
        "architect": {
            "build": {
                "builder": "@angular-devkit/build-angular:browser",
                "options": {
                    "outputPath": "dist/example",
                    "index": "projects/example/src/index.html",
                    "main": "projects/example/src/main.ts",
                    "polyfills": "projects/example/src/polyfills.ts",
                    "tsConfig": "projects/example/tsconfig.app.json",
                    "assets": [
                        "projects/example/src/favicon.ico"
                    ],
                    "styles": [
                        "projects/example/src/styles.scss"
                    ],
                    "scripts": []
                }
            },
            "serve": {
                "builder": "@angular-devkit/build-angular:dev-server",
                "options": {
                    "browserTarget": "example:build"
                }
            }
        }
    },
    "example-dev": {
        "root": "projects/example/",
        "sourceRoot": "projects/example/src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {},
        "architect": {
            "build": {
                "builder": "@angular-devkit/build-angular:browser",
                "options": {
                    "outputPath": "dist/example",
                    "index": "projects/example/src/index.html",
                    "main": "projects/example/src/main.ts",
                    "polyfills": "projects/example/src/polyfills.ts",
                    "tsConfig": "projects/example/tsconfig.app.json",
                    "assets": [
                        "projects/example/src/favicon.ico",
                        "projects/example/src/assets/development.css"
                    ],
                    "styles": [
                        "projects/example/src/styles.scss"
                    ],
                    "scripts": []
                }
            },
            "serve": {
                "builder": "@angular-devkit/build-angular:dev-server",
                "options": {
                    "browserTarget": "example-dev:build"
                }
            }
        }
    }
}

In the project "example" (production) i removed the line "projects/example/src/assets/development.css" in the assets. In the project "example-dev" i changed the browserTarget in serve to "example-dev:build" instead of "example:build"

If i want to serve the development project:

ng serve example-dev -o

If i want to serve the production project:

ng serve example -o

If someone has a cleaner solution then I would love to hear that :)

EDITED

Thanks to Karthick Venkat i have found a other way to make it work, which is alot cleaner.

In your angular.json file you need to add a new configuration for development. See the code below:

{
"projects": {
    "example": {
        "root": "projects/example/",
        "sourceRoot": "projects/example/src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {},
        "architect": {
            "build": {
                "builder": "@angular-devkit/build-angular:browser",
                "options": {
                    "outputPath": "dist/example",
                    "index": "projects/example/src/index.html",
                    "main": "projects/example/src/main.ts",
                    "polyfills": "projects/example/src/polyfills.ts",
                    "tsConfig": "projects/example/tsconfig.app.json",
                    "styles": [
                        "projects/example/src/styles.scss"
                    ],
                    "scripts": []
                },
                "configurations": {
                    "production": {
                        "fileReplacements": [{
                            "replace": "projects/example/src/environments/environment.ts",
                            "with": "projects/example/src/environments/environment.prod.ts"
                        }],
                        "optimization": true,
                        "outputHashing": "none",
                        "sourceMap": false,
                        "extractCss": true,
                        "namedChunks": false,
                        "aot": true,
                        "extractLicenses": true,
                        "vendorChunk": false,
                        "buildOptimizer": true,
                        "assets": [
                            "projects/example/src/favicon.ico"
                        ]
                    },
                    "development": {
                        "assets": [
                            "projects/example/src/favicon.ico",
                            "projects/example/src/assets"
                        ]
                    }
                }
            },
            "serve": {
                "builder": "@angular-devkit/build-angular:dev-server",
                "options": {
                    "browserTarget": "example:build"
                },
                "configurations": {
                    "production": {
                        "browserTarget": "example:build:production"
                    },
                    "development": {
                        "browserTarget": "example:build:development"
                    }
                }
            }
        }
    }
}

}

If i want to serve the development project:

ng serve example --configuration=development -o

If i want to serve the production project:

ng serve example --configuration=production -o

or

ng serve example -o