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

Confirmed. This doesn't work for me either:

"fileReplacements": [
                {
                  "replace": "src/app/app.component.html",
                  "with": "src/app/som-other.html"
                }
              ]

However it still goes to look for the file, I noticed this because I misspelled 'some-other.html' and got the following error: The src/app/some-other.html path in file replacements does not exist.

phl3x0r avatar May 16 '18 08:05 phl3x0r

FYI, this has been addressed and will be part of the 6.1 release.

To look at that change, please refer to this PR: angular/devkit#887

Brocco avatar May 16 '18 19:05 Brocco

Just for reference: https://github.com/angular/devkit/issues/885 explains:

Currently, the file replacement functionality works only for TS files. More specifically - it works only for files that are part of the webpack compiler host.

The above-mentioned https://github.com/angular/devkit/pull/887 apparently fixes that; can't wait for 6.1 now ;-)

(And indeed, for me .scss files are not copied either.)

avbentem avatar May 22 '18 08:05 avbentem

scss files are indeed not copied. Also not working with 6.1.0-beta.2. Any idea if it will be?

robindijkhof avatar Jun 11 '18 16:06 robindijkhof

~~FYI, not working in 6.1.0-rc.0 either~~ UPDATE: if updating an existing project it's not enough to update the global and project-local @angular/cli version, you also need to update the @angular-devkit/build-angular package to 0.7.0-rc.0. It is a separate package.

npm i -g @angular/[email protected]
cd your-project-folder
npm i -D  @angular/[email protected] @angular-devkit/[email protected]

@robindijkhof

suau avatar Jun 21 '18 11:06 suau

Issue still happening in this version: "@angular/cli": "6.1.5".

luillyfe avatar Aug 29 '18 05:08 luillyfe

Issue still happening in this version: "@angular/cli": "6.1.5".

"configurations": { "production": { "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true, "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ] } }

UPDATE:

fixed by updating @angular-devkit/build-angular as @suau suggested Thanks @suau

georgefam avatar Sep 04 '18 11:09 georgefam

@luillyfee @georgefam did you make sure you've updated @angular-devkit/build-angular as well ?

suau avatar Sep 04 '18 11:09 suau

@suau I can no longer reproduce this issue. Also I cannot remember how I did make it work, maybe is how you are pointing out.

luillyfe avatar Sep 04 '18 23:09 luillyfe

Any updates on this issue? Should it be fixed in 6.1.5?

Im trying to replace a constants.scss (with constants.newconfig.scss) file that I import at the top of my root styles file. But none of the newconfig styles are pulling through.

The strange thing is if I use this method with a component specific scss file it works perfectly. From what I can see its only with the root style files.

Here's a stackoverflow link to my question.

Im using 6.1.5

developcodeza-matthew avatar Sep 05 '18 13:09 developcodeza-matthew

@developcodeza-matthew there is not link in your answer!

luillyfe avatar Sep 05 '18 14:09 luillyfe

@luillyfe My bad 🙈 Should be there now

developcodeza-matthew avatar Sep 05 '18 14:09 developcodeza-matthew

I'm facing the same issue with CLI 6.2.2


UPDATE:

I fixed it by updating @angular-devkit/build-angular as @suau suggested

iki789 avatar Sep 14 '18 19:09 iki789

@iki789 You save my life . Thanks Bro.

MarkJAmes1217 avatar Sep 20 '18 06:09 MarkJAmes1217

Is it possible to do dynamic file replacements?

I'm trying to build two different UI's from the same App. So i have two different html files.

          "fileReplacements": [
            {
              "replace": "**.template1.ts",
              "with": "**.template2.ts"
            }

It's throwing me an error. Is this possible?

oberoivarun avatar Sep 24 '18 08:09 oberoivarun

using angular ^6.0.9 & angular/cli ^6.0.8, updated @angular-devkit/build-angular in devDependencies in package.json to ^0.8.9. seems to work as expected with environments and all

shayll-herolo avatar Feb 04 '19 10:02 shayll-herolo

with @angular/cli 7.3.0 html files are not replaced.

kojilab avatar Feb 22 '19 00:02 kojilab

Replacing html files is good feature to have. Very strange that the angular team hasn't addressed this issue.

AntonisFK avatar Mar 07 '19 19:03 AntonisFK

Has anyone got a fix for this yet?

ANewChanceAtLife avatar Mar 13 '19 10:03 ANewChanceAtLife

UPDATE: if updating an existing project it's not enough to update the global and project-local @angular/cli version, you also need to update the @angular-devkit/build-angular package to 0.7.0-rc.0. It is a separate package.

npm i -g @angular/[email protected]
cd your-project-folder
npm i -D  @angular/[email protected] @angular-devkit/[email protected]

@robindijkhof

@AntonisFK @MattCMcNabb did you make sure yo update to the newest version as well ? works fine for me.

suau avatar Mar 13 '19 15:03 suau