问题
Is it possible to read a JSON file of child-themes and variables relevant to each one and have a dynamically generated build process via grunt.js?
Specifically, I have a standard grunt.initConfig()
, for each JSON object in an external themes.json
file, I want to repeat the entire build process of the main grunt.initConfig()
but I need to pass variables (not always the same) via meta associated with each child theme to the build process.
I've spent alot of time working through it and I'm beginning to think grunt.js
just can't accomodate the build process I'm looking for. I thought that by creating a mirrored hierarchy in a custom meta value like this
module.exports = function(grunt) {
grunt.initConfig({
pkg: '<json:package.json>',
_themes: '<json:themes.json>',
recess : 'foo',
concat : 'bar',
mincss : 'baz
}
and then inside of themes.json
mirroring that structure again but calling out to the root config file like this
{
"themFoo" : {
"_meta_val_1" : "x",
"_meta_val_2" : "y",
"_meta_val_3" : "z",
"recess" : "<%= recess %>",
"concat" : "<%= concat %>",
"mincss" : "<%= min %>"
}
That I could get the default config and just fill in the blanks with template variables, but it doesn't seem to work that way.
回答1:
Ben Alman, author of Grunt.js was kind enough to explain it here for anyone looking to do what I've outlined above:
https://github.com/gruntjs/grunt/issues/568#issuecomment-11335941
Essentially, I was going about this incorrectly, not realizing that the JSON structure was non-negotiable at the top-level. You can have optional and/or multiple files, outputs, etc. nested beneath the top-level functions, but those top-level JSON data items are reserved for native functions and extensions.
Here's a gist of how I was able to accomplish what I outlined above: https://gist.github.com/4294776
来源:https://stackoverflow.com/questions/13832800/dynamic-build-processes-with-grunt-js