Search code examples
gruntjsgrunt-usemin

Grunt config functions omitted


I am trying to define a custom function in the useminPrepare configuration, but no matter what I do, the function is eventually omitted in the build.

I have updated Node(0.10.36) and NPM(2.4.1) to the latest versions.

The config file:

useminPrepare: {
    src: options.dist.dir + '/index.html',
    options: {
        dest: options.dist.dir + '/',
        staging: options.temp.dir,

        flow: {
            steps: {
                js: ['concat', 'uglifyjs']
            },
            post: {
                js: [
                    {
                        name: 'concat',
                        createConfig: function(a,b) {
                            console.log(a,b);
                        }
                    },
                    'uglifyjs'
                ]
            }
        }
    }
}

Grunt build verbose (notice createConfig is gone):

Running "useminPrepare:useminPrepare" (useminPrepare) task
Verifying property useminPrepare.useminPrepare exists in config...OK
Files: dist/index.html
Options: dest="dist/", staging=".tmp", flow={"steps":{"js":["concat","uglifyjs"]},"post":{"js":[{"name":"concat"},"uglifyjs"]}}
Going through dist/index.html to update the config
Looking for build script HTML comment blocks

Anyone have any idea?

It also omits if I specify my own parameter with a function as value (it wont get omitted if I specify a string):

name: 'concat',
test: function() {
    console.log("Hello world");
},
createConfig: function() {
    console.log(a,b);
}

Solution

  • I figured out what I did wrong, I should have specified the options outside my subtask, like so:

    useminPreprare: {
        options: {
            dest: options.dist.dir + '/',
            staging: options.temp.dir,
    
            flow: {
                steps: {
                    js: ['concat', 'uglifyjs'],
                    css: ['concat', 'cssmin']
                },
                post: {
                    js: [
                        {
                            name: 'concat',
                            createConfig: function(a,b) {
                                console.log(a,b);
                            }
                        },
                        'uglifyjs'
                    ]
                }
            }
        },
        build: {
            src: options.dist.dir + '/index.html',
        }
    }
    

    Now I can execute useminPrepare with the task useminPrepare:build