fileReplacements doesn't work with css files
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
As said in https://github.com/angular/angular-cli/issues/10881#issuecomment-389644466, ng-cli should be able to replace all type of files from 6.1.0, and not only .ts files.
This feature seems to be active in the rc but not in beta, see https://github.com/angular/angular-cli/issues/10881#issuecomment-399063501.
We added the generic replacement functionality in https://github.com/angular/devkit/pull/887, but this is limited to webpack loader and plugins that actually use the webpack file system. This might not work for all files, and we should error out when we know that's the case.
I have updated my angular/cli to version 6.1.1 and angular-devkit/build-angular to version 0.7.1. I see ng-build started to replace html files. However it is still not replacing scss files. My configuration looks like this:
"test": {
"fileReplacements": [
{
"replace": "src/index.html",
"with": "src/index_test.html"
},
{
"replace": "src/scss/base/_variables.scss",
"with": "src/scss/themes/_dark.scss"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": true,
"extractCss": true,
"namedChunks": false,
"aot": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": false
}
So I have updated angular/cli to version 6.1.2 but to no avail. @filipesilva so far we know that it works with html, and ts files. It doesn't work (from what I have tested) with css, scss and json files. What are the next steps to this issue? Can we at least make sure that the basic files like css, html and ts get replaced so most use cases are covered?
@angular/[email protected] @angular-devkit/[email protected]
OR
@angular/[email protected] @angular-devkit/[email protected]
Replacing scss files still not working.
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
with @angular/cli: 6.2.1 I was able to switch the global style file, but not a variables file like this:
"fileReplacements": [
{
"replace": "projects/example/src/styles.scss",
"with": "projects/example/src/styles-example.scss"
}
]
However the latter would be a more lean solution.
I am writing an app that is built for several clients.
Each clients have a variable.scss file with its Material Design variables
I wanted to use fileReplacements to switch this variable file at build time but it does not work.
I am pretty sure this is a common need, not sure how others do but fixing this issue would make the world a better place.
thanks
@shprink My exact need as well. Holding my thumbs that these issues will be solved soon so we can use fileReplacements as you would with Android Build Flavors or Swifts Build Variants
@shprink same 👍
I created component "widget" and need some static demo for this component. It mean, that I need to change controller, but html and styles stay same. To do it I can use "fileReplacement" feature. But it doesn't work...
src/app/widget/widget.component.ts:
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'fh-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.scss']
})
export class WidgetComponent implements OnInit {
title = '';
constructor() {
}
ngOnInit() {
this.title = 'wodget - real component';
}
}
src/app/widget/widget-static/widget.component.ts
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'fh-widget',
templateUrl: './widget.component.html',
styleUrls: ['./widget.component.scss']
})
export class WidgetComponent implements OnInit {
title = '';
constructor() {
}
ngOnInit() {
this.title = 'widget - static component';
}
}
and create a custom configuration with:
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/app/widget/widget.component.ts",
"with": "src/app/widget/widget-static/widget.component.ts"
}
]
But ng build -c static-demo has error:
NgModule has path until original component src/app/widget/widget.component.ts.

if I remove html+styles, because I don't need, then ng build has another error:
ERROR in : Couldn't resolve resource ./widget.component.scss relative to /..../src/app/widget/widget-static/widget.component.ts
do I something wrong?
@shprink
We also needed different variable configurations for different customers.
This is the workaround we used:
src/
scss/
product_configurations/
classic/
_product_configuration.scss
_fonts.scss
...
special-customer/
_product_configuration.scss
_fonts.scss
...
...
_variables.scss
style.scss
src/scss/_variables.scss
@import '_product_configuration';
/** Other variables, e.g. using colour functions, etc. */
/** [...] */
src/scss/style.scss
@import '_variables';
@import '_fonts';
/** Other imports, that use the variables, e.g. Bootstrap, Angular Material */
/** [...] */
/** Other styles */
/** [...] */
Excerpt from angular.json:
"configurations": {
"classic": {
"stylePreprocessorOptions": {
"includePaths": ["src/scss/product_configurations/classic"]
},
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.classic.ts"
},
{
"replace": "src/index.html",
"with": "src/index.classic.html"
}
]
},
"special-customer": {
"stylePreprocessorOptions": {
"includePaths": ["src/scss/product_configurations/special-customer"]
},
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.special-customer.ts"
},
{
"replace": "src/index.html",
"with": "src/index.special-customer.html"
}
]
}
}
@richardtreier thanks for sharing that, it is a working workaround !
Using angular 7.2 I'm able to replace html files but not for scss files. Is there any updates?
Using angular 7.2 I'm able to replace html files but not for scss files. Is there any updates?
Actually html files don't work for me with 7.3
I am also unable to replace html files with 7.1, using code in my angular.json which previously worked:
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/index.html",
"with": "src/index.prod.html"
}
]
Results in a build that uses index.html, and does not use index.prod.html
UPDATE: After trying to update packages in various manners, oddly enough, literally doing mv index.prod.html index.prod.html and then yarn build again fixed it, and the changes were picked up and built properly. Very odd.
any news on this? Is this going to be implemented?
@shprink did your solution work with angular 7.x? It didn't work for me ...
Same problem in here with Angular 7.2.10 (2019-03-20)
I'm using this workaround meanwhile: https://github.com/angular/angular-cli/issues/11451#issuecomment-424715627
@jesussobrino I use a major class in my body and use the environment to exchange it in the view. This causes a lot more css code but works pretty well. This also allows changeable themes as a nice sideeffect.
import { Component } from '@angular/core';
import {environment} from '../environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
constructor() {
document.body.classList.add(environment.theme);
}
}
@richardtreier 's workaround didn't work for me (Angular 7.2) but I managed to achieve similar results (I guess) by redefining the styles entrypoint for each configuration.
in angular.json:
"configurations": {
"my-theme": {
"styles": ["src/product_configurations/my-theme/styles.scss"]
},
}
Then for instance inproduct_configurations/my-theme/styles.scss:
@import "../../styles"; // Still get the common styles first
body.my-theme { // my-theme-specific styling
@import "components"; // my-theme-specific components styling under product_configurations/my-theme
}
@Javarome This is how I did it too. Thinking more about the topic, I came to the conclusion that working with css variables is the best you can do. There is a polyfill for IE9+. Using var() will save you a lot of css code in the final build.