stencil-cli icon indicating copy to clipboard operation
stencil-cli copied to clipboard

Feature Request: Remove unused CSS from compiled theme stylesheet

Open tomdiggle opened this issue 5 years ago • 13 comments

It would be great if stencil-cli could remove used css from production files to help control the size of the files using something like Purgecss.

tomdiggle avatar Nov 24 '19 17:11 tomdiggle

@tomdiggle pull requests are welcome :)

junedkazi avatar Mar 26 '20 12:03 junedkazi

Every BigCommerce theme would benefit from this feature and I would be happy to help and submit a PR. Here are my thoughts on how this feature could look.

Add a new --optimize flag to stencil start to run a local server with unused css removed. This can be used to test locally and makes sure everything works with your site before creating a new release. Any unused css will be removed when running bundle, release and push without having to use the --optimize flag.

What are your thoughts on this @junedkazi?

tomdiggle avatar Mar 27 '20 11:03 tomdiggle

@tomdiggle this is a great idea, but having hard time with the "unused" identification process. I have been looking into this using minimalcss, but the problem is you need an inventory each page so can be identified on what is needed. Did you have some idea on how this would work?

cc @hectorfhurtado

carsonreinke avatar Mar 27 '20 13:03 carsonreinke

@tomdiggle couple of questions around how it will work;

  • If you introduce it as as flag as part of stencil start won't it start cleaning up css as you are making changes to your files or changing id/class names across your templates. You might end of loosing work for an unintended change.
  • Also how do we know which file was cleaned to test the exact page ? Are we going to display on the console as to which file/line was cleaned up ?
  • How will this work with css imports ?
  • What happens with scss files do they continue to hold the unused css ?

junedkazi avatar Mar 27 '20 13:03 junedkazi

What happens with scss files do they continue to hold the unused css ?

The scss files would continue to hold the unused css. We only want to remove unused css from the compiled css stylesheet being used by the theme inside the <head> tags.

tomdiggle avatar Mar 27 '20 21:03 tomdiggle

@carsonreinke I've looked into it a little bit and I'm unsure what is the best way to go about identifying what css is unused.

When running stencil start the css compiling is handled by the cssHandler function located in /server/plugins/theme-assets/theme-assets.module.js. I've tried using uncss (see image below) to remove the unused css by using request.info.referrer as the url. This failed because a javascript error theme-bundle.head_async.js could not be loaded.

Screenshot 2020-04-04 at 15 04 07

Maybe @junedkazi can offer some insight on how to go about using the raw html as a reference point?

tomdiggle avatar Apr 04 '20 14:04 tomdiggle

@tomdiggle I think this is making an assumption that Sass is compiled on each request, I'm not positive, but pretty sure it is not.

Unless BigCommerce would offer more information or confirm on what compiles the Sass, the approach would have to pre-compile Sass instead with WebPack instead of having it generated by the server. This also offers the ability to have async loading, spitting, etc. Pretty sure BigCommerce would be glad to be rid of this. We have done some work on this and it can be done.

If that was done, you could then use uncss (or some other tool) to provide a list of each URL locally, so that during theme bundling, the Sass file that is built, has the extra CSS removed.

Maybe I am over complicating this, but that is how I understand this.

cc @hectorfhurtado

carsonreinke avatar Apr 10 '20 17:04 carsonreinke

@carsonreinke Agreed, hopefully it's something that BigCommerce can add soon. It would be a great feature to have.

tomdiggle avatar Apr 18 '20 15:04 tomdiggle

the approach would have to pre-compile Sass instead with WebPack instead of having it generated by the server.

If I am not completly mistaken, this change would in general allow theme developers to employ different strategies for their post processing etc.

0dp avatar May 08 '20 07:05 0dp

@0dp oh, LOL, you found it!

this change would in general allow theme developers to employ different strategies for their post processing

Yes, it would be all done when packaging the theme.

carsonreinke avatar May 11 '20 21:05 carsonreinke

@0dp oh, LOL, you found it!

this change would in general allow theme developers to employ different strategies for their post processing

Yes, it would be all done when packaging the theme.

This would be amazing!

wearegoose avatar May 11 '20 22:05 wearegoose

Any updates on this one? I'm guessing as its all baked into stencil-cli we cant do much?

ImTheDeveloper avatar Jan 09 '22 13:01 ImTheDeveloper

I am just curious if something has moved here.. happy to help !!

syedazam avatar Jan 24 '22 15:01 syedazam