-
-
Notifications
You must be signed in to change notification settings - Fork 601
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
Support Tree shaking of CSS #506
Comments
Send PR for discussion how this would/could look like, but tbh I have no idea how |
There was a PR submitted but it did not get rebased and was closed because of no activity #402 Not sure if something similar can be implemented where css-loader uses ES6 |
The conversion to ES2015 Modules will definitely by coming soon, but this is a joined effort with changes needed in |
Confirm that all css-loader needs to do to support CSS treeshaking is to emit ES2015 modules. The "deadcss" plugin runs a sub-compilation and checks the used exports of css-loader. |
any update on this? |
in |
@evilebottnawi Do you have any ideas to implement this in webpack 5? Maybe only for css modules.. |
@vankop i think we need postcss plugin for this |
Maybe my vision is mistaken, but how you want to tree shake css without webpack plugin? You need to understand which css classes does not imported in JS. |
@vankop to be honestly, i think it is impossible, i really don't have ideas how we can remove unused selectors, we don't have infromation about which selectors used, so we can't remove them, also it is not safe, because you can asynchronously load CSS file and use any logic so removing selectors is not safe |
@evilebottnawi actually, such information is in css modules i use just naruto, sasuke must be remove from bundle |
@MaxmaxmaximusGitHub CSS has side effect, we don't know where you can use classes, you can just put them in |
@evilebottnawi If you're using CSS Modules then all class names are local and can't be referenced globally. I had this working in 2017 (#402 and various other plugins) but it involved a lot of breaking changes and there didn't seem to be the will to get it merged |
I just checked and the demo project I created still works: https://github.com/simlrh/dead-css-loader-demo Styles all import from the semantic ui css, which is 290kb minified, and the resulting bundle.js (including the entire app, react, etc) is only 179kb |
And Yes and No, you still inject styles in DOM, so they still have side effects, and in theory you can use it in any file (just write hashing class 😄 ), I think we can implement plugin for this, it is not loader work |
@alexander-akait Is this question even valid?
|
We still need, because we can remove unused CSS classes from a CSS file |
So this issue can be closed? |
No |
@alexander-akait So is it still possible to implement tree-shaking of unused CSS classes using |
It is possible, but I don't think we will implement it here, but if someone want it - feel free to send a PR, I want to keep this open |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Webpack 2 support tree shaking and removal of dead code but this does not work for CSS. If we have a javascript file which exports different CSS files and we only import some of then, then css loader does not tree shake and remove the unused CSS.
Example if we had the code below, css-loader currently is including CSS for both Checkbox and Button but it should only include code for Checkbox since Button is never imported:
What is the expected behavior?
css-loader should tree shake and remove unused CSS import when used webpack 2 with tree shaking feature.
If this is a feature request, what is motivation or use case for changing the behavior?
This will help out reduce the CSS bundle size out of box when using webpack 2
Please mention other relevant information such as your webpack version, Node.js version and Operating System.
This will work with webpack 2.
The text was updated successfully, but these errors were encountered: