Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compress and minify assets using jekyll plugins

Tags:

minify

jekyll

I have a simple test jekyll app like so:

index.html is:

<script type="text/javascript" src="/assets/myfile.js"><script>
<p>hi</p>

and the myfile.js files is as follows:

var  x  = "this is a string";
alert(x +  "hi");

I just want to add a plugin so that it can minify the js file before it is written to the _site/assets/myfile.js file

How can i achienve that?

like image 450
umar Avatar asked Apr 18 '12 11:04

umar


4 Answers

Here's a summary of what I've found while solving this in 2014:

Started with

  1. https://github.com/donaldducky/jekyll-cssminify

    Development was going well until it was beat by this project:

  2. http://www.matthodan.com/2012/11/22/jekyll-asset-pipeline.html

    ...which so far has been my favorite to implement, except it doesn't work now and it looks like development has been halted, so I was forced to turn to

  3. https://github.com/envygeeks/jekyll-assets

    ...which again, beats #2 but is just a touch trickier to use. Anyway, that's the only one that works, so that's what I'm using now.

like image 71
Ben Avatar answered Nov 15 '22 03:11

Ben


There are plugins for HTML and CSS minification with Jekyll, but a search doesn't turn up anything obvious for JS.

However, the source for CSS one above is less than 100 lines and actually uses Juicer to do the minification, which can also do JS, so that plugin could easily be used as a template to implement your own JS minifier (it looks like even just s/css/js/g would be close to working).

like image 5
huon Avatar answered Nov 15 '22 03:11

huon


I recommend the Jekyll Asset Pipeline gem for this. It supports any language (e.g. Scss, Less, CoffeeScript, Erb, etc.) and has a bunch of features (e.g. asset tagging, compression, gzipping, etc.) that set it apart. It also seems to be the fastest growing Jekyll-related gem these days, which I take to mean that it is gaining traction in the community.

like image 4
Matt Hodan Avatar answered Nov 15 '22 03:11

Matt Hodan


For JS and CSS you can use the Jekyll Asset Bundler It uses the yui-compressor or closure-compiler gems.

like image 3
crisscross Avatar answered Nov 15 '22 03:11

crisscross