Last active
August 29, 2015 13:57
-
-
Save konsumer/9358851 to your computer and use it in GitHub Desktop.
a quick example of minification, optimization, LESS, and auto-dependencies with grunt. Put index.html in app/ Put site.less in app/less/ Put your images in app/img/ Do `npm install` to install all the dev tools, then run `grunt` to build. `grunt serve` runs a dev webserver for app/. You can update bower deps with `grunt bower`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"directory": "app/bower_components" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
grunt.initConfig({ | |
'pkg': grunt.file.readJSON('package.json'), | |
'cfg': { | |
'dist': 'dist', | |
'app': 'app', | |
'tmp': '.tmp' | |
}, | |
'clean': { | |
'prod': { | |
'src': ['<%= cfg.tmp %>', '<%= cfg.dist %>'] | |
} | |
}, | |
'bowerInstall': { | |
'default':{ | |
'src': [ | |
'<%= cfg.app %>/*.html', | |
], | |
'ignorePath': '<%= cfg.app %>/' | |
} | |
}, | |
'copy': { | |
'prod': { | |
'files': [{ | |
'expand': true, | |
'dot': true, | |
'cwd': '<%= cfg.app %>/', | |
'dest': '<%= cfg.dist %>/', | |
'src': [ | |
'fonts/**', | |
'bower_components/**', | |
'js/**', | |
], | |
'filter': 'isFile' | |
}] | |
} | |
}, | |
'imagemin': { | |
'prod':{ | |
'files': [{ | |
'expand': true, | |
'cwd': '<%= cfg.app %>/', | |
'src': ['img/**/*.{png,jpg,gif}'], | |
'dest': '<%= cfg.dist %>/' | |
}] | |
} | |
}, | |
'less': { | |
'prod': { | |
'options': { | |
'paths': ["<%= cfg.app %>/less"] | |
}, | |
'files': { | |
"<%= cfg.dist %>/css/site.css": "<%= cfg.app %>/less/site.less", | |
"<%= cfg.dist %>/css/ie.css": "<%= cfg.app %>/less/ie.less" | |
} | |
} | |
}, | |
'string-replace': { | |
'prod': { | |
'files': { | |
'<%= cfg.dist %>/index.html': '<%= cfg.app %>/index.html' | |
}, | |
'options': { | |
'replacements': [ | |
{ | |
'pattern': /<!-- DEV -->.+<!-- \/DEV -->/g, | |
'replacement': '' | |
}, | |
{ | |
'pattern': /<link rel="stylesheet\/less" type="text\/css" href="less\/(.+)\.less" \/>/ig, | |
'replacement': '<link rel="stylesheet" type="text/css" href="css/$1.css" />' | |
} | |
] | |
} | |
} | |
}, | |
'uglify':{ | |
'options':{ | |
'mangle':false // needed for angular | |
} | |
}, | |
'useminPrepare': { | |
'html': ['<%= cfg.dist %>/*.html'] | |
}, | |
'usemin': { | |
'html': ['<%= cfg.dist %>/*.html'], | |
'options': { | |
'dirs': ['<%= cfg.dist %>/'] | |
} | |
}, | |
'connect': { | |
'server': { | |
'options': { | |
'port': 9001, | |
'base': 'app', | |
'keepalive':true | |
} | |
} | |
} | |
}); | |
require('load-grunt-tasks')(grunt); | |
grunt.registerTask('bower', ['bowerInstall']); | |
grunt.registerTask('server', ['connect']); | |
grunt.registerTask('default', ['clean:prod','bowerInstall', 'copy:prod', 'string-replace:prod', 'less:prod', 'useminPrepare', 'usemin', 'concat', 'uglify', 'cssmin', 'clean:build', 'imagemin:prod']); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>Site</title> | |
<!-- build:css css/site.css --> | |
<!-- bower:css --> | |
<!-- endbower --> | |
<link rel="stylesheet/less" type="text/css" href="less/site.less" /> | |
<!-- YOUR CSS/LESS HERE, OR IMPORT IN less/site.less --> | |
<!-- endbuild --> | |
<!--[if lt IE 8]> | |
<!-- build:css css/ie.css --> | |
<link rel="stylesheet/less" type="text/css" href="less/ie.less" /> | |
<!-- endbuild --> | |
<![endif]--> | |
<!-- DEV --><script src="http://cdnjs.cloudflare.com/ajax/libs/less.js/1.6.3/less.min.js"></script><!-- /DEV --> | |
</head> | |
<body> | |
<div class="container">My cool site goes here.</div> | |
<!-- build:js site.js --> | |
<!-- bower:js --> | |
<!-- endbower --> | |
<!-- YOUR SCRIPTS HERE --> | |
<!-- endbuild --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "myApp", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "grunt server", | |
"postinstall": "bower install" | |
}, | |
"author": "David Konsumer <konsumer@jetboystudio.com>", | |
"license": "MIT", | |
"devDependencies": { | |
"grunt": "^0.4.2", | |
"bower": "^1.2.8", | |
"grunt-string-replace": "^0.2.7", | |
"grunt-usemin": "^2.0.2", | |
"grunt-contrib-less": "^0.9.0", | |
"grunt-contrib-clean": "^0.5.0", | |
"grunt-contrib-imagemin": "^0.5.0", | |
"grunt-contrib-copy": "^0.5.0", | |
"grunt-contrib-concat": "^0.3.0", | |
"grunt-contrib-uglify": "^0.3.3", | |
"load-grunt-tasks": "^0.4.0", | |
"grunt-bower-install": "^1.0.2", | |
"grunt-contrib-cssmin": "^0.9.0", | |
"grunt-contrib-connect": "^0.7.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment