public
Last active

DocPad: Minify your assets with Grunt

  • Download Gist
README.md
Markdown

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

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.

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.

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

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.

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

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

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']

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

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!

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
        @

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.