Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active October 11, 2015 18:08
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
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

@lukekarrys
Copy link

I get the following error when using this:

error: Something went wrong with the action
error: An error occured: 
TypeError: Cannot read property 'config' of undefined

Changing the => to -> fixed it for me.

@balupton
Copy link
Author

Fixed :) Thanks!

@DjebbZ
Copy link

DjebbZ commented Feb 5, 2013

I can haz plugin ? Seriously, wrapping this code into a plugin, with a little addition like optional use this only for production but in dev environment would be very useful. DocPad lacks a build process and this plugin would fill the gap very well.

@hfjallemark
Copy link

This doesn't seem to work on Windows - no files are generated. Any clue?

@hfjallemark
Copy link

Here is the console output:

$ docpad run
info: Welcome to DocPad v6.21.10
info: Plugins: coffeescript, eco, livereload, marked, partials, stylus, text
info: Environment: development
info: DocPad listening to http://localhost:9778/ on directory c:\temp\html5-boilerplate.docpad\out
info: LiveReload listening to new socket on channel /docpad-livereload with log level 1
info: Generating...
CreateProcessW: %1 is not a valid Win32 application.
info: Generated all 18 files in 0.22 seconds
info: Watching setup starting...
info: Watching setup
info: The action completed successfully

Notice CreateProcessW: %1 is not a valid Win32 application - not sure what that means.

@cattermo
Copy link

I have the same problem as hfjallemark. Any help would be great!
Win 64 machine.

@mvhenderson
Copy link

For windows change the commad to use grunt.cmd instead of grunt

@SteveMcArthur
Copy link

In windows you need to make sure the Grunt command line is installed with

npm install -g grunt-cli

I then had to change the command value in the docpad.coffee file to:

command = ['grunt.cmd', 'default']

@seniorpreacher
Copy link

I have some trouble with the spawn function. I tried this example and the extended version from this docpad skeleton, but I get the same error every time:

error: An error occured:
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)
    → [2013-06-01 14:03:27.698] [C:\Users\USERNAME\AppData\Roaming\npm\node_modules\docpad\out\lib\docpad.js] [DocPad.log]

I have the grunt-cli installed.
I use the following versions:
grunt - 0.4.1
bal-util - 2.1.0

@psteinweber
Copy link

Could anyone go into details about (2) please? I don't get what I have to do with the grunt file. I suppose it's the file which is in /node_modules/grunt/Gruntfile.js ? There's lots of stuff in there, so what do I have to add/adjust to minify my HTML, CSS and JS?
Sorry for the newbie question... Thanks a lot!

@balupton
Copy link
Author

Try now

@0xgeert
Copy link

0xgeert commented Aug 9, 2013

The above docs as is don't work when doing a new install of grunt:

npm install grunt-cli -g results in grunt being available globally on your system. This means it isn't available at the constructed path `node_modules/.bin/grunt'

Easiest imo is to do the following (especially notice: save:false):

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

        # Perform the grunt `min` task
        # https://github.com/gruntjs/grunt/blob/0.3-stable/docs/task_min.md
        command = [grunt, 'min']

        # Execute
        safeps.spawn(command, {save:false,output:true}, next)

        # Chain
        @

@Hypercubed
Copy link

Could this be a plugin (docpad-plugin-grunt) with the default command = [gruntPath, 'default'] ?

@zenorocha
Copy link

@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