Soviut
Soviut

Reputation: 91525

How to config.get() a project variable into a task's initConfig in Grunt?

In Grunt, I have a custom multitask that extracts paths from an HTML file and uses grunt.config.set() to add those found paths to an array on the config called "pathsfound".

I want to use grunt.config.get() to access those paths so that I can use them to concat, uglify, etc.

Gruntfile.coffee

    pathfinder:
        dist:
            files: [
                expand: true
                cwd:  '<%= yeoman.app %>'
                src: '*.html'
            ]
    concat:
        dist:
            src: grunt.config.get('pathsfound')
            dest: 'stuff.js'

And my registered task looks like:

grunt.registerTask 'dist', ['pathfinder:dist', 'concat:dist']

However, the concat task is giving me an TypeError: Cannot call method 'indexOf' of undefined error, suggesting that grunt.config.get() can't find the pathsfound variable in initConfig.

Is there any way to lazy load config variables during the initConfig phase?

Upvotes: 4

Views: 2549

Answers (1)

Renato Zannon
Renato Zannon

Reputation: 29941

The way you've written it, the grunt.config.get call is executed when the config object is being built, so your variable isn't there yet.

A way to make grunt load them later is to use a template instead:

concat:
  dist:
    src: "<%= pathsfound.bower %>"

The templates are lazily expanded, just before the task is run. If the task that sets the config runs before the task that needs it, it should work.

Upvotes: 5

Related Questions