JosefJezek
7/10/2013 - 9:39 AM

grunt-contrib-watchでlivereload

grunt-contrib-watchでlivereload

{
    "name": "",
    "version": "0.0.1",
    "devDependencies": {
        "grunt": "~0.4.1",
        "grunt-contrib-jshint": "~0.6.2",
        "grunt-contrib-watch": "~0.5.1",
        "grunt-contrib-less": "~0.6.4",
        "grunt-contrib-connect": "~0.3.0",
        "connect-livereload": "~0.2.0"
    }
}
module.exports = function (grunt) {
    'use strict';
	
	// grunt-contrib-watchのlivereloadオプションでlivereload
	grunt.initConfig({

		watch: {
			html: {
				files: '**/*.html',
				tasks: [],
				options: {
					livereload: true,
					nospawn: true
				}
			},
			less: {
				files: ['./css/less/*'],
				tasks: ['less'],
				options: {
					livereload: true,
					nospawn: true
				}
			},
			js: {
				files: 'js/*.js',
				tasks: ['jshint'],
				options: {
					livereload: true,
					nospawn: true
				}
			}
		},

		connect: {
			livereload: {
				options: {
					port: 9001,
					hostname: '0.0.0.0',
					base: './',
					middleware: function (connect, options) {
						return [
							require('connect-livereload')(),
							connect.static(options.base),
							connect.directory(options.base)
						];
					}
				}
			}
		},

		jshint: {
			files: {
				src: ['js/*.js']
			},

			options: {
				eqeqeq: true,
				globals: {
					jQuery: true
				}
			}
		},

		less: {
			development: {
				options: {
					compress: true,
					yuicomepress: true
				},
				files: {
					'css/common.css': 'css/less/common.less'
				}
			}
		}

	});

	grunt.event.on('watch', function (action, filepath) {
		grunt.log.write(action, filepath);
	});

	grunt.loadNpmTasks('grunt-contrib-watch');
	grunt.loadNpmTasks('grunt-contrib-jshint');
	grunt.loadNpmTasks('grunt-contrib-connect');
	grunt.loadNpmTasks('grunt-contrib-less');

	grunt.registerTask('default', ['connect', 'watch']);
};