-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
201 lines (170 loc) · 5.55 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var gulp = require('gulp');
var connect = require('gulp-connect');
var gprint = require('gulp-print');
var gutil = require('gulp-util');
function logBabelError(err) {
// format error https://github.com/zertosh/errorify/issues/4
var message = [err.message, err.codeFrame].join('\n\n');
console.log(message);
}
function transformES6(file) {
var babelify = require('babelify');
return babelify(file, {
optional: [
'es7.objectRestSpread',
'es7.classProperties',
'es7.decorators',
]
});
}
gulp.task('watch-js', function() {
// https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
// http://christianalfoni.github.io/javascript/2014/08/15/react-js-workflow.html
var watchify = require('watchify');
var browserify = require('browserify');
var stringify = require('stringify');
var source = require('vinyl-source-stream');
var watcher = watchify(browserify({
entries: ['./client/js/main.js'],
debug: true,
extension: ['.jsx'],
transform: [
transformES6,
stringify({
extensions: ['.svg'],
minify: true,
}
)],
cache: {},
packageCache: {},
fullPaths: true,
}));
watcher.on('log', gutil.log);
watcher.on('update', function() {
watcher.bundle()
.on('error', logBabelError)
.pipe(source('main.js'))
.pipe(gulp.dest('./build/js/'))
.pipe(gprint())
.pipe(connect.reload());
});
return watcher
.bundle()
.on('error', logBabelError)
.pipe(source('main.js'))
.pipe(gulp.dest('./build/js/'))
.pipe(gprint());
});
gulp.task('build-js', function() {
var browserify = require('browserify');
var stringify = require('stringify');
var source = require('vinyl-source-stream');
return browserify({
entries: ['./client/js/main.js'],
debug: false,
extension: ['.jsx'],
transform: [
transformES6,
stringify({
extensions: ['.svg'],
minify: true,
}
)],
cache: {},
packageCache: {},
fullPaths: true,
})
.bundle()
.on('error', logBabelError)
.pipe(source('main.js'))
.pipe(gulp.dest('./build/js/'))
.pipe(gprint());
});
gulp.task('clean', function () {
var gclean = require('gulp-clean');
return gulp.src('build/', {read: false})
.pipe(gclean());
});
gulp.task('styles', function () {
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('autoprefixer-core');
var autoprefix = autoprefixer({browsers: ['last 2 version']});
gulp.src('client/scss/main.scss')
.pipe(sass())
.pipe(postcss([autoprefix]))
.pipe(gulp.dest('build/css/'))
.pipe(gprint())
.pipe(connect.reload());
});
gulp.task('copy-html', function(){
gulp.src('client/*.html')
.pipe(gulp.dest('build/'))
.pipe(gprint());
});
gulp.task('copy-data', function() {
var symlink = require('gulp-symlink');
return gulp.src('data')
.pipe(symlink('build/data'));
});
gulp.task('copy-images', function() {
var symlink = require('gulp-symlink');
return gulp.src('client/images')
.pipe(symlink('build/images'));
});
gulp.task('watch', function() {
gulp.watch('client/scss/**/*.scss', [ 'styles' ]);
gulp.watch('client/*.html', [ 'copy-html' ]);
});
gulp.task('webserver', function() {
connect.server({
livereload: true,
root: 'build',
host: '0.0.0.0',
port: 1234,
});
});
gulp.task('deploy-gh-pages', function() {
var ghPages = require('gulp-gh-pages');
return gulp.src('./build/**/**/*.*')
.pipe(ghPages({message: 'Deploy to gh-pages :deciduous_tree: ' + new Date()}));
});
// (ohgodwhy)
// Watch tasks should depend on suppress-errors - it will force all stream pipes to print but not crash on error
gulp.task('suppress-errors', function(){
function monkeyPatchPipe(o){
while(!o.hasOwnProperty('pipe')){
o = Object.getPrototypeOf(o);
if(!o){
return;
}
}
var originalPipe = o.pipe;
var newPipe = function(){
var result = originalPipe.apply(this, arguments);
if(!result.pipe['monkey patched for suppress-errors']){
monkeyPatchPipe(result);
}
return result.on('error', function (err) {
gutil.log(gutil.colors.yellow(err));
gutil.beep();
this.emit('end');
});
};
newPipe['monkey patched for suppress-errors'] = true;
o.pipe = newPipe;
}
monkeyPatchPipe(gulp.src(""));
});
gulp.task('inject-cordova', function() {
var htmlreplace = require('gulp-html-replace');
return gulp.src('./build/index.html')
.pipe(htmlreplace({
'cordova': '<script src="cordova.js"></script>',
}))
.pipe(gulp.dest('./build/'));
});
gulp.task('build', ['styles', 'build-js', 'copy-html', 'copy-data', 'copy-images']);
gulp.task('default', ['suppress-errors', 'clean', 'styles', 'copy-html', 'copy-data', 'copy-images', 'webserver', 'watch', 'watch-js']);
gulp.task('frontend', ['clean', 'copy-html', 'styles', 'webserver', 'watch']);