On Github boyney123 / IntroductionToGrunt
Created by @boyney123
Created by @boyney123
Guess the software
npm install -g grunt-cli
try {
gruntpath = resolve('grunt', {basedir: basedir});
} catch (ex) {
gruntpath = findup('lib/grunt.js');
// No grunt install found!
if (!gruntpath) {
if (options.version) { process.exit(); }
if (options.help) { info.help(); }
info.fatal('Unable to find local grunt.', 99);
}
}
// Everything looks good. Require local grunt and run it.
require(gruntpath).cli();
Full file here grunt.js
{
"name": "my-project-name",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-uglify": "~0.2.2"
}
}
npm init //command will create basic package.json file
//wrapper function
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Default task(s) and custom tasks
grunt.registerTask('default', ['uglify']);
};
npm install {plugin name} --save-dev
npm install grunt-contrib-cssmin --save-dev
grunt.loadNpmTasks({plugin name});
grunt.loadNpmTasks('grunt-contrib-cssmin');
//wrapper function
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
},
//Minify all contents of a release directory and add a .min.css extension
cssmin: {
minify: {
expand: true,
cwd: 'release/css/',
src: ['*.css', '!*.min.css'],
dest: 'release/css/',
ext: '.min.css'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Load the css min
grunt.loadNpmTasks('grunt-contrib-cssmin');
// Default task(s) and custom tasks
grunt.registerTask('default', ['uglify', 'cssmin']);
// New task to just minify the css
grunt.registerTask('css', ['cssmin']);
};
grunt
grunt css
npm install grunt-contrib-watch --save-dev
grunt.loadNpmTasks('grunt-contrib-watch');
watch: {
scripts: {
files: ['**/*.js'],
tasks: ['jshint'],
options: {
spawn: false,
},
},
},
npm install grunt-prompt --save-dev
grunt.loadNpmTasks('grunt-prompt');
grunt.initConfig({
prompt: {
target: {
options: {
questions: [
{
config: 'config.name', // arbitray name or config for any other grunt task
type: '<question type="">', // list, checkbox, confirm, input, password
message: 'Question to ask the user',
default: 'value', // default value if nothing is entered
choices: 'Array|function(answers)',
validate: function(value), // return true if valid, error message if invalid
filter: function(value), // modify the answer
when: function(answers) // only ask this question when this function returns true
}
]
}
},
},
})
</question>
npm install grunt-uncss --save-dev
grunt.loadNpmTasks('grunt-uncss');
uncss: {
dist: {
files: {
'dist/css/tidy.css': ['app/index.html','app/about.html']
}
}
}
npm install grunt-contrib-imagemin --save-dev
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.initConfig({
imagemin: { // Task
static: { // Target
options: { // Target options
optimizationLevel: 3
},
files: { // Dictionary of files
'dist/img.png': 'src/img.png', // 'destination': 'source'
'dist/img.jpg': 'src/img.jpg',
'dist/img.gif': 'src/img.gif'
}
},
dynamic: { // Another target
files: [{
expand: true, // Enable dynamic expansion
cwd: 'src/', // Src matches are relative to this path
src: ['**/*.{png,jpg,gif}'], // Actual patterns to match
dest: 'dist/' // Destination path prefix
}]
}
}
});
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.registerTask('default', ['imagemin']);
npm install grunt-inline-css --save-dev
grunt.loadNpmTasks('grunt-inline-css');
grunt.initConfig({
inlinecss: {
main: {
options: {
},
files: {
'out.html': 'in.html'
}
}
}
})
gruntjs.com
@gruntjs
@gruntweekly