javascriptgruntjsdocpadlivereload

How to have a the docpad grunt skeleton min vendor js files with live reload


I'm using skeleton #2, HTML5BP + Grunt. The first time I docpad run the following happens:

info: LiveReload listening to new socket on channel /docpad-livereload
Performing writeFiles (postparing) at 0/1 0% [...] Running "min:js" (min) task
File "../out/scripts/all.min.js" created.
Uncompressed size: 298495 bytes.
Compressed size: 38257 bytes gzipped (106756 bytes minified).

Which is as is supposed to be. However using the livereload plugin if I change a template or document file, I get:

--Running "min:js" (min) task
File "../out/scripts/all.min.js" created.
Uncompressed size: 0 bytes.

Editing my script.js throws it into the mix, but none of my vendor js files are rendered with it, which is just as useless. grunt-cssmin renders all scss/css files grunt-config.json regardless, which works fine. Moving my js from /files/vendor to /documents/scripts didn't change this behavior.

I've done a little poking around, but I'm new to grunt and nothing jumped out at me.

It'd be nice if I could either:

a) have all JS files in grunt-config.json minned and zipped each time

b) not have grunt min js files in development environment

As is if I want to make any changes to something regarding javascript, I need to ctrl-c docpad and then run it again, which is meh.


Solution

  • Not ideal, but effective enough:

    events:
    
        # Write After
        # Used to minify our assets with grunt
        writeAfter: (opts,next) ->
            # Prepare
            docpad = @docpad
            rootPath = docpad.config.rootPath
            balUtil = require 'bal-util'
            _ = require 'underscore'
    
            # Make sure to register a grunt `default` task
            command = ["#{rootPath}/node_modules/.bin/grunt", 'default']
    
            # Execute
            balUtil.spawn command, {cwd:rootPath,output:true}, ->
                src = []
                gruntConfig = require './grunt-config.json'
                _.each gruntConfig, (value, key) ->
                    src = src.concat _.flatten _.pluck value, 'src'
                #_.each src, (value) ->
                #    balUtil.spawn ['rm', value], {cwd:rootPath, output:false}, ->
                #balUtil.spawn ['find', '.', '-type', 'd', '-empty', '-exec', 'rmdir', '{}', '\;'], {cwd:rootPath+'/out', output:false}, ->
                next()
    
            # Chain
            @
    

    The three lines around "balUtil" which perform find/rm commands were commented out.

    Not ideal since the "uncompressed" files are left around -- but that's not really the end of the world. Live-reloading to empty pages was a tad more frustrating, ultimately.

    There could be a way to further enhance this to detect a live reload (development) vs generating a build for production, but I haven't grokked that yet.