-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
106 lines (90 loc) · 3.02 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* The idiomatic ReactJS application compiles JSX as a build step, and doesn't use the in-browser JSX transformer.
* This repo uses a very basic gulp + webpack + babel build to write React with ES6, and build it for the browser
* @type {Gulp|exports}
*/
const gulp = require('gulp')
, gwebpack = require('webpack-stream')
, browserSync = require('browser-sync')
, reload = browserSync.reload
, del = require('del')
, fs = require('fs')
, url = require("url")
, $ = require('gulp-load-plugins')({pattern: ['gulp-*']});
/**
* A simple task to clean any build products
*/
gulp.task('clean', function( cb ) {
del(['build'], cb);
});
/**
* A simple task to copy our HTML file and images to the dist directory
*/
gulp.task('copy', function() {
var html = gulp.src("./app/index.html").pipe(gulp.dest("dist/"))
, vendor = gulp.src("./app/vendor/**.*").pipe(gulp.dest("dist/vendor"))
, images = gulp.src("./app/images/**/*.*").pipe(gulp.dest("dist/images"));
});
/**
* The main step is 'pack' this step takes our 'client.js' file, and builds it using Webpack, the babel-loader plugin to
* transpile ES6 and outputs it to the dist directory
*/
gulp.task('pack', function() {
return gulp.src('./app/client.js')
.pipe(gwebpack({
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
path: __dirname + '/dist/js',
filename: 'client.js'
},
module: {
loaders: [
{test: /\.jsx$/, loader: 'babel?stage=1'},
{test: /\.js$/, exclude: /node_modules/, loader: 'babel?stage=1'}
]
}
}))
.pipe(gulp.dest('dist/'));
});
/**
* Simple watcher that watches for certain changes, and launches the appropriate tasks
*/
gulp.task('watch', ['build'], function() {
browserSync({
notify: true,
port: 8000,
server: {
baseDir: 'dist',
middleware: function( req, res, next ) {
var fileName = url.parse(req.url);
fileName = fileName.href.split(fileName.search).join("");
var fileExists = fs.existsSync('dist' + fileName);
if ( !fileExists && fileName.indexOf("browser-sync-client") < 0 ) {
req.url = "/" + "index.html";
}
return next();
}
}
});
//watch for JS changes
gulp.watch(['app/**/*.jsx', 'app/**/*.js'], ['pack', 'copy']);
//watch for static asset changes
gulp.watch(['app/index.html', 'app/assets/images/**/*'], ['copy']);
var reloading;
gulp.watch(['./dist/**'], function( file ) {
clearTimeout(reloading);
reloading = setTimeout(function() {
reload();
}, 100);
});
});
/**
* The default task is build
*/
gulp.task("default", ["watch"]);
/**
* Define our build task
*/
gulp.task("build", ["copy", "pack"]);