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

Craco start and craco build causing different css ordering #12

Open
ndbroadbent opened this issue Jan 17, 2019 · 9 comments
Open

Craco start and craco build causing different css ordering #12

ndbroadbent opened this issue Jan 17, 2019 · 9 comments

Comments

@ndbroadbent
Copy link
Member

From: dilanx/craco#57

We recently moved over to craco from react-app-rewired, in conjunction with ant-design. However the css is applied differently when running locally compared to running the completed build. Below is the contents of the craco.config.js file.
test
const CracoAntDesignPlugin = require('craco-antd');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const WebpackBar = require('webpackbar');
module.exports = {
plugins: [{ plugin: CracoAntDesignPlugin }],
webpack: {
plugins: [
new WebpackBar({ profile: true }),
],
},
};

Is this the correct way to ensure that the ant-d styles will be applied last, or have I missed something?

@ndbroadbent
Copy link
Member Author

@PatchOnKnee This config looks correct to me, but this is probably a bug.

There are a few differences between the dev and prod config, but I'm not sure why those would change the order of the CSS.

Is this the correct way to ensure that the ant-d styles will be applied last

I think it would make sense to apply the ant-d CSS first, so that you can override it with your own custom CSS? Sorry I'm not sure if I fully understand the issue.

@icecoldmax
Copy link

I'm getting a similar issue I think - the customizeTheme options are being applied differently in build than with start.

  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        customizeTheme: {
          "@primary-color": "#337AB7",
          "@layout-header-background": "#252D3B",
        }
      }
    }
  ]

The @primary-color is applied correctly when running with craco start, but with craco build it goes back to default ant-d colours.

Looking in the Chrome inspector at an AntD button of type primary:

craco start - primary colour override in <style> tags:

craco-start

craco build - primary colour override in mixin.less and deactivated:
craco-build

... and the default colour is being applied in <style> tags like with craco start.

@kmcaloon
Copy link

@ndbroadbent I can confirm that this is definitely due to the stylesheet chunks being injected in the wrong order during build. I'm assuming this has something to do with the less loader, but can't really seem to spot the issue

@kmcaloon
Copy link

Actually this is more likely related to miniCssExtractt. I also don't think the plugin options are properly modifying the loader rules for build. For example, when I experiment and modify the chunkFilename option for miniCssExtractPLuginOptions, it has no affect on the build output.

@chetanpan
Copy link

Hi,

I am facing same!

After building, I have to change order of links to stylesheets manually in index.html to make things work correctly...

Regards

@ndbroadbent
Copy link
Member Author

Hi everyone, I've started looking into this but am having some problems reproducing the issue.

I've created a test-app and a puppeteer test that builds the production assets, serves them, opens up the page in Chrome, and checks that the CSS overrides are working properly. It also clicks a button to make sure that a counter state increments.

Could someone please take a look at this puppeteer test and let me know how I can reproduce this CSS ordering issue? Thanks!

(I'm also working on #10, to get the jest tests working for App.test.tsx.)

@JacksonBates
Copy link

I'm just leaving my notes here for potential weary travelers!

I encountered a related issue to this CSS ordering warning coming from MiniCssExtractPlugin. The warnings were making my CI fail, but the app worked fine.

I decided to suppress the warnings, as outlined here: https://github.com/webpack-contrib/mini-css-extract-plugin/blob/v0.8.0/README.md#remove-order-warnings

However, MiniCssExtractPlugin already exists in the original webpack config, so I had to replace it with a new one, with ignoreOrder set to true. My final craco.config.js looks like this:

const CracoAntDesignPlugin = require("craco-antd");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  webpack: {
    configure: (webpackConfig, { env, paths }) => {
      webpackConfig = {
        ...webpackConfig,
        plugins: [
          ...webpackConfig.plugins.filter((element) => {
            if (element.options) {
              return !element.options.hasOwnProperty("ignoreOrder");
            }
            return true;
          }),
          new MiniCssExtractPlugin({
            filename: "static/css/[name].[contenthash:8].css",
            moduleFilename: this.moduleFilename,
            ignoreOrder: true,
            chunkFilename: "static/css/[name].[contenthash:8].chunk.css",
          }),
        ],
      };
      return webpackConfig;
    },
  },
  plugins: [{ plugin: CracoAntDesignPlugin }],
};

I'd love a better way of targeting the original MiniCss plugin to remove it than the naive filter I used - but this was a 1am decision after many hours trying to resolve the issue - happy to hear a better way.

Hope this helps someone!

@cpconcertage
Copy link

I'm just leaving my notes here for potential weary travelers!

I encountered a related issue to this CSS ordering warning coming from MiniCssExtractPlugin. The warnings were making my CI fail, but the app worked fine.

I decided to suppress the warnings, as outlined here: https://github.com/webpack-contrib/mini-css-extract-plugin/blob/v0.8.0/README.md#remove-order-warnings

However, MiniCssExtractPlugin already exists in the original webpack config, so I had to replace it with a new one, with ignoreOrder set to true. My final craco.config.js looks like this:

const CracoAntDesignPlugin = require("craco-antd");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  webpack: {
    configure: (webpackConfig, { env, paths }) => {
      webpackConfig = {
        ...webpackConfig,
        plugins: [
          ...webpackConfig.plugins.filter((element) => {
            if (element.options) {
              return !element.options.hasOwnProperty("ignoreOrder");
            }
            return true;
          }),
          new MiniCssExtractPlugin({
            filename: "static/css/[name].[contenthash:8].css",
            moduleFilename: this.moduleFilename,
            ignoreOrder: true,
            chunkFilename: "static/css/[name].[contenthash:8].chunk.css",
          }),
        ],
      };
      return webpackConfig;
    },
  },
  plugins: [{ plugin: CracoAntDesignPlugin }],
};

I'd love a better way of targeting the original MiniCss plugin to remove it than the naive filter I used - but this was a 1am decision after many hours trying to resolve the issue - happy to hear a better way.

Hope this helps someone!

Very helpful. I didn't even have time to become a weary traveler!

@olihou
Copy link

olihou commented Aug 20, 2020

This is a hope that is bug will be resolved ?

kwajiehao added a commit to isomerpages/isomercms-frontend that referenced this issue Jun 17, 2021
This reverts commit 17276df.

We are reverting PR #511 because we found out that the outputs
of `craco start` and `craco build` were different, resulting in
CSS files being imported in an order different to what is intended.

The workarounds we found online were convoluted and would introduce
unnecessary complexity to the repo. Furthermore, we also found
a library (react-app-rewire-alias) which can help us achieve aliasing
with CRA as an alternative to Craco. In addition to the bug associated
with Craco here, Craco is also overkill since it's a powerful tool which
helps users to extend their default CRA configs for webpack among other
things.

As such, we are reverting this commit and will be attempting to
reintroduce aliases at a later date.

A sample of how to use react-app-rewire-alias can be found in the
vaccinegovsg repo https://github.com/datagovsg/vaccinegovsg-clinic/blob/dev/frontend/config-overrides.js

References:
- dilanx/craco#57
- DocSpring/craco-antd#12
kwajiehao added a commit to isomerpages/isomercms-frontend that referenced this issue Jun 17, 2021
* Revert "style: introduce aliasing, sort imports (#511)"

This reverts commit 17276df.

We are reverting PR #511 because we found out that the outputs
of `craco start` and `craco build` were different, resulting in
CSS files being imported in an order different to what is intended.

The workarounds we found online were convoluted and would introduce
unnecessary complexity to the repo. Furthermore, we also found
a library (react-app-rewire-alias) which can help us achieve aliasing
with CRA as an alternative to Craco. In addition to the bug associated
with Craco here, Craco is also overkill since it's a powerful tool which
helps users to extend their default CRA configs for webpack among other
things.

As such, we are reverting this commit and will be attempting to
reintroduce aliases at a later date.

A sample of how to use react-app-rewire-alias can be found in the
vaccinegovsg repo https://github.com/datagovsg/vaccinegovsg-clinic/blob/dev/frontend/config-overrides.js

References:
- dilanx/craco#57
- DocSpring/craco-antd#12

* lint: correct linting error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants