You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gulp-htmlmin无法识别<xmp>标签进行HTML压缩的问题

问题:Gulp-htmlmin无法压缩标签内的内容? &lt;a class=&quot;header-anchor&quot; href=&quot;#问题:gulp-htmlmin无法压缩标签内的内容?&quot; aria-hidden=&quot;true&quot;&gt;#&lt;/a&gt;&lt;/h1&gt; &lt;p&gt;我看你遇到的问题是,用gulp-htmlmin压缩HTML时,&lt;xmp&gt;标签里的内容完全没被处理——里面的class属性还保留着引号,换行也没去掉,但外面的标签都正常压缩了。咱们来一步步解决这个问题:&lt;/p&gt; &lt;h2 id=&quot;为什么会这样?&quot;&gt;为什么会这样? &lt;a class=&quot;header-anchor&quot; href=&quot;#为什么会这样?&quot; aria-hidden=&quot;true&quot;&gt;#&lt;/a&gt;&lt;/h2&gt; &lt;p&gt;首先得说,&lt;xmp&gt;是个&lt;strong&gt;已经被HTML5废弃的标签&lt;/strong&gt;了,它的作用是直接展示原始HTML内容,不需要转义。而gulp-htmlmin依赖的html-minifier库,默认会把&lt;xmp&gt;内部的内容当作「原始文本」,不会对其进行任何压缩处理,这就是为什么里面的内容和你写的一模一样,没被修改。&lt;/p&gt; &lt;h2 id=&quot;解决方案(推荐第一种,符合标准)&quot;&gt;解决方案(推荐第一种,符合标准) &lt;a class=&quot;header-anchor&quot; href=&quot;#解决方案(推荐第一种,符合标准)&quot; aria-hidden=&quot;true&quot;&gt;#&lt;/a&gt;&lt;/h2&gt; &lt;h3 id=&quot;方案1:替换为标准的组合&quot;&gt;方案1:替换&lt;xmp&gt;为标准的&lt;pre&gt;&lt;code&gt;组合 &lt;a class=&quot;header-anchor&quot; href=&quot;#方案1:替换为标准的组合&quot; aria-hidden=&quot;true&quot;&gt;#&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;既然&lt;xmp&gt;已经被废弃,不如换成现在推荐的写法:用&lt;pre&gt;&lt;code&gt;包裹内容,同时把内部的HTML字符转义(比如&lt;code&gt;&amp;lt;&lt;/code&gt;改成&lt;code&gt;&amp;amp;lt;&lt;/code&gt;,&lt;code&gt;&amp;gt;&lt;/code&gt;改成&lt;code&gt;&amp;amp;gt;&lt;/code&gt;)。这样既符合HTML规范,htmlmin也能正常压缩内部内容。&lt;/p&gt; &lt;p&gt;修改你的HTML代码:&lt;/p&gt; &lt;pre class=&quot;hljs&quot;&gt;&lt;code class=&quot;language-html volc-pre-code&quot;&gt;&amp;lt;pre&amp;gt;&amp;lt;code&amp;gt;&amp;amp;lt;u&amp;gt;This is U tag with underlined aqua - Default &amp;amp;lt;/u&amp;gt; &amp;amp;lt;u class=&amp;quot;u-gold&amp;quot;&amp;gt;This is U tag with underlined gold&amp;amp;lt;/u&amp;gt; &amp;amp;lt;u class=&amp;quot;u-orange&amp;quot;&amp;gt;This is U tag with underlined orange&amp;amp;lt;/u&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt; &lt;/code&gt;&lt;/pre&gt; &lt;p&gt;这样处理后,htmlmin会自动压缩里面的属性(比如把&lt;code&gt;class=&amp;quot;u-gold&amp;quot;&lt;/code&gt;变成&lt;code&gt;class=u-gold&lt;/code&gt;),还能去掉多余的换行和空格。&lt;/p&gt; &lt;h3 id=&quot;方案2:强制让htmlmin处理内部内容(不推荐,因为已废弃)&quot;&gt;方案2:强制让htmlmin处理&lt;xmp&gt;内部内容(不推荐,因为&lt;xmp&gt;已废弃) &lt;a class=&quot;header-anchor&quot; href=&quot;#方案2:强制让htmlmin处理内部内容(不推荐,因为已废弃)&quot; aria-hidden=&quot;true&quot;&gt;#&lt;/a&gt;&lt;/h3&gt; &lt;p&gt;如果你一定要保留&lt;xmp&gt;标签,可以在gulp管道里先手动处理&lt;xmp&gt;内部的内容,再交给htmlmin压缩。需要用到&lt;code&gt;gulp-replace&lt;/code&gt;插件:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;先安装gulp-replace:&lt;/li&gt; &lt;/ol&gt; &lt;pre class=&quot;hljs&quot;&gt;&lt;code class=&quot;language-bash volc-pre-code&quot;&gt;npm install gulp-replace --save-dev &lt;/code&gt;&lt;/pre&gt; &lt;ol start=&quot;2&quot;&gt; &lt;li&gt;修改你的gulpfile.js:&lt;/li&gt; &lt;/ol&gt; &lt;pre class=&quot;hljs&quot;&gt;&lt;code class=&quot;language-javascript volc-pre-code&quot;&gt;let htmlmin = require(&#x27;gulp-htmlmin&#x27;); let replace = require(&#x27;gulp-replace&#x27;); // ... gulp.task(&#x27;html&#x27;, gulp.series(&#x27;clean&#x27;, function() { return gulp.src(globs.html) // 先处理&amp;lt;xmp&amp;gt;内部的内容 .pipe(replace(/&amp;lt;xmp&amp;gt;([\s\S]*?)&amp;lt;\/xmp&amp;gt;/g, (match, innerContent) =&amp;gt; { // 压缩内部:去掉多余换行和空格,移除属性引号 let minifiedInner = innerContent .replace(/\n\s+/g, &#x27; &#x27;) // 去掉换行和前面的空格 .replace(/\s+/g, &#x27; &#x27;) // 合并多余空格 .replace(/([&amp;quot;&#x27;])([^&amp;quot;&#x27;]+)\1/g, &#x27;$2&#x27;); // 移除属性的引号 return `&amp;lt;xmp&amp;gt;${minifiedInner}&amp;lt;/xmp&amp;gt;`; })) .pipe(htmlmin({ collapseWhitespace: true, removeComments: true, })) .pipe(gulp.dest(&#x27;./dist&#x27;)); })); &lt;/code&gt;&lt;/pre&gt; &lt;p&gt;这样处理后,&lt;xmp&gt;内部的内容就会被压缩,和外面的标签保持一致的压缩效果。&lt;/p&gt; &lt;p&gt;内容的提问来源于stack exchange,提问作者user11831257&lt;/p&gt;
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:01:24