最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CSS optimization to auto change class names - Stack Overflow

programmeradmin5浏览0评论

I am looking for a tool to optimize my website's plete build. I am using SASS as a CSS preprocessor and Uglify to create my JavaScript files, and a couple of more tools all tied together with Grunt. Also, the markup is in this case a single index.html file, but in other projects it's in either PHP files or in Liquid templates.

I am using BEM (block, element, modifier) to name my classes and sometimes, that leads to quite huge classnames, such as navigation__item__link--home or even longer.

I am looking for a tool that automatically converts the final, built CSS files in a way that it changes the class or id names to something like .a, .b, but changes every HTML and JavaScript files according to the same method, so that correlated codes won't get messed up.

I want to keep my original project files of course, so I would like the tool to be capable of creating new files according to these rules and preferably, capable of using it with Grunt.

I am looking for a tool to optimize my website's plete build. I am using SASS as a CSS preprocessor and Uglify to create my JavaScript files, and a couple of more tools all tied together with Grunt. Also, the markup is in this case a single index.html file, but in other projects it's in either PHP files or in Liquid templates.

I am using BEM (block, element, modifier) to name my classes and sometimes, that leads to quite huge classnames, such as navigation__item__link--home or even longer.

I am looking for a tool that automatically converts the final, built CSS files in a way that it changes the class or id names to something like .a, .b, but changes every HTML and JavaScript files according to the same method, so that correlated codes won't get messed up.

I want to keep my original project files of course, so I would like the tool to be capable of creating new files according to these rules and preferably, capable of using it with Grunt.

Share Improve this question edited Apr 17, 2014 at 7:57 try-catch-finally 7,6446 gold badges49 silver badges71 bronze badges asked Apr 9, 2014 at 10:01 pentzzsoltpentzzsolt 1,1311 gold badge10 silver badges19 bronze badges 8
  • why exaclty would you care to use BEM in the first place? in my view, css preprocessors help mitigate the pain of unstructured, intransparent rules definition. if you don't like the style of a convention, drop the convention instead of introducing a workaround to that convention. – Jakob Hohlfeld Commented Apr 10, 2014 at 13:19
  • I love every single aspect of the convention and from a development point of view, not only is it optimal, in my point of view it's heaven itself. I am trying to separate development from deployment here and by the way, this sort of optimization can be helpful not only if you use BEM, but anyway, in this case you just win more. – pentzzsolt Commented Apr 10, 2014 at 20:04
  • You must have users with a very slow connection or an extreme amount of users to be worried about the length of class names. With a 1000 classes (defined or called) and an average of 20char reduction (which is a lot) we are talking about 2,5kB per user/per session with proper caching. – Hugo Delsing Commented Apr 14, 2014 at 8:25
  • We did benchmark the potential advantage in our CSS file by manually overriding the classnames one by one and paring that to the original file, and the results were significant. Compared to the original, minified file, which was 54kb, the new file, also minified of course, was only 36kb, which is a 33% drop only in the CSS. I don't know where your data is ing from, but I would love to see an actual benchmark result. – pentzzsolt Commented Apr 14, 2014 at 8:51
  • 1 Well, I believe that any website always benefits from every single byte of drop in file size, of course the cutoff of where it begins to be worth of the work that you put into it depends in every case. In my case, 20kb shouldn't be crucial, but would be worth the extra work. :) – pentzzsolt Commented Apr 14, 2014 at 11:08
 |  Show 3 more ments

2 Answers 2

Reset to default 5 +150

It sounds like you are looking for a tool called munch. It is a pre-deployment tool that does just what you are asking for. It is a mand line utility that you point at your view (html), css and js files and it will shorten class names and ids while still keeping them unique and keeping referential integrity intact between all of the source files. There are also some handy options, like the ability to output a map file that lists what transformations it made. The documentation is pretty straight-forward.

FWIW, I am not affiliated with the project.

I would remend Grunt for this.

The sass and css can be done with existing grunt-sass plugin and the javascript can use the grunt-uglify plugin.

While I'm not sure if there is a plugin that already does the custom uglification, you could easily write a plugin to do so. Grunt plugins are written in node, so it's pretty approachable if you're familiar with javascript.

发布评论

评论列表(0)

  1. 暂无评论