Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify JavaScript during GitHub Pages build?

I have a static website through GitHub Pages, built on Jekyll-Bootstrap. My little website includes a lot of JavaScript, and for maintainability I would like all of the JavaScript to remain human-readable in the GitHub repo.

But for the end-user of my website, I would prefer to minify the JavaScript.

Is there some way to build a hook into the GitHub Pages build process to minify/uglify JavaScript, so that the end user can download smaller files?

like image 626
john_science Avatar asked Nov 18 '16 16:11

john_science


People also ask

Should you minify JavaScript?

Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance. The obfuscation shouldn't adversely affect performance.

Why should you minify your JavaScript scripts before publishing them?

These characters include whitespaces, line breaks, comments, and block delimiters which are useful for us humans but unnecessary for machines. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.

How do I minify a JavaScript file?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

How does Minify JavaScript work?

How Minification Works. Minification works by analyzing and rewriting the text-based parts of a website to reduce its overall file size. Minification extends to scripts, style sheets, and other components that the web browser uses to render the site. Minification is performed on the web server before a response is sent ...


3 Answers

The GitHub pages build service cannot have any other code running on it other than Jekyll in safe mode and the small number of included plugins. This is done for security.

Your best option is to use an alternative service to build your site and push the result back to GitHub. The source for the site would reside in the master branch and the compiled source in gh-pages.

A suitable service for doing so would be one of many CI services, such as Travis CI. These are typically used to run software test suites on every push to a repo, but can be used to build your website and push the result back to you.


The Jekyll docs have 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.

To authenticate your push the script will need access to your github personal access token. You can't just put this straight in the deploy script as it's a secret. See the Travis docs on encrypting environment variables.

like image 99
wjdp Avatar answered Oct 14 '22 02:10

wjdp


If you are using Github to generate the site and display it, there is no option to do this because Github is strict about what it will process - for security.

A workaround is to do your compiling and processing locally, then push the resulting output to to gh-pages - which is happy to simply host static pages.

You can still use github to host the project. You just do not use Github to compile it.

Your dev process might be:

  1. Check you're master and local match.
  2. Do your work in dev mode.
  3. Build in production.
  4. Use grunt or other program to minify/uglify/etc the _site production files - outputting to a separate dist (distribution) folder.
  5. Push the contents of the dist folder to your gh-pages.
  6. Commit changes to the project files back to the master.

I am probably not making much sense, but perhaps this discussion might help more: https://gist.github.com/cobyism/4730490

Have fun!

like image 36
TBB Avatar answered Oct 14 '22 02:10

TBB


You can try to use my own minifier https://github.com/Mendeo/jekyll-minifier. It is written purely on liquid, so you do not need any additional gems install and it is fully compatible with GitHub Pages.

like image 1
Александр Меняйло Avatar answered Oct 14 '22 01:10

Александр Меняйло