Skip to content

Instantly share code, notes, and snippets.

@jonchretien
Last active December 15, 2015 23:19
Show Gist options
  • Save jonchretien/5339051 to your computer and use it in GitHub Desktop.
Save jonchretien/5339051 to your computer and use it in GitHub Desktop.
Default Gruntfile (v0.4.x).
module.exports = function(grunt) {
// project configuration.
grunt.initConfig({
dirs: {
dest: '_dist'
},
file: 'index.html',
pkg: grunt.file.readJSON('package.json'),
clean: {
minified: ['css/main.min.*.css'],
release: ['<%=dirs.dest%>'],
uglified: ['js/app.min.*.js']
},
cssmin: {
compress: {
files: {
'css/main.min.<%= grunt.template.today("yymmddhhmmss") %>.css': ['css/main.css']
}
}
},
jshint: {
files: ['Gruntfile.js', 'js/base.js', 'js/init.js'],
options: {
globals: {
asi: true,
browser: true,
curly: true,
eqeqeq: true,
forin: false,
immed: false,
newcap: true,
noempty: true,
strict: true,
undef: true,
sub: true
}
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> v<%= pkg.version %> - copyright <%= grunt.template.today("yyyy") %> <%= pkg.author %> - <%= pkg.repository %> */\n'
},
build: {
// src: 'js/app.js',
// dest: 'js/app.min.<%= grunt.template.today("yymmddhhmmss") %>.js'
files: {
'js/app.min.<%= grunt.template.today("yymmddhhmmss") %>.js': ['js/base.js', 'js/init.js']
}
}
},
htmlmin: {
dist: {
options: {
removeComments: true,
collapseWhitespace: true
},
files: {
'<%=dirs.dest%>/index.html': '<%=dirs.dest%>/index.html'
}
}
},
copy: {
main: {
files: [
{
src: ['index.html', 'css/**', 'js/**'],
dest: '<%=dirs.dest%>/'
}
]
}
}
});
// load grunt tasks from NPM packages
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
// creates a new directory
grunt.registerTask('mkdir', grunt.file.mkdir);
// opens a file and updates the href/src attributes for
// CSS and JS files to point to the minified versions
grunt.registerTask( 'update', 'Update file paths.', function(file) {
var script = grunt.file.expand('js/app.min*.js')[0];
var css = grunt.file.expand('css/main.min*.css')[0];
var index = grunt.file.expand( grunt.config.get('dirs.dest') + '/' + file)[0];
var contents = grunt.file.read( index );
var revised = contents.replace( /href\="css\/main\.css"/, 'href="' + css + '"' ).replace( /src\="js\/app\.js"/, 'src="' + script + '"' );
grunt.file.write( index, revised );
});
// default task(s).
grunt.registerTask('default', [
'clean',
'cssmin',
'jshint',
'uglify',
'mkdir:' + grunt.config.get('dirs.dest'),
'copy',
'update:' + grunt.config.get('file'),
'htmlmin'
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment