Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS import order differs between dev and prod #549

Open
chris-hailstorm opened this issue May 15, 2020 · 1 comment
Open

CSS import order differs between dev and prod #549

chris-hailstorm opened this issue May 15, 2020 · 1 comment
Labels
bug/confirmed We have confirmed this is a bug help wanted topic/config Babel, Webpack, ESLint, Prettier, etc. topic/web

Comments

@chris-hailstorm
Copy link
Contributor

chris-hailstorm commented May 15, 2020

ENVIRONMENT

0.6.0

OBSERVATION

CSS styles change between dev and prod.

In index.js, I have something like this:

import 'src/styles/scaffold.css'
import 'src/styles/extras.css'
import 'semantic-ui-css/semantic.css'  // lives under node_modules/semantic-ui-css

When I run my app in dev mode, later CSS imports override earlier ones, as expected.

In production mode, the ordering is different and different styles (lack of override) result.

Devs expect there would be no difference in final styles just due to dev vs. prod mode.

ANALYSIS

Redwood uses different webpack loaders based on mode, using MiniCssExtractPlugin in prod but not in dev. The resulting ordering-of-imports issue is a known problem for webpack:

WORKAROUNDS

I used two different workarounds to solve this issue (poorly):

  1. used web/config/webpack.config.js to drop the MiniCssExtractPlugin loader, which makes prod mode the same as dev mode for CSS files -- delivers the expected styles, but without the performance boost provided by that loader (why it was used in the first place):
  config.module.rules[0].oneOf[5] = {
    test: /\.css$/,
    use: [
      'style-loader',
      'css-loader',
    ],
  }
  1. copied the CSS file from the node_modules subfolder and updated my import to use that file sitting next to scaffold.css -- works but I have to manually update if that package is updated:
import 'src/styles/scaffold.css'
import 'src/styles/extras.css'
import 'src/styles/semantic.css'  // lives with other RW CSS files

RECOMMENDATION

CSS files should be processed in the order they're imported, regardless of which loader(s) are used. That means a later import will reliably override a style defined in an earlier import. This is a common way to tune styles without changing an original .css or .scss file.

We should either research a different recommended way to use the existing webpack plugins (which looks to be difficult) or possibly write a custom plugin to ensure the right ordering.

@thedavidprice thedavidprice added bug/confirmed We have confirmed this is a bug topic/config Babel, Webpack, ESLint, Prettier, etc. labels May 15, 2020
@brianespinosa
Copy link

@chris-hailstorm do you have a repo that exhibits this problem with Redwood? I was digging through Mini CSS Extract Plugin and it does not appear that there is any intention by those maintainers to resolve the problem as they feel it is a non issue and should be controlled in different ways.

It seems like it would potentially be better to have CSS that is not optimized and consistent than to have it be optimized and inconsistent (your first use case). If you have a working repo that consistently reproduces this problem, I wanted to look into using the optimize-css-assets-webpack-plugin instead and see if we get similar optimization of styles. Maybe using a different plugin to optimize will get us where we want to go.

@jtoar jtoar removed the topic/config Babel, Webpack, ESLint, Prettier, etc. label Jun 4, 2021
@jtoar jtoar added this to v1 priority in Current-Release-Sprint Jun 4, 2021
@jtoar jtoar added this to New issues in Current-Release-Sprint via automation Jun 9, 2021
@jtoar jtoar moved this from New issues to On deck (future release) in Current-Release-Sprint Jun 9, 2021
@jtoar jtoar removed this from On deck (future release) in Current-Release-Sprint Jun 9, 2021
@jtoar jtoar added this to v1 priority in Triage Jun 9, 2021
@jtoar jtoar added this to New issues in Current-Release-Sprint via automation Jun 10, 2021
@jtoar jtoar moved this from New issues to On deck (future-release; help-wanted) in Current-Release-Sprint Jun 10, 2021
@thedavidprice thedavidprice moved this from On deck (v1-rc priorities; help-wanted) to Icebox (post v1-RC priorities) in Current-Release-Sprint Jun 10, 2021
@jtoar jtoar removed this from Icebox (post v1-RC priorities) in Current-Release-Sprint Dec 9, 2021
@jtoar jtoar added topic/config Babel, Webpack, ESLint, Prettier, etc. topic/web labels Dec 13, 2021
@jtoar jtoar removed the v1/priority label Apr 15, 2022
@simoncrypta simoncrypta self-assigned this May 17, 2022
@simoncrypta simoncrypta removed their assignment Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug help wanted topic/config Babel, Webpack, ESLint, Prettier, etc. topic/web
Projects
Status: Backlog
Development

No branches or pull requests

5 participants