angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[Bug] fileReplacements configuration can not replace html files

Open idododu opened this issue 8 years ago • 39 comments

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 fileReplacements config in configurations block of angular.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-b to start dev server

Observed behavior

home-a.component.ts was replaced correctly.

  • home-a.component.ts in chrome source image
  • home-a.component.ts in my source code image

home-a.component.ts was not replaced

  • home-a.component.html in chrome source
module.exports = "<p>\r\n  home-a works!\r\n</p>\r\n"
  • home-a.component.html in my source code
<p>
  home-a works!
</p>
  • home-b.component.html in my source code
<p>
  home-b works!
</p>

Desired behavior

  1. Expected to see content of home-a.component.html should be replaced by home-b.component.html since i have configed in angular.json
{
                  "replace": "src/app/home-a/home-a.component.html",
                  "with":    "src/app/home-b/home-b.component.html"
                },
  1. 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 environments configuration.

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/*/**"
}

idododu avatar May 16 '18 08:05 idododu

@suau @Brocco

Has anyone tried to replace app.module.ts?

Jun711 avatar Mar 21 '19 22:03 Jun711

@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.

Santalov avatar May 24 '19 14:05 Santalov

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.

C0ZEN avatar Jun 04 '19 12:06 C0ZEN

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).

jlnknz avatar Jun 07 '19 03:06 jlnknz

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

xxyyzz2050 avatar Jul 02 '19 11:07 xxyyzz2050

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"

BovineEnthusiast avatar Jul 29 '19 22:07 BovineEnthusiast

also failed for me in version 8.0.1

elliotsabitov avatar Jul 29 '19 22:07 elliotsabitov

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 avatar Aug 06 '19 00:08 vaughnmuirhead

@vaughnmuirhead Exact same case with me.

BilalAlam173 avatar Aug 16 '19 07:08 BilalAlam173

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"

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",

ricardosaracino avatar Aug 20 '19 16:08 ricardosaracino

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

Chris1234567899 avatar Sep 03 '19 20:09 Chris1234567899

This is working in my angular client but not in my angular universal ssr project.

Not doing anything differently.

lukegorman avatar Sep 05 '19 14:09 lukegorman

"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 avatar Sep 12 '19 03:09 racerhere

@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.

lukegorman avatar Sep 12 '19 07:09 lukegorman

@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

ricardosaracino avatar Sep 12 '19 09:09 ricardosaracino

@mgechev Can you please reopen this bug so it will be priorities?

eliraneliassy avatar Sep 12 '19 13:09 eliraneliassy

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

shayll-herolo avatar Sep 12 '19 14:09 shayll-herolo

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.

clydin avatar Sep 12 '19 14:09 clydin

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.