-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
105 lines (91 loc) · 2.82 KB
/
gulpfile.ts
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
import * as del from 'del';
import * as fs from 'fs';
import * as gulp from 'gulp';
import * as nodemon from 'gulp-nodemon';
import * as ts from 'gulp-typescript';
import * as gutil from 'gulp-util';
import { promisify } from 'util';
import * as webpack from 'webpack';
import * as WebpackDevServer from 'webpack-dev-server';
import { API_PORT, CLIENT_DEV_PORT, ANALYZER_PORT } from './src/server/env';
import { createDirname } from './src/util/dirname';
import { gql2ts } from './src/util/graphql/generate-tsd';
import { makeConfig } from './webpack.config';
const gulpLog = (prefix: string, msg: string) =>
msg.split('\n').forEach(line => gutil.log(prefix, line));
const dirname = createDirname(__dirname);
const TS_BUILD_PATHS = fs
.readdirSync(dirname('./src'))
.filter(name => name !== 'client')
.map(name => dirname('./build', name));
const tsProject = ts.createProject(dirname('./src/tsconfig.json'), {
outDir: dirname('./build'),
});
gulp.task('client-build', () => {
return del(dirname('./build/client'))
.then(() => promisify(webpack)(makeConfig()))
.then((stats: webpack.Stats) => {
gulpLog('[webpack]', stats.toString('normal'));
});
});
gulp.task('client-watch', () => {
const compiler = webpack(
makeConfig({
dev: true,
devServerPort: CLIENT_DEV_PORT,
analyzerPort: ANALYZER_PORT,
}),
);
new WebpackDevServer(compiler, {
contentBase: dirname('/build'),
publicPath: '/client',
stats: {
modules: true,
colors: true,
},
hot: true,
proxy: {
'/api': {
target: `http://localhost:${API_PORT}`,
pathRewrite: { '^/api': '' },
onProxyReq: (proxyReq: any) =>
proxyReq.setHeader('x-proxy-path', '/api'),
},
'*': {
target: `http://localhost:${CLIENT_DEV_PORT}/client`,
ignorePath: true,
},
},
}).listen(CLIENT_DEV_PORT, 'localhost', (error: any) => {
if (error) {
throw new gutil.PluginError('[webpack-dev-server]', error);
}
gulpLog('[webpack-dev-server]', `started at ${CLIENT_DEV_PORT} port`);
});
});
gulp.task('server-build', ['schemas-build'], () =>
Promise.all(TS_BUILD_PATHS.map(s => del(s))).then(() =>
Promise.all([
tsProject.src().pipe(tsProject()).js.pipe(gulp.dest(dirname('./build'))),
gulp.src('./src/**/*.gql').pipe(gulp.dest(dirname('./build'))),
]),
),
);
gulp.task('server-watch', ['schemas-build'], () =>
nodemon({
script: dirname('./src/server'),
ext: 'ts',
watch: [dirname('./src/**/*.ts')],
ignore: [dirname('./src/client/**/*.ts')],
exec: 'ts-node',
}),
);
gulp.task('schemas-build', () =>
gulp.src('./src/**/*.gql').pipe(gql2ts()).pipe(gulp.dest(dirname('./src'))),
);
gulp.task('schemas-watch', () => {
gulp.watch(dirname('./src/**/*.gql'), ['schemas-build']);
});
gulp.task('watch', ['client-watch', 'server-watch', 'schemas-watch']);
gulp.task('build', ['client-build', 'server-build']);
gulp.task('default', ['watch']);