firstandthird / load-grunt-config

Grunt plugin that lets you break up your Gruntfile config by task
firstandthird.github.io/load-grunt-config/
MIT License
374 stars 64 forks source link

Data not working. #77

Closed lukasoppermann closed 10 years ago

lukasoppermann commented 10 years ago

Hey,

I probably do it wrong, but shouldn't this work? I always get an error, I tried replacing config in in data:{} with just test: "test" but the error remains.

Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: config is not defined

gruntfile.js

// module exports
module.exports = function(grunt) {

    var path = require('path');
    // get config from package file
    var config = {
        pkg: grunt.file.readJSON('package.json'),
        env: process.env
    };

    config = grunt.util._.extend(config,
        require('load-grunt-config')(grunt, {
            configPath: path.join(process.cwd(), config.pkg.dirs["task-options"]),
            loadGruntTasks: false,
            init: false,
            data: {
                config: config
            },
        })
    );

tasks/options/imagemin.js

module.exports = {
    dynamic: {
        options: {
            optimizationLevel: 3
        },
        files: [{
            expand: true,
            cwd: config.pkg.dirs.devmedia, // Src matches are relative to this path
            src: ['*.{png,jpg,gif}'], // Actual patterns to match
            dest: config.pkg.dirs.media, // Destination path prefix
        }]
    }
}
SolomoN-ua commented 10 years ago

Hi, you should do it like this (for example):

cwd: '<%= config.pkg.dirs.devmedia %>'
lukasoppermann commented 10 years ago

Hey @SolomoN-ua, this doesn't change anything. The problem is that somehow the config variable is not defined. I am pretty sure it would work like config.pkg.dirs.devmedia anyway, because your version basically just puts a variable in a string and then makes this string interpret the variable (if I am not mistaking).

I did try it anyway, but receive the same error message.

SolomoN-ua commented 10 years ago

You can use config as variable only in gruntfile.js because it is defined there, but in other files you should use it as in my example (because config is not defined in other files), or you should use it like this:

module.exports = function(grunt, options) {
  return {
    cwd: options.config.pkg.dirs.devmedia
  }
}

Take a look into documentation https://github.com/firstandthird/load-grunt-config#grunt-tasks-files So first make sure you are using it properly in all tasks files

lukasoppermann commented 10 years ago

Hey, thanks, the export as a function fixed it all. :+1: