-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.babel.js
executable file
·254 lines (213 loc) · 6.71 KB
/
gulpfile.babel.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import autoprefixer from 'gulp-autoprefixer';
import babel from 'babel-core/register';
import babelify from 'babelify';
import browserify from 'browserify';
import compileHandlebars from 'gulp-compile-handlebars';
import concat from 'gulp-concat';
import connect from 'connect';
import connectLivereload from 'connect-livereload';
import csso from 'gulp-csso';
import del from 'del';
import domain from 'domain';
import eslint from 'gulp-eslint';
import { existsSync, readFileSync } from 'fs';
import flatten from 'gulp-flatten';
import filter from 'gulp-filter';
import gif from 'gulp-if';
import gulp from 'gulp';
import gutil from 'gulp-util';
import hbsfy from 'hbsfy';
import http from 'http';
/*
We need to import in that way, because there's bug in isparta.
See: https://github.com/douglasduteil/isparta/pull/60
*/
import { Instrumenter } from 'isparta';
import istanbul from 'gulp-istanbul';
import livereload from 'gulp-livereload';
import minifyHtml from 'gulp-minify-html';
import mocha from 'gulp-mocha';
import opn from 'opn';
import plumber from 'gulp-plumber';
import sass from 'gulp-sass';
import sequence from 'run-sequence';
import serveStatic from 'serve-static';
import serveIndex from 'serve-index';
import size from 'gulp-size';
import tap from 'gulp-tap';
import useref from 'gulp-useref';
const settings = (() => {
const settingsPath = './settings.json';
if (existsSync(settingsPath)) {
return JSON.parse(readFileSync(settingsPath, 'utf-8'));
}
return {};
}());
const DEFAULT_SETTINGS = {
googleApiKey: 'You need to specify Google API Key'
};
/* Default task */
gulp.task('default', function() {
gulp.start('build');
});
/* Removing whole ./dist directory */
gulp.task('clean', del.bind(null, './dist'));
/* Building HTML */
gulp.task('html', function() {
const assets = useref.assets({searchPath: '{src}'});
return gulp.src('./src/*.html')
.pipe(compileHandlebars(Object.assign({}, DEFAULT_SETTINGS, settings)))
.pipe(assets)
.pipe(gif('*.css', csso()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gif('*.html', minifyHtml({conditionals: true, loose: true})))
.pipe(gulp.dest('./dist'));
});
/* End of building HTML */
/* Building JS */
gulp.task('js', function() {
return gulp.src('./src/js/main.js')
.pipe(plumber())
.pipe(tap(function(file) {
const dom = domain.create();
dom.on('error', function(err) {
gutil.log(
gutil.colors.red('Browserify compile error:'),
err.message, '\n\t',
gutil.colors.cyan('in file'), file.path
);
gutil.beep();
});
dom.run(function() {
file.contents = browserify({
entries: [file.path],
debug: true,
standalone: 'Planting',
transform: [hbsfy, babelify],
}).bundle();
});
}))
.pipe(gulp.dest('./dist/js/'));
});
/* End of building JS */
/* Building CSS */
gulp.task('css:main', function() {
return gulp.src('./src/styles/**/*.scss')
.pipe(sass())
.pipe(autoprefixer({browsers: ['last 1 version']}))
.pipe(gulp.dest('./dist/styles'));
});
gulp.task('css:vendor', function() {
return gulp.src([
'./node_modules/jquery-ui/themes/base/jquery-ui.css',
'./node_modules/jquery-ui/themes/base/jquery.ui.dialog.css',
])
.pipe(concat('vendor.css'))
.pipe(gulp.dest('./dist/styles/'));
});
gulp.task('css', ['css:vendor', 'css:main']);
/* End of building CSS */
/* Building fonts */
gulp.task('fonts', function() {
return gulp.src('./src/fonts/**/*')
.pipe(filter('**/*.{eot,svg,ttf,woff}'))
.pipe(flatten())
.pipe(gulp.dest('./dist/fonts'));
});
/* End of building fonts */
/* Building extras */
gulp.task('extras', function() {
gulp.src('./src/objects/**/*').pipe(gulp.dest('./dist/objects'));
return gulp.src([
'./src/*.*',
'!./src/*.html',
], {
dot: true,
}).pipe(gulp.dest('./dist'));
});
/* End of building extras */
/* Building whole library */
gulp.task('build', function(cb) {
return sequence(
'clean',
['html', 'js', 'css', 'fonts', 'extras'],
'buildsize',
cb
);
});
/* End of building whole library */
/* Showing size of build */
gulp.task('buildsize', function() {
return gulp.src('./dist/**/*').pipe(size({title: 'build', gzip: true}));
});
/* Serving assets using development webserver */
gulp.task('connect', function() {
const app = connect()
.use(connectLivereload({port: 35729}))
.use(serveStatic('./dist'))
.use(serveStatic('./src'))
.use('/node_modules', serveStatic('./node_modules'))
.use(serveIndex('./src'));
http.createServer(app)
.listen(9000)
.on('listening', function() {
console.log('Started connect web server on http://localhost:9000');
});
});
/* End of serving assets using development webserver */
/* Watching for changes in sources */
gulp.task('watch', function() {
livereload.listen();
return gulp.watch('./src/**/*', ['reserve']);
});
/* End of watching for changes in sources */
/* Opening browser pointing to library */
gulp.task('openbrowser:planting', function() {
opn('http://localhost:9000/planting.html');
});
gulp.task('openbrowser:campaign', function() {
opn('http://localhost:9000/campaign.html');
});
gulp.task('openbrowser:viewer', function() {
opn('http://localhost:9000/viewer.html');
});
/* Refreshing opened browser with new code */
gulp.task('refreshbrowser', function() { return livereload.changed(); });
/* Continous serving new version of library with watching for changes */
gulp.task('serve:planting', function(cb) {
return sequence('build', 'connect', 'watch', 'openbrowser:planting', cb);
});
gulp.task('serve:campaign', function(cb) {
return sequence('build', 'connect', 'watch', 'openbrowser:campaign', cb);
});
gulp.task('serve:viewer', function(cb) {
return sequence('build', 'connect', 'watch', 'openbrowser:viewer', cb);
});
gulp.task('serve', function(cb) {
return sequence('build', 'connect', 'watch', ['openbrowser:planting',
'openbrowser:campaign', 'openbrowser:viewer'], cb);
});
gulp.task('reserve', function(cb) {
return sequence('build', 'refreshbrowser', cb);
});
/* Running ESLint on source */
gulp.task('lint', function() {
return gulp.src(['./src/js/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/* End of running ESLint on source */
/* Running unittests with coverage */
gulp.task('test:setup', function() {
return gulp.src(['src/**/*.js'])
.pipe(istanbul({instrumenter: Instrumenter, includeUntested: true}))
.pipe(istanbul.hookRequire());
});
gulp.task('test', ['test:setup'], function() {
return gulp.src(['test/*.js', '!test/env'])
.pipe(mocha({compilers: {js: babel}}))
.pipe(istanbul.writeReports());
});
/* End of running unittests with coverage */