On Github TheRealAlan / wordcamp2014
JavaScript Plugin / Framework Management & Updating
All the Rest
html // your public folder assets // the files you will be editing └─ less └─ js └─ img bower_components // home to your Bower packages node_modules // home to your Node packages tmp // holds concatenated js to lint bower.json // the list of your Bower packages package.json // the list of your Node packages Gruntfile.js // where the Grunt magic happens .jshintrc // your JS Hint config file (optional)This layout is for custom themes. Developers building themes for resale will want to move their Bower and Grunt files into the theme folder itself.
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | shInstall Homebrew with
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)Get Homebrew up to date and clean by running brew update and brew doctor, then add it to the path with
export PATH="/usr/local/bin:$PATH" >> ~/.bash_profileInstall Node.js with brew install node Install the Grunt CLI with npm install -g grunt-cli Install Bower with npm install -g bower This assumes you're on a Mac. Homebrew requires xCode to be installed first. If prompted to install Xcode Command Line Tools, choose Yes.
Create a file called bower.json.
{ "name": "your-project-name", "version": "1.0.0", "dependencies": { } }
Create a file called package.json.
{ "name": "your-project", "version": "1.0.0", "dependencies": { } }
Create a file called Gruntfile.js.
'use strict'; module.exports = function (grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // package options will go here }); // register tasks here grunt.registerTask('default', [ // default actions go here ]); };With this file created, Grunt is up and running. You'll execute it by going to the project directory in the terminal and typing 'grunt'. But, in its current state, it won't actually do anything. This sample configuration doesn't have any packages loaded. Before we load any packages, we need to know what we want Grunt to do for us.
Get the Bower packages you want from their registry.
First think about what you will always need for your project. What frameworks do you use? Bootstrap? Foundation? Font Awesome? jQuery? Zepto? Round these up with Bower (don't forget about the package directory [here](http://sindresorhus.com/bower-components/)). Do you use LESS or SASS? Get those files set up. Then depending on what you need out of these frameworks, you'll configure Grunt to process your CSS, lint and concatenate your JavaScript, and minify both.You'll want the LESS, SASS, or some other package if your CSS preference is different.
npm install grunt-contrib-less --save-dev
or
npm install grunt-contrib-compass --save-dev
Get JSLint.
npm install grunt-jslint --save-dev
You'll want this.
npm install grunt-contrib-concat --save-dev
Get Uglify.
npm install grunt-contrib-uglify --save-dev
Try Grunt Notify.
npm install grunt-notify --save-dev
I like Imagemin.
npm install grunt-contrib-imagemin --save-dev
You want to use Watch. For updating CSS and JavaScript in the browser without refreshing the page, get the Chrome extension LiveReload.
npm install grunt-contrib-watch --save-dev
Want to set up a node.js server for your project and ditch MAMP? Get Express.
For WordPress, you'll want the PHP version.With the packages you want registered, the 'Load Tasks' section of your file should look something like this:
// Load tasks grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-notify'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-imagemin'); grunt.loadNpmTasks('grunt-contrib-compass');
// package options jshint: { options: { jshintrc: '.jshintrc' // jshint config file }, all: [ 'Gruntfile.js', 'assets/js/*.js' ] }, concat: { basic: { src: [ 'bower_components/jquery/dist/jquery.js', 'bower_components/foundation/js/foundation/foundation.js', 'assets/js/main.js' ], dest: 'tmp/app.js' }, extras: { src: [ 'bower_components/modernizr/modernizr.js' ], dest: 'tmp/modernizr.js' } }, compass: { dist: { options: { config: 'config.rb' } } }, imagemin: { dynamic: { files: [{ expand: true, cwd: 'assets/img/', src: ['**/*.{png,jpg,gif}'], dest: 'public/build/img/' }] } }, uglify: { build: { files: { 'public/build/js/modernizr.min.js' : 'tmp/modernizr.js', 'public/build/js/app.min.js' : 'tmp/app.js' } } }, clean: { dist: [ 'tmp/**', 'public/build/img/**' ] },Next you'll want to set the options (under the comment 'package options' in the sample file) for each task that will be run. A few things to note here - I'm using a 'tmp' folder to concatenate the JavaScript to, before it's minified. I also have my 'production' files in an `assets` folder outside of the public folder, and everything is processed into a `build` folder inside my public folder. You don't have to do this, but I prefer to keep my source files and processed files separate. Also, watch your commas. As you nest options, it's easy to miss one.
A sample configuration:
watch: { compass: { files: ['assets/sass/**/*.{scss,sass}'], tasks: ['compass'] }, css: { files: ['public/build/css/*'], options: { livereload: true } }, js: { files: [ 'assets/js/*.js' ], tasks: ['concat', 'uglify'], options: { livereload: true, atBegin: true } }, imagemin: { files: [ 'assets/img/**' ], tasks: ['imagemin'], options: { livereload: true, atBegin: true } } }'Watch' is a very powerful Grunt package that continues to run until you tell it to stop. This is super useful for development tasks like rebuilding your files as you make changes and refreshing the browser. What's happening here is for as long as Grunt Watch is running, it's watching all of our production files and re-running the tasks we tell it to whenever one of those files changes. This can be run with the command `grunt watch`, but there's a simpler way we can incorporate this by including it in our default tasks.
// Register default tasks grunt.registerTask('default', [ 'watch' ]);Any tasks inside 'default' will be run whenever you run the command 'grunt'. This configuration runs Grunt's 'watch' function. With this, all you need to type to get everything up and running is the simple command 'grunt'.
With all of these modules registered and configured, your Gruntfile.js should look something like this:
module.exports = function (grunt) { 'use strict'; grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // package options jshint: { options: { jshintrc: '.jshintrc' }, all: [ 'Gruntfile.js', 'tmp/js/*.js' ] }, concat: { basic: { src: [ 'bower_components/jquery/dist/jquery.js', 'bower_components/foundation/js/foundation/foundation.js', 'assets/js/main.js' ], dest: 'tmp/app.js' }, extras: { src: [ 'bower_components/modernizr/modernizr.js' ], dest: 'tmp/modernizr.js' } }, compass: { dist: { options: { config: 'config.rb' } } }, imagemin: { dynamic: { files: [{ expand: true, cwd: 'assets/img/', src: ['**/*.{png,jpg,gif}'], dest: 'public/build/img/' }] } }, uglify: { build: { files: { 'public/build/js/modernizr.min.js' : 'tmp/modernizr.js', 'public/build/js/app.min.js' : 'tmp/app.js' } } }, clean: { dist: [ 'tmp/**', 'public/build/img/**' ] }, watch: { compass: { files: ['assets/sass/**/*.{scss,sass}'], tasks: ['compass'] }, css: { files: ['public/build/css/*'], options: { livereload: true } }, js: { files: [ 'assets/js/*.js' ], tasks: ['concat', 'uglify'], options: { livereload: true, atBegin: true } }, imagemin: { files: [ 'assets/img/**' ], tasks: ['imagemin'], options: { livereload: true, atBegin: true } } } }); // Load tasks grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-notify'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-imagemin'); grunt.loadNpmTasks('grunt-contrib-compass'); // Register default tasks grunt.registerTask('default', [ 'watch' ]); };