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 am unable to get this to work so far. I get the following error:

Aborted due to warnings.
error: Something went wrong with the action
error: An error occured:
Error: exited with a non-zero status code
at ChildProcess. (/Users/ethan/Sites/codecuts/node_modules/safeps/out/lib/safeps.js:165:23)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket. (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)

Do I need to install something, or as is suggested here call loadNpmTasks?

@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