More Grunt & Jekyll

Russ McKendrick · 4 min read ·
At the end of my last post about Grunt I mentioned that I will be adding code to concatenate the javascript, compress the images and also tidy the code up abit. I have now done this âŚ.
Javascript
I moved all of the javascript to a folder called _js and then set up the following âŚ.
js: { src: [ â_js/jquery.min.jsâ, â_js/jquery.plugin.min.jsâ, âjs/bootstrap.min.jsâ, â_js/jquery.flexslider-min.jsâ, â_js/smooth-scroll.min.jsâ,â _js/skrollr.min.jsâ, â_js/twitterFetcher_v10_min.jsâ, â_js/spectragram.min.jsâ, â_js/scrollReveal.min.jsâ, â_js/isotope.min.jsâ, â_js/lightbox.min.jsâ, â_js/jquery.countdown.min.jsâ, â_js/scripts.jsâ ], dest: âjs/main.jsâ }As you can see I am having to merge the javascript in a certain order, I tried using src: [â_js/*â] but this broke a lot of the site.
Now I have all of the javascript in a single file which works I can compress it using Uglify âŚ.
uglify: { options: { banner: â/*! <%= pkg.name %> <%= grunt.template.today(âdd-mm-yyyyâ) %> */\nâ }, my_target: { files: { âjs/main.min.jsâ: [âjs/main.jsâ] } }},I then created a task to put it all together âŚ.
grunt.registerTask(âjsâ, [âconcat:jsâ, âuglifyâ]);as well as adding the _js and tasks to the watch section of my Gruntfile.js.
Images
I use the TinyPNG Photoshop plug-in to export the headers and other images so most of the graphics on the site are already quite compressed, however to ensure that everything is as optimised as possible I added the following âŚ.
imgcompress: { dist: { options: { optimizationLevel: 7, progressive: true }, files: [{ expand: true, cwd: âimg/â, src: â{,*/}*.{png,jpg,jpeg}â, dest: âimg/â }] }},svgmin: { dist: { files: [{ expand: true, cwd: âimg/â, src: â{,*/}*.svgâ, dest: âimg/â }]}},as the site has a lot of image files Newer was used to files are only processed if they have been changed or havenât been processed. I added the following task âŚ.
grunt.registerTask(âimagesâ, [ânewer:imgcompressâ, ânewer:svgminâ]);However, for the moment I have left it out of the watch list as it can take a little while to execute.
Full files
This now means my Gruntfile.js looks like âŚ.
module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON(âpackage.jsonâ),
less: { development: { options: { compress: true, yuicompress: true, optimization: 2 }, files: { â_css/main.cssâ: â_less/main.lessâ } } }, concat: { css: { src: [ â_css/*â ], dest: âcss/main.cssâ }, js: { src: [ â_js/jquery.min.jsâ, â_js/jquery.plugin.min.jsâ, âjs/bootstrap.min.jsâ, â_js/jquery.flexslider-min.jsâ, â_js/smooth-scroll.min.jsâ,â _js/skrollr.min.jsâ, â_js/twitterFetcher_v10_min.jsâ, â_js/spectragram.min.jsâ, â_js/scrollReveal.min.jsâ, â_js/isotope.min.jsâ, â_js/lightbox.min.jsâ, â_js/jquery.countdown.min.jsâ, â_js/scripts.jsâ ], dest: âjs/main.jsâ } }, cssmin: { css: { src: âcss/main.cssâ, dest: âcss/main.min.cssâ } }, uglify: { options: { banner: â/*! <%= pkg.name %> <%= grunt.template.today(âdd-mm-yyyyâ) %> */\nâ }, my_target: { files: { âjs/main.min.jsâ: [âjs/main.jsâ] } } }, jekyll: { options: { src: â.â }, dist: { options: { dest: â./_siteâ, config: â_config-dev.ymlâ } } }, imgcompress: { dist: { options: { optimizationLevel: 7, progressive: true }, files: [{ expand: true, cwd: âimg/â, src: â{,*/}*.{png,jpg,jpeg}â, dest: âimg/â }] }},svgmin: { dist: { files: [{ expand: true, cwd: âimg/â, src: â{,*/}*.svgâ, dest: âimg/â }]}},watch: { options: { livereload: true, }, css: { files: [â_css/*â, â_less/*â], tasks: [âlessâ, âconcat:cssâ, âcssmin:cssâ, âjekyllâ] }, js: { files: [â_js/*â], tasks: [âconcat:jsâ, âuglifyâ, âjekyllâ] }, html: { files: [â*.htmlâ, â_includes/*.htmlâ, â_layouts/*.htmlâ, â_posts/*â, â*.ymlâ, âimg/*â], tasks: [âjekyllâ], options: { spawn: false, } }},connect: { server: { options: { port: 8000, base: â./_siteâ } }},
});require(âload-grunt-tasksâ)(grunt);grunt.registerTask(âdefaultâ, [âconcat:jsâ, âuglifyâ, âlessâ, âconcat:cssâ, âcssmin:cssâ, âjekyllâ, âconnectâ, âwatchâ]);grunt.registerTask(âcssâ, [âlessâ, âconcat:cssâ, âcssmin:cssâ]);grunt.registerTask(âjsâ, [âconcat:jsâ, âuglifyâ]);grunt.registerTask(âserverâ, [âconnectâ, âwatchâ]);grunt.registerTask(âimagesâ, [ânewer:imgcompressâ, ânewer:svgminâ]);};And the package.json file âŚ.
{ ânameâ: âmediaglassesâ, âversionâ: â0.0.1â, âdescriptionâ: âThe Media Glasses Jekyll siteâ, âauthorâ: âRuss McKendrick <russ@mckendrick.io>â, âhomepageâ: âhttp://media-glass.es/", âlicenseâ: ââ, âdevDependenciesâ: { âgruntâ: â~0.4.5â, âgrunt-contrib-concatâ: â*â, âgrunt-contrib-cssminâ : â*â, âgrunt-contrib-watchâ : â*â, âgrunt-contrib-lessâ: â*â, âgrunt-jekyllâ: â*â, âgrunt-contrib-connectâ: â*â, âgrunt-contrib-uglifyâ: â*â, âgrunt-contrib-imageminâ: â*â, âgrunt-svgminâ: â*â, âgrunt-imgcompressâ: â*â, âgrunt-newerâ: â*â, âload-grunt-tasksâ: â*â }}Related terms: AgentAgent SkillsAI Coding AssistantApplication Programming InterfaceClaude CodeCodex
More from the archive
More site updates
Russ Mckendrick improves blog speed, adds Gruntfile features for testing and deployment to Amazon S3 with Cloudflare cache flushing.
2 min read

Git + Rebase
Streamline your Git workflow with Russ Mckendrick's tips! Learn how to fork, add remotes, and rebase for maximum efficiency.
2 min read

Comments