在 GitHub 页面构建期间缩小 JavaScript?

Minify JavaScript during GitHub Pages build?

我通过 GitHub 页面建立了一个静态网站,该网站基于 Jekyll-Bootstrap。我的小网站包含很多 JavaScript,为了可维护性,我希望所有 JavaScript 在 GitHub 存储库中保持人类可读。

但对于我网站的最终用户,我更愿意缩小 JavaScript。

是否有某种方法可以在 GitHub 页面构建过程中构建一个挂钩到 minify/uglify JavaScript,以便最终用户可以下载较小的文件?

除了 safe mode and the small number of included plugins 中的 Jekyll 之外,GitHub 页面构建服务不能有任何其他代码 运行ning。这样做是为了安全。

您最好的选择是使用替代服务来构建您的站点并将结果推送回 GitHub。该站点的源代码位于 master 分支中,编译后的源代码位于 gh-pages 中。

这样做的合适服务是许多 CI 服务之一,例如 Travis CI。这些通常用于 运行 每次推送到存储库时的软件测试套件,但可用于构建您的网站并将结果推送回给您。


Jekyll 文档有 a guide for testing builds on Travis. Pushing the output isn't mentioned. You'll need a script in the after_success derivative in the Travis conf file. An example from a site I maintain

要验证您的推送,脚本需要访问您的 github 个人访问令牌。你不能直接把它放在部署脚本中,因为它是一个秘密。请参阅 encrypting environment variables.

上的 Travis 文档

如果您使用 Github 生成站点并显示它,则没有执行此操作的选项,因为 Github 对其处理的内容很严格 - 为了安全。

一种解决方法是在本地进行编译和处理,然后将生成的输出推送到 gh-pages - 它很乐意简单地托管静态页面。

您仍然可以使用 github 来托管该项目。你只是不使用Github来编译它。

您的开发过程可能是:

  1. 检查你是主控和本地匹配。
  2. 在开发模式下工作。
  3. 在生产中构建。
  4. 使用 grunt 或其他程序 minify/uglify/etc _site 生产文件 - 输出到单独的 dist(分发)文件夹。
  5. 将 dist 文件夹的内容推送到您的 gh-pages。
  6. 将对项目文件的更改提交回主服务器。

我可能没有多大意义,但也许这个讨论可能会有更多帮助:https://gist.github.com/cobyism/4730490

玩得开心!

Netlify is an alternative to GitHub Pages that integrates with GitHub (even private repos) and similarly publishes the output of Jekyll (or other static site generators). There are some limits on the free tier,但大多数个人用户不太可能运行加入其中。

您应该能够添加 Jekyll 压缩器插件之一 listed here to accomplish your goals. Here are plugin installation instructions

如果这对您有用,请添加评论!我很想听听进展如何。

你可以尝试使用我自己的压缩器https://github.com/Mendeo/jekyll-minifier。它纯粹是在 liquid 上编写的,所以你不需要安装任何额外的 gem,它与 GitHub 页面完全兼容。