Last active
October 7, 2015 08:46
-
-
Save tdpauw/b586302c78ffda3ea309 to your computer and use it in GitHub Desktop.
Example gulpfile.js with index.html generation and live reload
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
/*jshint node:true */ | |
'use strict'; | |
// var pkg = require('./package.json'); | |
var buildCfg = require( './build.config.js' ); | |
var gulp = require('gulp'); | |
var del = require('del'); | |
var jshint = require('gulp-jshint'); | |
var inject = require('gulp-inject'); | |
var connect = require('gulp-connect'); | |
var open = require('gulp-open'); | |
var merge = require('merge-stream'); | |
var series = require('stream-series'); | |
gulp.task('clean', function() { | |
return del([buildCfg.buildDir, buildCfg.distDir]); | |
}); | |
gulp.task('lint', function() { | |
return gulp.src(['gulpfile.js', 'build.config.js'].concat(buildCfg.appFiles.js).concat(buildCfg.appFiles.jsunit)) | |
.pipe(jshint()) | |
.pipe(jshint.reporter('jshint-stylish')) | |
.pipe(jshint.reporter('fail')); | |
}); | |
gulp.task('copy', ['clean', 'lint'], function() { | |
var appJs = gulp.src(buildCfg.appFiles.js, {base: buildCfg.srcDir}) | |
.pipe(gulp.dest(buildCfg.buildDir)); | |
var vendorJs = gulp.src(buildCfg.vendorFiles.js, {base: '.'}) | |
.pipe(gulp.dest(buildCfg.buildDir)); | |
var vendorCss = gulp.src(buildCfg.vendorFiles.css, {base: '.'}) | |
.pipe(gulp.dest(buildCfg.buildDir)); | |
var atpl = gulp.src(buildCfg.appFiles.atpl, {base: buildCfg.srcDir}) | |
.pipe(gulp.dest(buildCfg.buildDir)); | |
var ctpl = gulp.src(buildCfg.appFiles.ctpl, {base: buildCfg.srcDir}) | |
.pipe(gulp.dest(buildCfg.buildDir)); | |
return merge(appJs, vendorJs, vendorCss, atpl, ctpl); | |
}); | |
gulp.task('index', ['copy'], function() { | |
var source = gulp.src(buildCfg.srcDir + '/index.html'); | |
var vendorJs = gulp.src(buildCfg.vendorFiles.js, {read: false}); | |
var vendorCss = gulp.src(buildCfg.vendorFiles.css, {read: false}); | |
var appJs = gulp.src(['app/**/*.js', 'common/**/*.js'], {read: false, cwd: buildCfg.buildDir}); | |
return source.pipe(inject(series(vendorJs, appJs, vendorCss))).pipe(gulp.dest(buildCfg.buildDir)); | |
}); | |
gulp.task('connect', function() { | |
connect.server({ | |
root: buildCfg.buildDir, | |
port: 9000, | |
livereload: true | |
}); | |
}); | |
gulp.task('open', function() { | |
gulp.src(__filename) | |
.pipe(open({uri: 'http://localhost:9000', app: 'firefox'})); | |
}); | |
gulp.task('reload', function() { | |
gulp.src(buildCfg.appFiles.index) | |
.pipe(connect.reload()); | |
}); | |
gulp.task('watch', function() { | |
gulp.watch(buildCfg.appFiles.index.concat(buildCfg.appFiles.js).concat(buildCfg.appFiles.atpl), ['build', 'reload']); | |
}); | |
gulp.task('build', ['clean', 'lint', 'copy', 'index']); | |
gulp.task('server', ['connect', 'open', 'watch']); | |
gulp.task('default', ['build']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment