-
Notifications
You must be signed in to change notification settings - Fork 34
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
Dead code elimination #13
Comments
Hey, this is a great idea, and we've been talking about it a bit, so I wanted to change the title and kick off the discussion. So, this is really hard. Right now, each file gets processed independently, so no file knows which parts of it are being used. If we change that, we have some problems:
So, theoretically I can see a mechanism where each file has sort of a line-by-line shadow map, where files are injected into some kind of intermediate area, and each time a class is referenced those lines are "illuminated", then at the "end" of the process (another tricky concept) you export all the illuminated lines and you have your file. BUT I kind of feel that's more trouble than its worth. I think a smarter approach is something like uncss where you look at the output of a file and remove everything unused. Uncss is super complex because it has to deal with real-world global selectors by firing up PhantomJS and jumping around your site. I think with CSS Modules we have the ability to do something better. Perhaps a command-line utility that you point at your "entry" CSS files, walks the tree and accumulates all the exported classes that you use, then looks through your bundled CSS and deletes anything you're not exporting? Anyway, yes I think we should do this... somehow :) |
Perhaps I'm thinking of something more rudimentary. It would be difficult to eliminate styles that are For example we have (taken from here https://github.com/css-modules/postcss-modules-extract-imports/tree/master/test/test-cases/import-multiple-classes): :local(.exportName) {
composes: importName secondImport from 'path/library.css';
other: rule;
} which becomes: :import("path/library.css") {
i__imported_importName_0: importName;
i__imported_secondImport_1: secondImport;
}
:local(.exportName) {
composes: i__imported_importName_0 i__imported_secondImport_1;
other: rule;
} When we process the Forgive my unfamiliarity with the actual implementation, I'm still digging through the source. |
Yeah, you're on the right track I think, but if But, once you have the full tree, you can go back over the generated CSS and delete stuff that's not present in it, for sure. |
I've implemented this here: #20 |
Would it be possible to not output styles that aren't used when composing classes?
For example:
main.css
other.css
main.js
output css
See also this failing test: #12
This functionality would be great when composing just a single class from a large css file. I'd be interested in helping implement this.
The text was updated successfully, but these errors were encountered: