<p>Hey all,</p>
<p>Let me first approach this by saying: I am looking for any advice I can get. I care less about the mechanism than achieving a good end result.</p>
<p>Can you recommend a minification tool for HTML, CSS, or JS that plays well Go's <a href="https://golang.org/pkg/text/template/">package template/text</a> and <a href="https://golang.org/pkg/html/template/">package template/html</a>'s directives? Alternatively, there appear to be a few Go-native minification libraries. What is your opinion of them?</p>
<p>Thank you in advance! Just a backend-loving Gopher who avoids the (browser) frontend as much as possible. I had given a few a try, and they seemed to have broken down on the directives. :-(</p>
<hr/>**评论:**<br/><br/>dmikalova: <pre><p><a href="https://github.com/tdewolff/minify" rel="nofollow">https://github.com/tdewolff/minify</a></p></pre>schumacherfm: <pre><p>+1 that code is very well written</p></pre>tdewolff: <pre><p>Thanks! Library author here.</p>
<p>You can minify template files as long as they are valid HTML, which I think most of the go templates are. What I mean is that you shouldn't have something like <code><tag {{printTagEnd}}text</code>, if <code>{{printTagEnd}}</code> prints <code>></code>. Any HTML parser will see text as being an attribute. See <a href="https://github.com/tdewolff/minify/issues/35" rel="nofollow">https://github.com/tdewolff/minify/issues/35</a>, I'm thinking about implementing full template support in the future.</p>
<p>However, my library is so fast (millisecond range), why not minify the resulting HTML instead of the template?</p></pre>matttproud: <pre><p>What you suggest is even better. I will study the extension of my handlers to enable this. Luckily the content I'm serving plays well with permissive cache controls and doesn't need invalidation.</p>
<p>I had hoped somebody would suggest this.</p></pre>mrmylanman: <pre><p>I have successfully used html-minifier for templates without much issue. There's some good support to prevent it from choking, too. I use Gulp as my task runner, but here's the relevant code. I hope this helps!</p>
<pre><code>var gulp = require("gulp")
var revCollector = require("gulp-rev-collector")
var minifyHTML = require("gulp-htmlmin")
// Replaces static asset links with the revisioned copy for cache busting
// purposes. We also minify the HTML templates, removing all comments.
gulp.task("rev:templates", () => {
return gulp.src(["dist/rev-manifest.json", "dist/templates/**/*.html"])
.pipe(revCollector({
replaceReved: true
}))
.pipe(minifyHTML({
caseSensitive: true,
collapseWhitespace: true,
ignoreCustomFragments: [
/{{if.*}}/,
],
minifyCSS: true,
minifyJS: true,
removeComments: true
}))
.pipe(gulp.dest("dist/templates"))
})
</code></pre>
<p>You can modify the <code>ignoreCustomFragments</code> portion to add a whitelist of sorts. That's how I got it to not choke on conditionals in HTML attributes (along with <code>caseSensitive</code>, which was required to keep it from lower-casing attributes, if memory serves).</p></pre>matttproud: <pre><p>Thank you. This kind of information was exactly what I was looking for. :)</p></pre>SilentWeaponQuietWar: <pre><p>Maybe a naive question, but why don't you minify all of the files first, before loading them in as templates? Any type of text replacement you are doing through go's text/html template libraries should work just the same whether it's been minified or not.</p></pre>matttproud: <pre><p>The problem is not Go's template packages themselves but rather that a number of minification tools (e.g., <a href="https://github.com/kangax/html-minifier" rel="nofollow">html-minifier</a>) choke when they encounter Go's template directives. This is to say, they do not interoperate well with document inputs that contain Go template directives and reject them as illegal input.</p></pre>Bromlife: <pre><p>Why are you minifying HTML etc anyway? Just gzip it.</p>
<p><a href="https://github.com/NYTimes/gziphandler" rel="nofollow">https://github.com/NYTimes/gziphandler</a></p></pre>matttproud: <pre><p>Going the last mile with Google Page Speed analysis tools. The handlers all set appropriate cache control rules as well as optionally GZIP-encoding the responses. I was frankly shocked that the speed analysis tools put so much closer weight on minification compared to the others. :-/ Morale of the story: developing for the Web is still an alchemy-filled shit sandwich.</p></pre>FIuffyRabbit: <pre><p>HTML isn't typically minified. </p></pre>bengui1d: <pre><p>I just use command line tools to do it in advance of deploying, personally. </p></pre>tcrypt: <pre><p>OP is asking how to do that without the tools choking on Go templates.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传