[Bug] fileReplacements configuration can not replace html files
Versions
Angular CLI: 6.0.0
Node: 8.11.1
Angular: 6.0.0
Windows 7
Repro steps
Reproduce git: https://github.com/idododu/ng6-filereplacements-bug
- Step 1: add
fileReplacementsconfig inconfigurationsblock ofangular.json
"project-b": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.project-b.ts"
},
{
"replace": "src/app/home-a/home-a.component.html",
"with": "src/app/home-b/home-b.component.html"
},
{
"replace": "src/app/home-a/home-a.component.ts",
"with": "src/app/home-b/home-b.component.ts"
}
]
}
- Step 2:
npm run project-bto start dev server
Observed behavior
home-a.component.ts was replaced correctly.
home-a.component.tsin chrome source
home-a.component.tsin my source code
home-a.component.ts was not replaced
home-a.component.htmlin chrome source
module.exports = "<p>\r\n home-a works!\r\n</p>\r\n"
home-a.component.htmlin my source code
<p>
home-a works!
</p>
home-b.component.htmlin my source code
<p>
home-b works!
</p>
Desired behavior
- Expected to see
content of
home-a.component.htmlshould be replaced byhome-b.component.htmlsince i have configed inangular.json
{
"replace": "src/app/home-a/home-a.component.html",
"with": "src/app/home-b/home-b.component.html"
},
- Usecase
Suppose we got two projects: Project A and Project B.
They share the most of my source codes except HomeComponent.
And I would like to put the codes in the same repo, and build different projects through
environmentsconfiguration.
Mention any other details that might be useful (optional)
Another point: can we support glob expressions in fileReplacements configuration?
{
"replace": "src/app/home-a/*/**",
"with": "src/app/home-b/*/**"
}
@suau @Brocco
Has anyone tried to replace app.module.ts?
@Jun711
Have tried to replace app.module.ts with app.module.promoter.ts, but get error, that AppComponent is in 2 modules.
ERROR in : Type AppComponent in C:/Users/santa/WebstormProjects/theBox/src/app/app.component.ts is part of the declarations of 2 modules: AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.ts and AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.promoter.ts! Please consider moving AppComponent in C:/Users/santa/WebstormProjects/theBox/src/app/app.component.ts to a higher module that imports AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.ts and AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.promoter.ts. You can also create a new NgModule that exports and includes AppComponent in C:/Users/santa/WebstormProjects/theBox/src/app/app.component.ts then import that NgModule in AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.ts and AppModule in C:/Users/santa/WebstormProjects/theBox/src/app/app.module.promoter.ts.
And there are no references to the app.module.promoter.ts in code at all.
I never had this problem, but since I updated my project to Angular 8, this is not working anymore. The replacement of the index.html does not occur.
I never had this problem, but since I updated my project to Angular 8, this is not working anymore. The replacement of the index.html does not occur.
For later reference, in #14599, @clydin said:
The file replacements option was never intended to replace files outside of the script bundles. That it worked previously was more of a defect itself than anything else.
See this ticket for temporary workarounds (disable es2015 targetting or rename index file after building).
even .ts not worked for me
//angular.json -> projects.myProject.architect.build.configurations.dev :
//also in architect.server section
"dev": {
"fileReplacements": [
{
"replace": "src/environments/index.ts",
"with": "src/environments/dev.ts"
}
]
}
//src/environments/index.ts
console.log("index")
//src/environments/dev.ts
console.log("dev")
> ng run myProject:build:dev && ng run myProject:server:dev && npx webpack --config webpack.server.config.js --progress --colors && nodemon dist/server --inspaect --open --watch
import env from "../src/environments/index.ts" //<--- should be replaced with dev.ts
it prints the console.log of index.ts not dev.ts
repo: https://github.com/xxyyzz2050/eldeebCMS
Can confirm this bug with Angular 8.1.3 (build-angular at 0.801.3). I've worked around it for now by having the following build script. I simply copy over the correct index.html at the end:
"build:stg": "ng build --configuration=staging && cp src/index.staging.html dist/index.html"
also failed for me in version 8.0.1
Failed for me on build but works on serve.
Config is:
{
"replace": "src/index.html",
"with": "src/prod/index.html"
}
Angular CLI: 8.0.3 Node: 10.16.0 OS: win32 x64 Angular: 8.0.1 ... animations, cdk, common, compiler, compiler-cli, core, forms ... language-service, material, platform-browser ... platform-browser-dynamic, router
Package Version
@angular-devkit/architect 0.800.3 @angular-devkit/build-angular 0.800.3
@angular-devkit/build-optimizer 0.800.3 @angular-devkit/build-webpack 0.800.3 @angular-devkit/core 8.0.3 @angular-devkit/schematics 8.0.3 @angular/cli 8.0.3 @angular/flex-layout 8.0.0-beta.26 @ngtools/webpack 8.0.3 @schematics/angular 8.0.3 @schematics/update 0.800.3 rxjs 6.5.2 typescript 3.4.5 webpack 4.30.0
@vaughnmuirhead Exact same case with me.
Can confirm this bug with Angular 8.1.3 (build-angular at 0.801.3). I've worked around it for now by having the following build script. I simply copy over the correct
index.htmlat the end:"build:stg": "ng build --configuration=staging && cp src/index.staging.html dist/index.html"
how do the scripts get injected into the index staging?
i have resorted to this should work in the azure pipeline
"build:en": "copy src\\index.en.html src\\index.html && ng build --configuration=production-en",
"build:fr": "copy src\\index.fr.html src\\index.html && ng build --configuration=production-fr",
Can confirm it does not work yet in 0.803.2
Trying to reference the index directly (not replacing it) seems to be another solution meanwhile: https://stackoverflow.com/a/57274333/3719922
This is working in my angular client but not in my angular universal ssr project.
Not doing anything differently.
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/index.html",
"with": "src/index.prod.html"
}
]
This is still not replacing the index.html in the build. @angular/cdk 8.2.0 @angular/cli 8.3.4 @angular/core 8.2.6 @angular-devkit/build-angular 0.803.4
Can anyone tell me if I am using the file replacements correctly?
@racerhere as stated above fileReplacements only works with certain filetypes. The most simple alternative is to use a shell script to replace those file in the build process.
@racerhere there is some discussion here
https://stackoverflow.com/questions/50113794/angular-how-to-dynamically-change-the-content-in-the-index-html-file-when-runn/57778511#57778511
@mgechev Can you please reopen this bug so it will be priorities?
I had the same issue. fortunately, our project only had 2 files for replacement so it was quite easy to change. in angular.json for every configuration just add the index property with a value of the path to the correct html file. https://angular.io/cli/build
so if previously the replacement was index.dev.html to index.html, the dist folder will only contain index.dev.html if thats not good enough, you can make a directory for each html file and just keep it as index.html.
here's an example:
{
"configurations":{
"production":{
"fileReplacements":[
{
"replace":"environments/environment.ts",
"with":"environments/environment.prod.ts"
}
],
"optimization":true,
"outputHashing":"all",
"sourceMap":false,
"extractCss":true,
"namedChunks":false,
"index":"src/prod-index/index.html",
"aot":true,
"extractLicenses":true,
"vendorChunk":true,
"buildOptimizer":true,
"budgets":[
{
"type":"initial",
"maximumWarning":"2mb",
"maximumError":"5mb"
}
]
},
"dev":{
"aot":false,
"index":"src/dev-index/index.html",
"fileReplacements":[
{
"replace":"environments/environment.ts",
"with":"environments/environment.dev.ts"
}
]
}
}
}
note the index property is referring to dev-index directory for dev configuration and to prod-index for prod configuration
it's kind of messy because if you have more then 2 different index html files you might need a lot more directories but other than that it works for me with angular 8.2.4
The index option supports a longhand form as follows:
"index": {
"input": "src/index.prod.html",
"output": "index.html",
},
Note that the schema used for IDE integration erroneously marks this as invalid (this will be corrected in the next patch release) but it can otherwise be safely used.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.