Ummm… build???
grunt.registerTask('default', ['clean', 'copy', 'build-index']);
grunt.registerTask('publish', ['default', 'gh-pages']);
Type "grunt", the default task will run.
"grunt publish" will run default followed by the gh-pages task.
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-gh-pages');
grunt.loadNpmTasks('grunt-contrib-clean');
Each Task is given a name. In this case:
The task name is usually obvious. Documentation will always tell you.
E.g. the gh-pages task documentation
grunt.initConfig({
clean: {
src: 'build/'
},
copy: {
main: {
src: '**/*',
dest: 'build/'
},
gitignore: {
src: '.gitignore',
dest: '<%= copy.main.dest %>'
}
},
'build-index': {
src: 'index.tmpl',
dest: '<%= copy.main.dest %>index.html',
files: '<%= copy.main.src %>'
},
'gh-pages': {
options: {
base: 'build',
dotfiles: true
},
src: '**/*'
}
});
module.exports = function(grunt) {
// Define Tasks, Load Plugins and Configure
};
grunt.initConfig({
copy: {
main: {
src: '**/*',
dest: 'build/'
}
}
});
Built-in
Easy file-sets
Plugins
grunt.initConfig({
copy: {
main: {
src: '**/*',
dest: 'build/'
},
gitignore: {
src: '.gitignore',
dest: '<%= copy.main.dest %>'
}
}
});
Each target processed in turn
grunt.initConfig({
copy: {
main: {
src: '**/*',
dest: 'build/'
}
},
'build-index': {
src: 'index.tmpl',
dest: '<%= copy.main.dest %>index.html',
files: '<%= copy.main.src %>'
}
});
<%= ... %>
inline config references
grunt.initConfig({
'gh-pages': {
options: {
base: 'build',
dotfiles: true
},
src: '**/*'
}
});
Task-specific options, again described in the documentation.
grunt.initConfig({
exclusions: ['build','node_modules'],
copy: {
main: {
src: '**/*',
dest: 'build/',
filter: function(filepath) {
var filepathParts = filepath.split(require('path').sep);
// We will copy all folders except those in the "exclude"
// property (build and node_modules)
var shouldExclude =
(filepathParts.length === 1 && grunt.file.isFile(filepath)) ||
(grunt.config('exclusions').indexOf(filepathParts[0]) !== -1);
if (!shouldExclude) {
// Log what we're copying with some debug info
grunt.verbose.writeln('Including ' + filepath + ' ' +
filepath.split(require('path').sep)[0]);
}
return !shouldExclude;
}
}
}
});
NodeJS fs.Stats method name (e.g. "isDir" or "isFile")
…or custom function
No, really, I don't know. I'll get back to you on this
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-gh-pages');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('default', ['clean', 'copy', 'build-index']);
grunt.registerTask('publish', ['default', 'gh-pages']);
grunt.registerTask('build-index',
'Generate index.html depending on configuration',
function() {
// Read the config and read the template file.
var conf = grunt.config('build-index'),
tmpl = grunt.file.read(conf.src);
// Process the template.
grunt.file.write(conf.dest, grunt.template.process(tmpl));
grunt.log.writeln('Generated \'' + conf.dest + '\' from \'' + conf.src + '\'');
});
// ... Remember the config was:
'build-index': {
src: 'index.tmpl',
dest: '<%= copy.main.dest %>index.html',
files: '<%= copy.main.src %>'
}
Like "default" but with different parameters
grunt.registerTask('default', ['clean', 'copy', 'build-index']);
npm install -g grunt-cli
npm install grunt --save
--save
will add grunt to your package.json's dependencies
section.
E.g. The 100-lines-or-less-builder package.json
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-gh-pages');
grunt.loadNpmTasks('grunt-contrib-clean');
list plugin in package.json:
{
"name": "100-lines-or-less-js",
"version": "0.0.1",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-copy": "*",
"grunt-gh-pages": "~0.9.0",
"grunt-contrib-clean": "~0.5.0"
}
}
Again, can add using --save-dev
npm option