Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active May 6, 2024 05:19
Show Gist options
  • Save balupton/3898915 to your computer and use it in GitHub Desktop.
Save balupton/3898915 to your computer and use it in GitHub Desktop.
DocPad: Minify your assets with Grunt

DocPad: Minify your assets with Grunt

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

Installation

  1. Install Dependencies

    npm install grunt safeps --save
    npm install grunt-cli -g 
  2. Create your website's grunt file to specify what we should minify. Guide here.

  3. Add the following to your docpad configuration file

    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, 'min']
    		
    		# Execute
    		safeps.spawn(command, {cwd:rootPath,output:true}, next)
    
    		# Chain
    		@

Working Example

Check out Luke Arrys's HTML5 Boilerplate + Grunt Skeleton

@ezmiller
Copy link

I was only able to get this working with the grunt-contrib-uglify package, like so:

First by installing the package:

npm install grunt-contrib-uglify  # run in project root path of course

Then by adding the following to my Gruntfile.js:

grunt.initConfig({
    uglify: {
        my_target: {
            files: {
                'out/scripts/all.js': ['out/scripts/all.js']
            }
        }
    }
});
grunt.loadNpmTasks('grunt-contrib-uglify');

Then setting up the following in docpad.coffee:

                       events:
            # Write after
            # Used to minir our assets with grunt
            writeAfter: (opts, next) ->
                # Prepare
                safeps = require('safeps')
                pathUtil = require('path')
                docpad = @docpad

                # Perform the uglify js min task
                uglify = ['grunt', 'uglify']

                # Execute
                safeps.spawn(uglify, {safe:false,output:true},next)

                # Chain
                @

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment