-
Notifications
You must be signed in to change notification settings - Fork 20
/
gulpfile.babel.js
102 lines (93 loc) · 2.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
import gulp from 'gulp';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import nodemon from 'nodemon';
import path from 'path';
import schema from 'gulp-graphql';
import fs from 'fs';
import configs from './webpack.config';
const [ frontendConfig, backendConfig ] = configs;
let compiler;
// trigger a manual recompilation of webpack(frontendConfig);
function recompile() {
if (!compiler)
return null;
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err)
reject(err);
console.log('[webpackDevServer]: recompiled');
resolve();
});
});
}
// run the webpack dev server
// must generate the schema.json first as compiler relies on it for babel-relay-plugin
gulp.task('webpack', ['generate-schema'], () => {
compiler = webpack(frontendConfig);
let server = new WebpackDevServer(compiler, {
contentBase: path.join(__dirname, 'build', 'public'),
hot: true,
noInfo: true,
stats: { colors: true },
historyApiFallback: true,
proxy: {
'/graphql': 'http://localhost:8080'
}
});
server.listen(3000, 'localhost', (err, result) => {
if (err)
return console.error(err);
console.log('[webpackDevServer]: listening on localhost:3000');
});
});
// restart the backend server whenever a required file from backend is updated
gulp.task('backend-watch', () => {
return new Promise((resolve, reject) => {
let compiled = false;
webpack(backendConfig).watch(100, (err, stats) => {
if (err)
return reject(err);
// trigger task completion after first compile
if (!compiled) {
compiled = true;
resolve();
} else {
nodemon.restart();
}
});
});
});
// Regenerate the graphql schema and recompile the frontend code that relies on schema.json
gulp.task('generate-schema', () => {
return gulp.src('./src/server/data/schema.js')
.pipe(schema({
json: true,
graphql: true
}))
.on('error', err => {
console.log(err.message);
})
.pipe(gulp.dest('./src/server/data'))
.on('end', recompile);
});
// recompile the schema whenever .js files in data are updated
gulp.task('watch-schema', () => {
gulp.watch(path.join(__dirname, './src/server/data', '**/*.js'), ['generate-schema']);
});
gulp.task('server', ['backend-watch', 'watch-schema'], () => {
nodemon({
execMap: {
js: 'node'
},
script: path.join(__dirname, 'build', 'server.js'),
// do not watch any directory/files to refresh
// all refreshes should be manual
watch: ['foo/'],
ext: 'noop',
ignore: ['*']
}).on('restart', () => {
console.log('[nodemon]: restart');
});
});
gulp.task('default', ['webpack', 'server']);