Recommended HTML/CSS/JS Minification for Go's {html,text}/template?

agolangf · · 549 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;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>&#39;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&#39;t have something like <code>&lt;tag {{printTagEnd}}text</code>, if <code>{{printTagEnd}}</code> prints <code>&gt;</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&#39;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&#39;m serving plays well with permissive cache controls and doesn&#39;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&#39;s some good support to prevent it from choking, too. I use Gulp as my task runner, but here&#39;s the relevant code. I hope this helps!</p> <pre><code>var gulp = require(&#34;gulp&#34;) var revCollector = require(&#34;gulp-rev-collector&#34;) var minifyHTML = require(&#34;gulp-htmlmin&#34;) // Replaces static asset links with the revisioned copy for cache busting // purposes. We also minify the HTML templates, removing all comments. gulp.task(&#34;rev:templates&#34;, () =&gt; { return gulp.src([&#34;dist/rev-manifest.json&#34;, &#34;dist/templates/**/*.html&#34;]) .pipe(revCollector({ replaceReved: true })) .pipe(minifyHTML({ caseSensitive: true, collapseWhitespace: true, ignoreCustomFragments: [ /{{if.*}}/, ], minifyCSS: true, minifyJS: true, removeComments: true })) .pipe(gulp.dest(&#34;dist/templates&#34;)) }) </code></pre> <p>You can modify the <code>ignoreCustomFragments</code> portion to add a whitelist of sorts. That&#39;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&#39;t you minify all of the files first, before loading them in as templates? Any type of text replacement you are doing through go&#39;s text/html template libraries should work just the same whether it&#39;s been minified or not.</p></pre>matttproud: <pre><p>The problem is not Go&#39;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&#39;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&#39;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

549 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传