r/reactjs • • Aug 23 '20

Resource A CLI tool that scans your project and splits the one big global CSS file into component level css files.

562 Upvotes

32 comments sorted by

20

u/CaptainOnBoard Aug 23 '20

As the title says, I made a CLI that takes in the path to the global css file and splits that into module level css files.

REPO : https://github.com/royalbhati/CSSplit

10

u/BoutTheGrind Aug 23 '20

As a CSS noob that's currently learning it with my react project right now, can you explain to me what the benefit is of this tool? Isn't part of the benefit of css that you don't have to repeat a lot of styles because they can be inherited from parents?

6

u/CaptainOnBoard Aug 23 '20

Having a big css file hurts your web performance because every time you load a page you are loading extra css that is not even needed.

Also it is very easy to work on modular code when the codebase is large.

While this tool does not help you write modular css, it's for codebases where css is written in one large file and if they want to make it modular.

11

u/[deleted] Aug 23 '20

not in every case! i believe if you have repeated styles there’s a possibility of actually increasing how much css gets loaded because of duplicate styling. (not in your case, probably)

i know modular css is easier to maintain (in the same vein as css-in-js, which i prefer) but ideally we would be using a build tool that compiles an atomic css file from modular css + code splits to reduce total data over the wire

1

u/ChangeYourBrain Aug 23 '20

Yeah, ideally you would only use what is absolutely necessary. I was analyzing the Stripe website and found that they are definitely doing this, but I’m unsure how the end result is compiled. It’s not really worth it if it’s hard for devs to use.

8

u/nathanjd Aug 23 '20

It only hurts on first page load. From then on it is cached.

3

u/BoutTheGrind Aug 23 '20

Ah, that makes sense. Thanks for the insight, and congrats on launching your tool!

3

u/swyx Aug 23 '20

you asked a good question. you only run into this after maintaining css for a while and seeing how you get into an unmaintainable "append-only css" shitshow by using css the way it was originally designed. BEM was a stopgap solution, now we have better tools to address this problem (incl css modules, css in js, and utility css)

note that other frameworks (vue, svelte) just went ahead and built in scoped css to their component designs since it is such a common problem. i've proposed building it in to a react sfc myself.

17

u/Chaos_Therum Aug 23 '20

A lot of people have gotten away from having global css and have moved towards module based css with things like styled components in order to modularize components rather than saving a couple of lines of css.

46

u/ftfymf Aug 23 '20

Nice, how does it handle rules used in multiple components? Does it repeat them in each component css file?

27

u/CaptainOnBoard Aug 23 '20

For now, yes.

I created this when I was working on codebase that had huge css files and it would have taken weeks to make it modular considering the size.

I haven't put much time into it, but if people really want that stuff I might work on it a bit more seriously.

6

u/DasBeasto Aug 23 '20

Does it handle nested css well? Very cool idea! I’m still looking for a good tool to strip out unused CSS rules if you want a next project!

8

u/gimp3695 Aug 23 '20

Unused css rules out be awesome to discover

5

u/tandrewnichols Aug 23 '20

You want PurgeCSS. It generates a CSS bundle from only the styles used in your application. There's a PostCSS plugin for it, so you can do autoprefixing and minification the same time.

2

u/flurpandzurp Aug 23 '20

Is this a problem nowadays, though? Sweet tool though, regardless

11

u/tandrewnichols Aug 23 '20

At large companies with old code bases? 100% a problem still.

3

u/swyx Aug 23 '20

i mean. i worked at a company with 3 frontend devs and a 4ish year old codebase. was already a problem. i think just age alone will do it regardless of team size.

1

u/tandrewnichols Aug 23 '20

Yeah, definitely a problem outside big companies with old code bases too. That's just the easiest example to point to. Honestly, I'm curious now where u/flurpandzurp worked that it WASN'T a problem.

1

u/ChangeYourBrain Aug 23 '20

IMO it’s mostly an issue because of laziness * time

1

u/flurpandzurp Aug 24 '20

Yep good point. My mind instantly went to new projects. D’oh.

1

u/Slapbox Aug 23 '20

This would have saved me many hours if I had it a year ago.

2

u/leodriesch Aug 23 '20 edited Aug 23 '20

Sorry for off-topic, but how can you put the terminal in the bottom left corner in VSCode?

Edit: Apparently you can just drag the terminal tab to the sidebar and it just works.

1

u/Attack_Bovines Aug 23 '20

Bring up your command palette ctrl (or cmd on macOS) + shift + p, then type “Terminal”. You want “Terminal: Create New Integrated Terminal”. The keyboard shortcut should show on the right side of the command palette result list item.

1

u/DVGY Aug 23 '20

This is pro level stuff, how can i implement a library which read jsx files and output className attributes values

1

u/abudabu Aug 23 '20

Nice. Does it infer what bits of the CSS to extract by analyzing an HTML file? Does it accept jsx files? What does "exclude" do? This would be good material for the README.

0

u/all-i-know-is-code Aug 23 '20

This is super cool - I’m doing exactly this by hand but for my Vue project at work 😭 wish I could use this but great work

-1

u/fuzulus Aug 23 '20

This is cool and I will definitely try it, but I'm interested if you can parse .html.twig files also?

2

u/CaptainOnBoard Aug 23 '20

I haven't had a chance to work on twig so I dont know how they look but it should work on any file that has html like syntax(start tags and closing tags)

1

u/fuzulus Aug 23 '20

Yeah, makes sense, but it wouldn't work in most cases then, which is okay. There are a few workarounds possible, awesome tool either way. :)

2

u/[deleted] Aug 23 '20

[removed] — view removed comment

2

u/CaptainOnBoard Aug 23 '20

I haven't tried it

I created this when I was working on legacy codebase that had huge css files and it would have taken weeks to make it modular considering the size.

I haven't put much time into it, but if people really want that stuff I might work on it a bit more seriously.