Skip to content

Instantly share code, notes, and snippets.

@greduan
Forked from balupton/README.md
Last active December 20, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greduan/6096964 to your computer and use it in GitHub Desktop.
Save greduan/6096964 to your computer and use it in GitHub Desktop.
Using Grunt with DocPad

DocPad: Integrating Grunt

The following will minify your assets with grunt each time a generation write completes.

Installation

  1. Install Dependencies

    npm install --save grunt safeps
    npm install --save grunt-cli
  2. Create your website's Gruntfile.js file to specify what we should do with Grunt. Guide here.

# Define the DocPad Configuration
docpadConfig = {
events:
# Write After
# Used to minify our assets with grunt
writeAfter: (opts,next) ->
# Prepare
safeps = require('safeps')
pathUtil = require('path')
docpad = @docpad
rootPath = docpad.getConfig().rootPath
gruntPath = pathUtil.join(rootPath, 'node_modules', '.bin', 'grunt')
# Perform the grunt `min` task
# https://github.com/gruntjs/grunt/blob/0.3-stable/docs/task_min.md
command = [gruntPath]
# Execute
safeps.spawn(command, {cwd:rootPath,output:true}, next)
# Chain
@
}
# Export the DocPad Configuration
module.exports = docpadConfig
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
// Import package.json file, we get some info from here
pkg: grunt.file.readJSON('package.json'),
// Minify JS into one file
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
},
files: {
src: ['out/scripts/file1.js', 'out/scripts/file2.js'],
dest: 'out/scripts/all.min.js'
}
},
// Minify CSS into one file
cssmin: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */'
},
files: {
src: ['out/styles/file1.css', 'out/styles/file2.css', 'out/styles/file3.css'],
dest: 'out/styles/all.min.css'
}
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.registerTask('default', ['uglify', 'cssmin']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment