-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
67 lines (61 loc) · 2.1 KB
/
Gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Gulpfile.js --- run `gulp` in terminal
var gulp = require('gulp')
var connect = require('connect')
var connectLivereload = require('connect-livereload')
var opn = require('opn')
var gulpLivereload = require('gulp-livereload')
/*
* Main Config
* ===========
*/
var config = {
// this is your local directory to become served as root,
// e.g. `localhost:8080` should point to show `index.html` in that directory
rootDir: __dirname,
// any port to use for your local server
servingPort: 8081,
// the files you want to watch for changes for live reload
// replace by any glob pattern matching your files
filesToWatch: ['*.{html,css,js}', '!Gulpfile.js']
}
/*
* Gulp Tasks
* ==========
*/
// `gulp` default task - called when you run `gulp` from CLI
// --------------
gulp.task('default', ['watch', 'serve'])
// `gulp watch` task watching for file changes
// ------------
gulp.task('watch', ['connect'], function () {
// start livereload server (at the default port 35729)
// https://github.com/vohof/gulp-livereload#install
gulpLivereload.listen()
// watch for file changes
gulp.watch(config.filesToWatch, function (file) {
// get the changed file
// not needed here but useful for fine grained customizations
gulp.src(file.path)
// notify server about changes
.pipe(gulpLivereload())
})
})
// `gulp serve` task loading the URL in your browser
// ------------
gulp.task('serve', ['connect'], function () {
opn('http://localhost:' + config.servingPort)
})
// `gulp connect` task starting your server
// --------------
gulp.task('connect', function () {
// connect server for our files (unrelated to the livereload server)
connect()
// inject JavaScript into our page with `index.html` to listen for change notifications:
// <script src="//localhost:35729/livereload.js?snipver=1"></script>
.use(connectLivereload())
// specify the root directory for our connect server
.use(connect.static(config.rootDir))
// start the server at the given port
// now we can view our `index.html` in the root under `localhost:port`
.listen(config.servingPort)
})