CSS Problem on --prod build
Versions
Angular CLI: 1.5.3
Node: 9.2.0
OS: linux x64
Angular: 5.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.5.3
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.36
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.3
@schematics/angular: 0.1.5
typescript: 2.4.2
webpack: 3.8.1
Repro steps
- Step 1 :
ng serve


I have also a masonry layout that is good.
- Step 2 :
ng serve --prod


My Masonry layout is also broken (hard to demonstrate sorry!) but i think all is related !
Observed behavior
- My progress bar turns blue!
- My masonry layout is broken!
- No errors anywhere(cli console, navigator console...) but the css is broken with a prod build!
Desired behavior
- My progress bar stays pink!
- My masonry layout stays good!
- Or At least log an error somewhere
Mention any other details that might be useful (optional)
- The --prod build is broking somthing in the css.
- I observed this since angular cli 1.5.0
- All was good with angular cli 1.4.9
I have created a reproduction for this issue. Here is the github repo.
To see it working without the prod flag: npm start
To see it working with the prod flag: npm run build -- --prod; cd www; npx local-web-server -p 4200
Also here is a screen capture showing the correct behavior on the left and the incorrect behavior on the right.
@alan-agius4 can this be reopened since we have a way to demonstrate the issue that can hopefully provide a path forward?
fixed this on angular 7, found one css file on the project : app.component.css, once renamed to app.component.scss the build is ok.
@jadhemar-MD I don't believe that to be the case for my reproduction above. If you send a PR that fixes the issue I'll humbly admit I'm wrong and buy you a beer. 😄
hi @adamduren, would be hard to send a PR for this :), what can i say is that we got the white page trouble because angular tried to load the old removed css (type MIME error in console). In our case the issue come from a forgotten css file in a component, once we moved this one from css to scss, i can confirm that fix the trouble.
Maybe my build config can help (angular 7):
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"serviceWorker": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
Thanks to @Deccoy 's post who show me the way https://github.com/angular/angular-cli/issues/8577#issuecomment-359198319
@jadhemar-MD I don't think this applies in my case. Thanks for tips though!
I did some work to remove Ionic to rule that out as a factor and the issue without Ionic still occurs.
Here are the results of my testing. I tried multiple variations of options by modifying angular.json. The only time for me when it consistently worked as expected was with aot: false. buildOptimizer is included as a column as you cannot keep buildOptimizer: true when aot: false. I'm guessing when setting via CLI it takes care of this for you.
So in my case it seems that AOT is the issue.
Base Options:
{
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
* means no change from original
| aot | buildOptimizer | extractCss | Result |
|---|---|---|---|
| * | * | * | FAIL |
| * | false | * | FAIL |
| * | * | false | FAIL |
| false | false | * | PASS |
| false | false | false | PASS |
Ok, I've figured it out at least for my case. See this commit.
In my more complex example I had a base styles object that was being spread into various style function objects to be DRY like so. style({ ...baseStyles, }).
Essentially AOT breaks the animation because it doesn't spread the base styles into object passed to the style function. I was able to determine this by looking at the AOT output.
If you checkout that repo and switch between the last two commits with a prod build you will see the bug in action. Essentially it should switch between a red and blue square and never be green. If it's green then you know the styles for the animation states are not being applied.
I now know a workaround although it's less than ideal.
Considering creating a separate issue since after testing it's possible these two are unrelated.
I now know a workaround although it's less than ideal.
@adamduren Can you tell us about the workaround? I still have the problem with
Angular CLI: 7.3.9
Node: 10.12.0
OS: darwin x64
Angular: 7.2.15
does anyone got solution for this problem with angulat cli 7.3.9 version
In my case, i created custom element (microapp) using angular elements, and inside base application i declared css variables, inside microapp i used them, basic motive is to pass styles from base app to micro app using css variables. It is working fine on my local. But i hosted microapp on azure and inside base application i provided hosted microapp's path.microapp is rendering properly but styles are not passing from base appication to micro application using css variables.
does anyone has any idea about same.
any updates on this?
It turned out to be an interesting case. I removed an option "preserveWhitespaces" from my tsconfig.app.json it fixed my problem with style differences between AOT build and development.
I used angular CLI, default configuration of Tsconfig doesnt have preserveWhitespaces in it. If i add this field into tsconfig, still it is not working
Had the same issue with Angular CLI: 8.1.0 and switching to "extractCss": false fixed it for now.
But I think this issue should not be closed.
Had the same issue with
Angular CLI: 8.1.0and switching to"extractCss": falsefixed it for now. But I think this issue should not be closed.
Still have this issue in angular:
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, material-moment-adapter
... platform-browser, platform-browser-dynamic, router
... service-worker
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/pwa 0.800.3
@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
Is possible that this can interfere? ->
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
As above said, I fixed it by edit package.json."build": "npm run color-less && ng build --prod --build-optimizer --extract-css=false",.Can anyone explain me why it works?🤔
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.3.9
Node: 10.15.3
OS: darwin x64
Angular: 7.2.15
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.9
@angular-devkit/build-angular 0.13.9
@angular-devkit/build-optimizer 0.13.9
@angular-devkit/build-webpack 0.13.9
@angular-devkit/core 7.3.9
@angular-devkit/schematics 7.3.9
@angular/cdk 7.2.2
@angular/cli 7.3.9
@ngtools/webpack 7.3.9
@schematics/angular 7.3.9
@schematics/update 0.13.9
rxjs 6.3.3
typescript 3.2.4
webpack 4.29.0
Having the same with "@angular/cli": "6.0.0". Can confirm that "extractCss": false resolves the problem. I was unable to track back which changes actually made the css processing faulty, since extractCss: true was working up until now.
The bug still persists in Angular v8.2.5 (@angular/cli: 8.3.4)
Using "extractCss": false in angular.json OR adding the --extract-css=false flag to the ng build --prod command does seem to solve the problem.