简体   繁体   English

在部署之前,是否有压缩 HTML 类属性和 CSS 选择器的工具?

[英]Before deployment, is there tool to compress HTML class attribute and CSS selectors?

In current project, I was asked for compressing the HTML class attribute and corresponding CSS selectors before deployment .在当前项目中,我被要求在部署之前压缩 HTML 类属性和相应的 CSS 选择器。 For example, the code on production is :例如,生产代码是

<div class="foo">
  <div id="bar"></div>
</div>

.foo {/*Style goes here*/}
#bar {/*Style goes here*/}

On deployment , I want the HTML class and corresponding CSS selectors to be substituted:在部署时,我希望替换 HTML 类和相应的 CSS 选择器:

<div class="a">
  <div id="b"></div>
</div>

.a {/*Style goes here*/}
#b {/*Style goes here*/}

What's the available tools there to archive this compression?有哪些可用的工具可以存档这种压缩?

If you really want to rename class names (keeping in mind what Madmartigan said) Google Closure Stylesheets does that.如果您真的想重命名类名(记住 Madmartigan 所说的), Google Closure Stylesheets会这样做。 It's an overkill, and YUI Compressor or any other minification + gzipping tool should give you enough performance boost, but it can do it.这是一种矫枉过正,YUI Compressor 或任何其他压缩 + gzip 压缩工具应该可以为您提供足够的性能提升,但它可以做到。 You'll have to use other Closure tools to make appropriate changes to your .js files and html templates.您必须使用其他 Closure 工具对.js文件和 html 模板进行适当的更改。

There is also a project called "rename-css-selectors" if you handle the code with node:如果您使用节点处理代码,还有一个名为“rename-css-selectors”的项目:

https://www.npmjs.com/package/rename-css-selectors https://www.npmjs.com/package/rename-css-selectors

There are plugins for nearly every build tool (webpack, parcel, gulp, ...):几乎每个构建工具(webpack、parcel、gulp 等)都有插件:

https://github.com/JPeer264/node-rcs-core#plugins https://github.com/JPeer264/node-rcs-core#plugins

This will minify all CSS selectors in HTML, JS and CSS files (actually any file you want).这将缩小 HTML、JS 和 CSS 文件(实际上是您想要的任何文件)中的所有 CSS 选择器。 I saved 20ish% of the CSS filesize at the end.最后我节省了 20% 的 CSS 文件大小。

This is amazingly short-sighted.这是惊人的短视。

  • Step 1: Turn on GZip or Zlib compression in web server第 1 步:在 Web 服务器中打开 GZip 或 Zlib 压缩
  • Step 2: All text gets compressed, often by 70% or more第 2 步:所有文本都被压缩,通常压缩 70% 或更多
  • Step 3: There is no step 3.第 3 步:没有第 3 步。
  • Step 4: PROFIT第 4 步:利润

There's nothing wrong with minification with gzipping, even before modern browsers introduced source maps, minification was a best practice because you can still get some significant savings even when used in conjunction with gzipping.使用 gzip 压缩没有任何问题,即使在现代浏览器引入源映射之前,压缩也是最佳实践,因为即使与 gzip 结合使用,您仍然可以获得一些显着的节省。 We put up with worse readability on production because the performance improvement was worth it.我们忍受了更差的生产可读性,因为性能改进是值得的。 Now with source maps, we can have our cake and eat it too.现在有了源地图,我们就可以吃蛋糕了。 Here's a good article demonstrating the effects of combining minification with gzip on html pages for large sites: http://madskristensen.net/post/effects-of-gzipping-vs-minifying-html-files这是一篇很好的文章,展示了在大型网站的 html 页面上将缩小与 gzip 相结合的效果: http : //madskristensen.net/post/effects-of-gzipping-vs-minifying-html-files

The difference in savings you get varies very greatly depending on the glyph distribution of the code being minified, so results will vary depending on the minification strategy and the language being minified, and even just depending on coding style.您获得的节省差异很大,具体取决于被压缩代码的字形分布,因此结果将取决于压缩策略和被压缩的语言,甚至仅取决于编码风格。 In the average case the savings are still significant.在平均情况下,节省仍然很大。

Minification handles more than just condensing glyphs, it can also restructure the code to remove unneeded characters while achieving the same effect, something that gzipping can't do.缩小处理不仅仅是压缩字形,它还可以重组代码以删除不需要的字符,同时实现相同的效果,这是 gzipping 无法做到的。

Now, to get back to your specific question, in your case, you want to minify class glyphs.现在,回到您的具体问题,在您的情况下,您想缩小类字形。 This is harder to do for several reasons.由于几个原因,这很难做到。 The scoping of those glyphs are between several files, as opposed to it being possible to scope them to local parts of one file.这些字形的范围在几个文件之间,而不是可以将它们的范围限定到一个文件的本地部分。 When minifying javascript, global scope variables do not get replaced by default because they may be needed in another script, but with CSS, you don't know what classes are local and which may be defined in another file.在缩小 javascript 时,默认情况下不会替换全局范围变量,因为它们可能在另一个脚本中需要,但是使用 CSS,您不知道哪些类是本地的,哪些可能在另一个文件中定义。 To make matters worse, you also need to sync the class replacement to javascript as well, as it's very common to find DOM elements in code via classes.更糟糕的是,您还需要将类替换同步到 javascript,因为通过类在代码中查找 DOM 元素是很常见的。 It would be impossible to sync this, as classes may be constructed dynamically in javascript, and even without that issue, it would be a huge ordeal.同步这个是不可能的,因为类可能是在 javascript 中动态构造的,即使没有这个问题,这也将是一个巨大的考验。 You can only sync the glyph replacement in javascript if you change your coding style to make sure you explicitly identify where css class strings are being used: https://code.google.com/p/closure-stylesheets/#Renaming如果您更改编码样式以确保明确标识使用 css 类字符串的位置,则只能在 javascript 中同步字形替换: https : //code.google.com/p/closure-stylesheets/#Renaming

Luckily, glyph replacement is the thing that minification does that gzipping also does very very well, so the size savings from that particular minification strategy is much much less than the other strategies which remove glyphs entirely.幸运的是,字形替换是缩小所做的事情,gzipping 也做得很好,因此该特定缩小策略的大小节省远小于其他完全删除字形的策略。

There is also gulp-selectors .还有gulp-selectors

Install it:安装它:

npm install gulp gulp-selectors

Now a quick-and-dirty node script:现在是一个快速而肮脏的node脚本:

var gulp = require('gulp');
var gs = require('gulp-selectors');
gulp
  .src(['*.html', '*.css'])
  .pipe(gs.run({}, '{ids: "*"}'))
  .pipe(gulp.dest('.'))'

The second argument to gs.run() is in order for it to leave IDs as-is, see also its website: https://www.npmjs.com/package/gulp-selectors gs.run()的第二个参数是为了让 ID 保持原样,另请参见其网站: https : gs.run()

There is plugin https://github.com/vreshch/optimize-css-classnames-plugin Work as Webpack Loader.有插件https://github.com/vreshch/optimize-css-classnames-plugin作为 Webpack Loader 工作。 That might work in most of cases这在大多数情况下可能有效

For css there is YUI compressor .对于 css,有YUI 压缩器 It will remove unnecessary white-space, and convert your properties to shorthand if you don't already use it.如果您还没有使用它,它将删除不必要的空格,并将您的属性转换为速记。 As for html I don't know any but remember that trimming white space in html is not always safe.至于 html,我一无所知,但请记住,在 html 中修剪空白并不总是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM