manfredsteyer / ngx-build-plus

Extend the Angular CLI's default build behavior without ejecting, e. g. for Angular Elements
1.19k stars 136 forks source link

Order of CSS "styles" array in angular.json is not preserved #380

Closed ktsangop closed 1 year ago

ktsangop commented 1 year ago

Hello everyone, I have noticed a strange behavior which does not seem to be compatible with the official angular builder. On our angular.json configuration, under architect.build.styles we have an array of external (node_modules) styles that we import like this:

            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "node_modules/primeicons/primeicons.css",
              "node_modules/primeng/resources/primeng.min.css",
              "node_modules/fullcalendar/dist/fullcalendar.min.css",
              "node_modules/font-awesome/css/font-awesome.min.css",
              "node_modules/prismjs/themes/prism.css",
              "src/styles.scss"
            ],

When building with the default angular builder, the order of CSS styles follows the order of the array above, meaning that src/styles.scss always overrides any styles added by e.g. bootstrap.min.css.

But when using ngx-build-plus this order is not preserved (not sure if it's reversed or not), and the only way to have styles.scss at the bottom of the compiled output, is to put it 1st (first) on the list above.

I have not found any official Angular documentation on how the builder processes this list, but according to popular belief, the last item of the list should be last on the output CSS (see https://github.com/angular/angular-cli/issues/9475)

Is there any info on how we should expect ngx-build-plus to process this list? Thank you in advance!

p.s. Dependencies on our project are as follows:

"dependencies": {
    "@angular-architects/module-federation": "14.3.10",
    "@angular/core": "^14.0.6",
}...
"devDependencies": {
    "@angular-devkit/build-angular": "14.0.6",
    "@angular/cli": "^14.0.6",
    "@angular/compiler-cli": "^14.0.6",
...
    "ngx-build-plus": "^14.0.0",
...

Reproduce

  1. Clone the 2 following projects on your machine https://gitlab.com/ktsangop/default-builder-test https://gitlab.com/ktsangop/ngx-build-plus-test
  2. Run yarn build:prod or npm build:prod for both projects
  3. Open the compiled styles file /dist/{project_name}/styles.{random_hash}.scss file in both projects
  4. You should see that the order of the styles is reversed in the project that uses ngx-build-plus as builder

Specifically the ngx-build-plus output should be

.style-main{color:"blue"}.style2{color:"green"}.style1{color:"red"}

In contrast the default Angular builder's output shoud be:

.style1{color:"red"}.style2{color:"green"}.style-main{color:"blue"}

Note that the order of CSS files in both projects' angular.json build configuration is:

...
"styles": [
  "src/style1.css",
  "src/style2.css",
  "src/styles.scss"
]...
ktsangop commented 1 year ago

For what it's worth, this seems to have been fixed in version 15.0.0 of the plugin. I have created a new branch on the reproduce repo with upgraded packages to Angular 15: https://gitlab.com/ktsangop/ngx-build-plus-test/-/tree/angular-15 which seems to be working as expected.

Looking at the changes made between the 2 versions https://github.com/manfredsteyer/ngx-build-plus/commit/d519571e25c36184d0f143179eabe24a71c24155 I am not sure how this could have been resolved.

The only thing that could have affected this is the "@angular-devkit/build-angular" dependency