0.2.1 • Published 10 years ago

grunt-split v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

#grunt-split

Grunt plugin that lets you split your gruntfile configuration into multiple files. You are not limited to defining one task per configuration file. Each file extends the configuration object meaning each file can contain a slice of the full configuration stack. This is particularly useful if you distribute a seed project in which you want separate features to be enabled or disabled from the build process.

##TL;DR

  • Each configuration file can contain any number of task configurations.
  • Configuration files are merged in the order in which they are included.
  • See the section on merge conflict resolution below for the merge rules.

##Installation

npm install -D grunt-split

##Grunt-Split Syntax

var loadConfig=require('grunt-split');
var settings={
	files: ['path/to/file.conf.js','path/to/another/file.conf.js']
};

var parameters={
	anyVariable: "you want to propagate to all the called scripts",
	I_find: "this a great place to specify folders in your project",
	folders: {
		bower: '/bower_components/',
		npm:'/npm_modules/'
	}
}

var config=loadConfig(settings,parameters)
// grunt.initConfig(config);
// Deprecated v0.2.0

config.initConfig();
// Can also call grunt.initConfig(config.getConfig()) since v0.2.0
	

##Separate Config File Syntax The object returned on module.exports for each of the split config files have 3 possible properties in the root object:

module.exports={
	npm_tasks:[],
	config: {},
	aliasses: {}
}

####npm_tasks This allows you to set the grunt dependencies for that particular configuration file. Any tasks listed in this array will be loaded using grunt.npmLoadTasks(). That way you can keep your grunt instance as thin as possible.

####config This is where the configuration lives. Configure any grunt plugins here keeping in mind how the merge-rules will determine how they merge with other configuration files.

####aliasses This is where you can specify grunt task aliasses that will be registered via grunt.registerTask(). #####syntax

module.exports={
	aliasses:{
		jobName:['task','task']
	}
}

##Example

module.exports = function(grunt) {

	var config=require('grunt-split')(grunt,{
		files:[
			'grunt/bootstrap.conf.js',
		//	'grunt/angular.conf.js',
			'grunt/font-awesome.conf.js',
		]
	});

	// grunt.initConfig(config); 
	// Deprecated v0.2.0

	config.initConfig();
	// Can also call grunt.initConfig(config.getConfig()) since v0.2.0
	

	grunt.registerTask('default',[]);
};

grunt/bootstrap.conf.js

module.exports = {
	npm_tasks:[
		'grunt-contrib-concat',
		'grunt-contrib-watch',
		'grunt-contrib-less'
	],
	config:{
		less:{
			bootstrap:{
				//bootstrap less compilation configuration goes here
			}
		},
		concat:{
			bootstrap:{
				//concatenate any css and javascript here
			}
		},
		watch:{
			bootstrap:{
				//instructions on what to watch
			}
		}
	},
	aliasses:{
		'watch_all',['watch:bootstrap']
	}
}

##Alternative Implementation There is an alternative implementation that allows one to send parameters from the calling grunt file to all the includes.

module.exports = function(grunt) {

	var config = require('grunt-split')(grunt,{
		files:[
			'grunt/bootstrap.conf.js',
		//	'grunt/angular.conf.js',
			'grunt/font-awesome.conf.js',
		]
	},parameters);
	
	grunt.initConfig(config);
	grunt.registerTask('default',[]);
};

grunt/bootstrap.conf.js

module.exports = function(parameters){
	var config={
		npm_tasks:[
			'grunt-contrib-concat',
			'grunt-contrib-watch',
			'grunt-contrib-less'
		],
		config:{
			less:{
				bootstrap:{
					//bootstrap less compilation configuration goes here
					//use parameters here
				}
			},
			concat:{
				bootstrap:{
					//concatenate any css and javascript here
				}
			},
			watch:{
				bootstrap:{
					//instructions on what to watch
				}
			}
		},
		aliasses:{
			'watch_all',['watch:bootstrap']
		}
	};

	return config;
}

##Merge Conflict Resolution It is useful knowing how the objects read from the different files get extended when a conflict is encountered

###Object Properties get overridden

// File 1
obj={
	key1:'value1'
}

// File 2
obj={
	key1:'value2',
	key2:'value2'
}

// Results In
obj={
	key1:'value2',
	key2:'value2'
}

###Object Properties get overridden unless they are objects, then they are extended

// File 1
obj={
	key1:{keyA:'value1'},
	key2:{keyA:'value1',keyB:'value2'}
}

// File 2
obj={
	key1:'value3',
	key2:{keyA:'value3',keyC:'value3'}
}

// Results In
obj={
	key1:'value3',
	key2:{keyA:'value3',keyB:'value2',keyC:'value3'}
}

###Array Properties get augmented

// File 1
obj={
	key1:['value1']
}

// File 2
obj={
	key1:['value2']
}

// Results In
obj={
	key1:['value1','value2'],
}
0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago