dciccale / grunt-processhtml

Process html files at build time to modify them depending on the release environment
MIT License
407 stars 30 forks source link

Change only context not files #87

Closed pbassut closed 8 years ago

pbassut commented 8 years ago

So, I see myself replicating the same files to different targets. Usually, grunt plugins let you specify either a global parameter but also a task-specific one.

Question: is there a way to do that with grunt-processhtml?

To illustrate, instead of this:

processhtml: {
    options: {
        process: true,
    },
    dev: {
        options: {
            files: {
                'myfile.html': ['src/myfile.html'],
                'myfile2.html': ['src/myfile2.html'],
                'myfile3.html': ['src/myfile3.html'],
            },
            data: {

                param1: 'value1'
            }
        }
    },
    dist: {
        options: {
            files: {
                'myfile.html': ['src/myfile.html'],
                'myfile2.html': ['src/myfile2.html'],
                'myfile3.html': ['src/myfile3.html'],
            },
            data: {
                param1: 'value1'
            }
        }
    }
},

I wish I could do this:

processhtml: {
    files: {
        'myfile.html': ['src/myfile.html'],
        'myfile2.html': ['src/myfile2.html'],
        'myfile3.html': ['src/myfile3.html'],
    },
    options: {
        process: true,
    },
    dev: {
        options: {
            data: {
                param1: 'value1'
            }
        }
    },
    dist: {
        options: {
            data: {
                param1: 'value1'
            }
        }
    }
}
pbassut commented 8 years ago

Oh, and also, the same goes for the data parameter. Would be great If I didn't need to replicate code. If param1 is defined on global data, the target data would 'inherit' those.

dciccale commented 8 years ago

grunt itself doesn't allow you to do that thing where you want to share the files.

inside the task, each object block, except for the options, is a target.

regarding your other suggestion, sounds like a good a idea to extend the local data with the global data, however grunt also overrides this behaviour by overriding defaults with specific local options as you can see here https://github.com/gruntjs/grunt/blob/master/lib/grunt/task.js#L63

the only way i see to achieve this is to add a new option like localData that could be extended by the global data. however nothing prevents you to also use localData option in the global and thus run into the same issue.

Also, remember this is just JS, so you could do that already by doing something like this:

...
var files = {
  'myfile.html': ['src/myfile.html'],
  'myfile2.html': ['src/myfile2.html'],
  'myfile3.html': ['src/myfile3.html']
};
var data = {
  param1: 'common thing'
};
var xtend = grunt.util._.extend;
...
processhtml: {
  options: {
    process: true,
  },
  dev: {
    files: files,
    options: {
      data: xtend({param2: 'only for dev'}, data)
    }
  },
  dist: {
    files: files,
    options: {
      data: {
        param1: xtend({param3: 'only for dist'}, data)
      }
    }
  }
}
pbassut commented 8 years ago

I know I could use plain js to accomplish this. But defining variable up top the gruntfile makes it look ugly IMO.

Thanks anyway.