-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
156 lines (107 loc) · 3.23 KB
/
Copy pathgulpfile.babel.js
File metadata and controls
156 lines (107 loc) · 3.23 KB
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
import gulp from 'gulp';
import webpack from 'webpack';
import eslint from 'eslint/lib/cli';
import run from 'run-sequence';
import gutil from 'gulp-util';
import changed from 'gulp-changed';
import gulpif from 'gulp-if';
import imagemin from 'gulp-imagemin';
import { exec } from 'child_process';
import del from 'del';
import gulpConfig from './config/gulp.config.js';
const prodBuildTask = 'build';
const startDevTask = 'start:dev';
const startProdTask = 'start:prod';
const isDevBuild = process.argv.indexOf(startDevTask) !== -1;
const startTask = isDevBuild ? startDevTask : prodBuildTask;
const config = gulpConfig(isDevBuild);
const webpackConfig = config.webpack.config;
const webpackCallback = config.webpack.cb;
// Run tasks
gulp.task('default', [startTask]);
gulp.task(prodBuildTask, done => {
run(['clean', 'lint'], ['bundle', 'images', 'copy'], done);
});
gulp.task(startDevTask, done => {
run(['clean', 'lint'], ['bundle', 'images', 'copy'], ['server', 'watch'], done);
});
gulp.task(startProdTask, done => {
run(['clean', 'lint'], ['bundle', 'images', 'copy'], ['server', 'watch'], done);
});
// Node servers starter
const startServer = (serverPath, done) => {
const prodFlag = !isDevBuild ? 'NODE_ENV=production' : '';
const cmd = isDevBuild && serverPath !== config.devServer ? 'nodemon --watch config --watch app' : 'node';
const server = exec(`NODE_PATH=. ${prodFlag} ${cmd} ${serverPath}`);
server.stdout.on('data', data => {
if (done && data === 'Webpack: Done!') {
done();
} else {
gutil.log(data.trim());
}
});
server.stderr.on('data', data => {
gutil.log(gutil.colors.red(data.trim()));
gutil.beep();
});
};
// Build bundles
gulp.task('bundle', done => {
if (isDevBuild) {
exec('mkdir -p public/assets');
startServer(config.devServer, done);
} else {
webpack(webpackConfig).run((err, stats) => {
webpackCallback('build', err, stats);
done();
});
}
});
// Start express servers
gulp.task('server', done => {
const servers = config.server.paths;
let queue = servers.length;
servers.forEach(server => {
startServer(server);
if (--queue === 0) done();
});
});
// Optimize images
gulp.task('images', done => {
gulp.src(config.images.src)
.pipe(gulpif(isDevBuild, changed(config.images.dest)))
.pipe(imagemin(config.images.imagemin))
.pipe(gulp.dest(config.images.dest))
.on('end', done);
});
// Copy files to `public`
gulp.task('copy', done => {
const files = config.copy.files;
let queue = files.length;
files.forEach(file => {
const from = config.copy.from + file[0];
const to = config.copy.to + (file[1] || file[0]);
exec(`cp -R ${from} ${to}`, err => {
if (err) {
gutil.log(gutil.colors.red(err));
gutil.beep();
}
if (--queue === 0) done();
});
});
});
// Watch statics
gulp.task('watch', () => {
const watchItems = config.watch.files.map(file => config.watch.root + file);
gulp.watch(watchItems, ['copy']);
});
// Lint scripts
gulp.task('lint', done => {
eslint.execute('--ext .js,.jsx ./');
done();
});
// Clean up before build
gulp.task('clean', done => {
const items = config.clean.map(dir => dir + '/**/*');
del(items, done);
});