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

发布于 2024-12-14 15:29:41 字数 471 浏览 1 评论 0原文

在当前项目中,我被要求在部署之前压缩 HTML 类属性和相应的 CSS 选择器。 例如,生产中的代码是

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

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

部署时,我希望替换 HTML 类和相应的 CSS 选择器:

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

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

有哪些可用工具可以存档此压缩?

In current project, I was asked for compressing the HTML class attribute and corresponding CSS selectors before deployment.
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:

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

池木 2024-12-21 15:29:41

If you really want to rename class names (keeping in mind what Madmartigan said) Google Closure Stylesheets does that. It's an overkill, and YUI Compressor or any other minification + gzipping tool should give you enough performance boost, but it can do it. You'll have to use other Closure tools to make appropriate changes to your .js files and html templates.

痞味浪人 2024-12-21 15:29:41

如果您使用节点处理代码,还有一个名为“rename-css-selectors”的项目:

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

几乎每个构建工具(webpack、parcel、gulp...)都有插件:

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

这个将缩小 HTML、JS 和 CSS 文件(实际上是您想要的任何文件)中的所有 CSS 选择器。最后我节省了 20% 的 CSS 文件大小。

There is also a project called "rename-css-selectors" if you handle the code with node:

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

There are plugins for nearly every build tool (webpack, parcel, gulp, ...):

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). I saved 20ish% of the CSS filesize at the end.

乖不如嘢 2024-12-21 15:29:41

这是令人惊奇的短视行为。

  • 第 1 步:在 Web 服务器中打开 GZip 或 Zlib 压缩
  • 第 2 步:所有文本都会被压缩,通常压缩 70% 或更多
  • 第 3 步:没有第 3 步。
  • 第 4 步:利润

This is amazingly short-sighted.

  • Step 1: Turn on GZip or Zlib compression in web server
  • Step 2: All text gets compressed, often by 70% or more
  • Step 3: There is no step 3.
  • Step 4: PROFIT
赢得她心 2024-12-21 15:29:41

使用 gzipping 进行缩小并没有什么问题,即使在现代浏览器引入源映射之前,缩小也是一种最佳实践,因为即使与 gzipping 结合使用,您仍然可以获得一些显着的节省。我们忍受了生产中较差的可读性,因为性能改进是值得的。现在有了源映射,我们就可以鱼与熊掌兼得。这是一篇很好的文章,演示了在大型网站的 html 页面上将缩小与 gzip 结合使用的效果: http://madskristensen.net/post/effects-of-gzipping-vs-minifying-html-files

根据字形的不同,您获得的节省差异差异很大被缩小的代码的分布,因此结果会根据缩小策略和被缩小的语言,甚至仅仅取决于编码风格而有所不同。在一般情况下,节省的费用仍然很大。

压缩不仅仅处理压缩字形,它还可以重构代码以删除不需要的字符,同时达到相同的效果,这是 gzipping 无法做到的。

现在,回到您的具体问题,在您的情况下,您想要缩小类字形。由于多种原因,这很难做到。这些字形的范围位于多个文件之间,而不是可以将它们的范围限制到一个文件的本地部分。缩小 javascript 时,默认情况下不会替换全局范围变量,因为其他脚本中可能需要它们,但使用 CSS,您不知道哪些类是本地类以及哪些类可能在另一个文件中定义。更糟糕的是,您还需要将类替换同步到 javascript,因为通过类在代码中查找 DOM 元素是很常见的。同步这个是不可能的,因为类可以在 javascript 中动态构造,即使没有这个问题,这也将是一个巨大的考验。如果您更改编码风格以确保明确标识 css 类字符串的使用位置,则只能同步 javascript 中的字形替换:https://code.google.com/p/closure-stylesheets/#Renaming

幸运的是,字形替换是压缩所做的事情,gzipping 也做得非常好,所以尺寸该特定缩小策略所节省的费用比完全删除字形的其他策略要少得多。

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. 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

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.

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. 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. 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. 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

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.

还在原地等你 2024-12-21 15:29:41

还有 gulp-selectors

安装它:

npm install gulp gulp-selectors

现在是一个快速而肮脏的 node 脚本:

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

gs.run() 的第二个参数是为了让它按原样保留 ID,另请参阅其网站: https://www.npmjs.com/package/gulp-selectors

There is also gulp-selectors.

Install it:

npm install gulp gulp-selectors

Now a quick-and-dirty node script:

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

ζ澈沫 2024-12-21 15:29:41

对于 css,有 YUI 压缩器。它将删除不必要的空白,并将您的属性转换为简写(如果您尚未使用它)。至于 html,我一无所知,但请记住,修剪 html 中的空白并不总是安全的。

For css there is YUI compressor. 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.

你在我安 2024-12-21 15:29:41

有插件
https://github.com/vreshch/optimize-css-classnames-plugin
作为 Webpack 加载器工作。这在大多数情况下可能有效

There is plugin
https://github.com/vreshch/optimize-css-classnames-plugin
Work as Webpack Loader. That might work in most of cases

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文