Centralized configuration system with template processing, deep property access, and data validation. Supports complex nested configurations, runtime data manipulation, and template-based value resolution.
Initialize and manage the main configuration object used by tasks.
/**
* Initialize configuration data (alias for grunt.config.init)
* @param {object} config - Configuration object
*/
grunt.initConfig = function(config) {};
/**
* Initialize configuration data
* @param {object} config - Configuration object
*/
grunt.config.init = function(config) {};
/**
* Deep merge object into existing configuration
* @param {object} obj - Object to merge
*/
grunt.config.merge = function(obj) {};Usage Examples:
// Initialize configuration
grunt.initConfig({
concat: {
options: {
separator: '\n',
banner: '/*! Project v1.0.0 */\n'
},
dev: {
files: {
'dist/app.js': ['src/*.js'],
'dist/vendor.js': ['vendor/*.js']
}
},
prod: {
files: {
'dist/app.min.js': ['src/*.js']
}
}
},
uglify: {
prod: {
files: {
'dist/app.min.js': ['dist/app.js']
}
}
},
meta: {
version: '1.0.0',
banner: '/*! Project v<%= meta.version %> */'
}
});
// Merge additional configuration
grunt.config.merge({
watch: {
files: ['src/*.js'],
tasks: ['concat:dev']
}
});Get and set configuration properties with support for deep property paths.
/**
* Get or set configuration property
* @param {string} [prop] - Property path (dot notation)
* @param {*} [value] - Value to set (if provided)
* @returns {*} Property value or entire config if no prop specified
*/
grunt.config = function(prop, value) {};
/**
* Get configuration property with template processing
* @param {string} [prop] - Property path (dot notation)
* @returns {*} Processed property value or entire config
*/
grunt.config.get = function(prop) {};
/**
* Get raw configuration property without template processing
* @param {string} [prop] - Property path (dot notation)
* @returns {*} Raw property value or entire config
*/
grunt.config.getRaw = function(prop) {};
/**
* Set configuration property
* @param {string} prop - Property path (dot notation)
* @param {*} value - Value to set
*/
grunt.config.set = function(prop, value) {};Usage Examples:
// Set configuration values
grunt.config.set('concat.dev.options.separator', '\n\n');
grunt.config.set('meta.version', '2.0.0');
// Get configuration values
const version = grunt.config.get('meta.version');
const concatFiles = grunt.config.get('concat.dev.files');
const entireConfig = grunt.config.get();
// Get raw values (without template processing)
const rawBanner = grunt.config.getRaw('concat.options.banner');
// Using shorthand syntax
grunt.config('uglify.prod.options.mangle', true);
const mangleOption = grunt.config('uglify.prod.options.mangle');Process templates within configuration values using Lodash template syntax.
/**
* Process templates in a value recursively
* @param {*} value - Value to process (can be string, object, array)
* @returns {*} Value with templates processed
*/
grunt.config.process = function(value) {};
/**
* Escape dots in property names for safe property access
* @param {string} str - String to escape
* @returns {string} Escaped string
*/
grunt.config.escape = function(str) {};
/**
* Convert property path to string format
* @param {string|Array} prop - Property path
* @returns {string} String representation of property path
*/
grunt.config.getPropString = function(prop) {};Usage Examples:
// Configuration with templates
grunt.initConfig({
meta: {
name: 'MyProject',
version: '1.2.0'
},
concat: {
options: {
banner: '/*! <%= meta.name %> v<%= meta.version %> */\n'
}
},
paths: {
src: 'src',
dist: 'dist/<%= meta.version %>'
}
});
// Process templates manually
const banner = grunt.config.process('<%= concat.options.banner %>');
const distPath = grunt.config.process('<%= paths.dist %>');
// Templates are processed automatically when accessing via grunt.config.get()
const processedBanner = grunt.config.get('concat.options.banner');
// Result: "/*! MyProject v1.2.0 */\n"
// Complex template processing
const templateString = 'Build <%= meta.name %> v<%= meta.version %> to <%= paths.dist %>';
const processed = grunt.config.process(templateString);
// Result: "Build MyProject v1.2.0 to dist/1.2.0"Validate that required configuration properties exist.
/**
* Require that specified configuration properties exist
* @param {string} props - Space-separated property paths
* @throws {Error} If any required property is missing
*/
grunt.config.requires = function(props) {};Usage Examples:
// Inside a task, ensure required config exists
grunt.registerTask('build', function() {
// Require specific properties
grunt.config.requires('concat.dev.files');
grunt.config.requires('uglify.prod.files');
// Require multiple properties
grunt.config.requires('meta.version meta.name paths.src paths.dist');
// Continue with task logic...
});
// Multi-task context usage
grunt.registerMultiTask('compile', function() {
// Require configuration for current target
this.requiresConfig('compile.' + this.target + '.files');
// Require global options
this.requiresConfig('compile.options.compiler');
});Direct access to the configuration data object.
/**
* The actual configuration data object
* @type {object}
*/
grunt.config.data = {};Usage Examples:
// Direct access to config data (not recommended for normal use)
const configData = grunt.config.data;
// Iterate over all configuration
Object.keys(grunt.config.data).forEach(function(taskName) {
console.log('Task:', taskName);
console.log('Config:', grunt.config.data[taskName]);
});
// Check if configuration exists
if ('concat' in grunt.config.data) {
console.log('Concat task is configured');
}Complex configuration scenarios and best practices.
Dynamic Configuration:
// Build configuration dynamically
const buildConfig = {};
// Add tasks based on environment
if (process.env.NODE_ENV === 'production') {
buildConfig.uglify = {
prod: {
files: {
'dist/app.min.js': ['src/*.js']
}
}
};
}
// Add file patterns dynamically
const srcFiles = grunt.file.expand('src/**/*.js');
buildConfig.jshint = {
all: srcFiles
};
grunt.initConfig(buildConfig);Shared Configuration:
// Shared configuration patterns
const sharedConfig = {
meta: {
banner: '/*! Project <%= pkg.version %> */',
pkg: grunt.file.readJSON('package.json')
},
paths: {
src: 'src',
dist: 'dist',
tmp: '.tmp'
}
};
grunt.initConfig({
// Spread shared config
...sharedConfig,
// Task-specific config
concat: {
options: {
banner: '<%= meta.banner %>'
},
dist: {
src: ['<%= paths.src %>/*.js'],
dest: '<%= paths.dist %>/app.js'
}
}
});Configuration Inheritance:
// Base configuration
const baseConfig = {
options: {
encoding: 'utf8',
strip: true
}
};
grunt.initConfig({
process: {
// Inherit base options
dev: {
options: {
...baseConfig.options,
debug: true
},
files: {...}
},
prod: {
options: {
...baseConfig.options,
minify: true
},
files: {...}
}
}
});