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

fileReplacements doesn't work with css files

Open Lionardo opened this issue 8 years ago • 45 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Area

- [x] devkit
- [ ] schematics

Versions

node --version: v9.4.0 npm --version: 5.6.0 macOS high sierra 10.13.5

Repro steps

1- In angular.json add a new configuration and try to replace a css file. 2- add a simple background style to header.local.css 3- import the header.css file into styles.scss

E.g.

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

The log given by the failure

none

Desired functionality

get the header.css replaced by header.local.css

Mention any other details that might be useful

Tested with angular cli version 6.0.1 and angular cli version 6.1.0-beta.0

Lionardo avatar Jul 04 '18 13:07 Lionardo

@konsultaner Yes this should be better for colors and other variables, but how can this help providing different CSS code per theme, beyond properties' values?

Javarome avatar May 02 '19 11:05 Javarome

@Javarome Thats definitly not working with variables.. Thats right.

konsultaner avatar May 02 '19 12:05 konsultaner

I also need this functionality. We have a white-labelled application that need specific .pdf, .scss, .png etc. files to be copied. In the case of .scss, these need to be replaced at pre-build.

I want to avoid having multiple projects/configurations/apps defined in the angular.json, because adding new brands would require a change to the base code each time. If .scss files worked with "fileReplacements", we could overcome this, by keeping the "branded" differences in different repositories and simply take the files from those during build.

In our case, TeamCity and OctoDeploy would take care of the rest.

mortware avatar Jun 17 '19 14:06 mortware

I need this feature for the ability to build the app using a specific theme which I can pass using a parameter.

takahser avatar Jun 21 '19 12:06 takahser

Hi, I need this functionality for different theme of my application.

rssaranece94 avatar Jul 01 '19 09:07 rssaranece94

Thank you @richardtreier for this very nice solution. Just implemented with angular-cli 7.3.9 and it works as advertised. My initial difficulties were due to my own errors; perhaps others who claim this doesn't work may wish to revisit.

jgeiger-cr avatar Jul 01 '19 12:07 jgeiger-cr

Here is my solution to this problem I wrote npm scripts in package.json file. Which copy the necessary files before starting ng build. I have two apps app1 and app2 with different themes. The production build command are app1 npm run build:app1:prod

app2 npm run build:app2:prod

"scripts": {
    "ng": "ng",
    "serve:app1": "npm run skin:app1 && ng serve app1 -c dev -o",
    "serve:app2": "npm run skin:app2 && ng serve app2 -c dev -o",
    "build:app1:stage": "npm run skin:app1 && ng build app1 -c stage --prod",
    "build:app1:prod": "npm run skin:app1 && ng build app1 --prod",
    "build:app2:stage": "npm run skin:app2 && ng build app2 -c stage --prod",
    "build:app2:prod": "npm run skin:app2 && ng build app2 --prod",
    "skin:app1": "cp -arv src/apps/app1/skin/. node_modules/ng-uikit-pro-standard/assets/scss/",
    "skin:app2": "cp -arv src/apps/app2/skin/. node_modules/ng-uikit-pro-standard/assets/scss/"
  }

kalimulhaq avatar Jul 04 '19 05:07 kalimulhaq

Thanks @richardtreier your solution worked perfectly!

Since I'm a bit new to Angular CLI I had a bit of trouble understanding what stylePreprocessorOptions does. For anyone needing a bit more information about stylePreprocessorOptions you can have a look at this article:

https://scotch.io/tutorials/angular-shortcut-to-importing-styles-files-in-components

dangrima90 avatar Aug 27 '19 13:08 dangrima90

I am trying to replace with robots.txt , doesn't work as well .

karocksjoelee avatar Oct 14 '19 10:10 karocksjoelee

@karocksjoelee robots.txt should work. This issue is related only to css files (where sass processor is used). All other types of file replacements work.

In angular.json: Make sure you first add robots.txt to build.assets so it gets copied at build time

"assets": [
              { "glob": "**/*", "input": "src/assets/", "ignore": ["**/*.ts"], "output": "/assets/" },
              { "glob": "favicon.ico", "input": "src/", "output": "/" },
              { "glob": "robots.txt", "input": "src/", "output": "/" }
            ],

and then apply the file replacements:

"fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                },
                {
                  "replace": "src/robots.txt",
                  "with": "src/robots.prod.txt"
                }
              ],

danmana avatar Nov 11 '19 13:11 danmana

Still doesn't work in 2020 !

kodze avatar Feb 05 '20 23:02 kodze

The functionality of fileReplacements block is not very sophisticated because it just replaces files. The main problem is that it doesn't affect the resolve process of imports, thus breaks for almost all files that have transitive relative imports.

For example if you have the following structure

  • app
    • components
      • test
        • test.component.scss
        • test.component.html
        • test.component.ts
  • replacements
    • test-new.component.ts (with templateUrl and styleUrl "../app/components/test/test.component.xxx")

and you want to replace the test.component.ts with test-new.component.ts, it won't work because the file content just replaces the content of the original test.component.ts in the test folder. The build process then looks up the imports like templateUrl and styleUrls which cannot be resolved in that context.

There may be a way to have a working folder structure to deal with such problems, but it's very cumbersome.

Because we need such functionality I'm using webpack with a custom webpack-resolve plugin instead of angular-cli. For the moment it works fine when using JIT and a bit limited when using AOT. But with angular 9 and ivy, new issues arrived. The hassle is real.

There really should be a more sophisticated "fileReplacement" option that deals with the correct resolvement and imports of replaced files.

chumbalum avatar Feb 18 '20 09:02 chumbalum

json files still does not work

"assets": [
  "src/config"
],
"fileReplacements": [
  {
    "replace": "src/environments/environment.ts",
    "with": "src/environments/environment.int.ts"
  },
  {
    "replace": "src/config/config.json",
    "with": "src/config/config.int.json"
  }
]

manoyanx avatar May 29 '20 02:05 manoyanx

@manoyanx as a workaround you can import the json files in your environment

tmtron avatar May 29 '20 06:05 tmtron

@tmtron, actually, fileReplacements is not meant to replace files which are outside of bundles. See https://github.com/angular/angular-cli/issues/16779#issuecomment-579183693

SchnWalter avatar May 29 '20 10:05 SchnWalter

@tmtron, actually, fileReplacements is not meant to replace files which are outside of bundles. See #16779 (comment)

I agree, but in this case the json files src/config/config*.json are in the bundle, right?
i.e. the json-file can be imported in typescript code, which is transpiled into js-code and thus part of an application bundle.
or the other way around: these files are not assets: i.e. you don't want to load them from your client application on demand.

tmtron avatar May 29 '20 11:05 tmtron

The assets don't count as part of the JS bundles, and he's been using that, see:

"assets": [
  "src/config"
  "..."
],

They are part of the bundle only if you import them directly using the --resolveJsonModule that you've linked above. Basically, if use use that flag with this kind of import, then you can use fileReplacemens:

import * as APP_CONFIG  from './app.config.json';

Otherwise, you'll need something like this: https://github.com/angular/angular-cli/issues/7506#issuecomment-324981817

More related info: #3855, #7506, #7704


P.S. @manoyanx, your comment and the discussion that followed is not related to the original issue, if the above linked issues don't help, you should create a new issue.

SchnWalter avatar May 29 '20 12:05 SchnWalter

@SchnWalter oh right - I missed the assets config and only looked at the path, thanks!

tmtron avatar May 29 '20 12:05 tmtron

Its a very important feature, please do give a fix on this ASAP.

On working with Internationalization it is very much essential thing. Also the component specific files should also be get replaced by this Angular config.

I am i18n for Internationalization, but the issue is with the SCSS file replacement for Arabic layout.

asphub avatar Jun 07 '20 10:06 asphub

Hi asphub, for the Internationalization you can use the i18n built in mechanism provided by Angular !

kodze avatar Jun 08 '20 08:06 kodze