johanholmerin / style9

CSS-in-JS compiler inspired by Meta's stylex
MIT License
570 stars 27 forks source link

Atomic css rule not works when component loaded as lazy #40

Closed behnammodi closed 3 years ago

behnammodi commented 3 years ago

If You have a component something like this:

import style9 from 'style9';

const styles = style9.create({
  blue: {
    color: 'blue',
  },
  red: {
    color: 'red'
  }
});

function LazyComponent() {
  return (
    <h1 className={styles('blue', true && 'red')}>I'm Lazy Component</h1>
  );
}

export default LazyComponent;

And before that you created this style in another component:

const styles = style9.create({
  blue: {
    color: 'blue',
  },
  red: {
    color: 'red'
  }
});

New CSS file generated as duplicate

johanholmerin commented 3 years ago

Could you create a repo with a reproduction?

behnammodi commented 3 years ago

Yeah

behnammodi commented 3 years ago

@johanholmerin I sent you a invite to private repo

johanholmerin commented 3 years ago

The CSS files need to be concatenated to a single file. Adding the following to your webpack config fixes the issue:

optimization: {
  splitChunks: {
    cacheGroups: {
      styles: {
        test: /\.css$/,
        chunks: 'all',
        enforce: true,
      }
    }
  }
}

I've updated the documentation to include this.

behnammodi commented 3 years ago

Why closed this issue? I added cacheGroups to config and it not works

behnammodi commented 3 years ago

Could you update https://github.com/behnammodi/style9-sample

behnammodi commented 3 years ago

I tested again, it works thanks 💪🏻, but i think we need ref to doc about CRA