-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgulpfile.js
116 lines (84 loc) · 2.56 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
107
108
109
110
111
112
113
114
115
116
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var livereload = require('gulp-livereload');
var connect = require('connect');
var rename = require('gulp-rename');
var browserify = require('browserify');
var watchify = require('watchify');
var es6ify = require('es6ify');
var reactify = require('reactify');
var source = require('vinyl-source-stream');
/** Config variables */
var serverPort = 8888;
var lrPort = 35731;
/** File paths */
var dist = 'dist';
var htmlFiles = 'app/**/*.html';
var htmlBuild = dist;
var jsxFiles = 'app/jsx/**/*.jsx';
var vendorFiles = [
'bower_components/react/react-with-addons.js',
'node_modules/es6ify/node_modules/traceur/bin/traceur-runtime.js'];
var vendorBuild = dist + '/vendor';
var requireFiles = './node_modules/react/react.js';
gulp.task('vendor', function () {
return gulp.src(vendorFiles).
pipe(gulp.dest(vendorBuild));
});
gulp.task('html', function () {
return gulp.src(htmlFiles).
pipe(gulp.dest(htmlBuild));
});
function compileScripts(watch) {
gutil.log('Starting browserify');
var entryFile = './app/jsx/app.jsx';
es6ify.traceurOverrides = {experimental: true};
var bundler;
if (watch) {
bundler = watchify(entryFile);
} else {
bundler = browserify(entryFile);
}
bundler.require(requireFiles);
bundler.transform(reactify);
bundler.transform(es6ify.configure(/.jsx/));
var rebundle = function () {
var stream = bundler.bundle({ debug: true});
stream.on('error', function (err) { console.error(err) });
stream = stream.pipe(source(entryFile));
stream.pipe(rename('app.js'));
stream.pipe(gulp.dest('dist/bundle'));
}
bundler.on('update', rebundle);
return rebundle();
}
gulp.task('server', function (next) {
var server = connect();
server.use(connect.static(dist)).listen(serverPort, next);
});
function initWatch(files, task) {
if (typeof task === "string") {
gulp.start(task);
gulp.watch(files, [task]);
} else {
task.map(function (t) { gulp.start(t) });
gulp.watch(files, task);
}
}
/**
* Run default task
*/
gulp.task('default', ['vendor', 'server'], function () {
var lrServer = livereload(lrPort);
var reloadPage = function (evt) {
lrServer.changed(evt.path);
};
function initWatch(files, task) {
gulp.start(task);
gulp.watch(files, [task]);
}
compileScripts(true);
initWatch(htmlFiles, 'html');
gulp.watch([dist + '/**/*'], reloadPage);
});