-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
277 lines (245 loc) · 8 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict'; // eslint-disable-line strict
// Import external modules
const babel = require('gulp-babel');
const browserSync = require('browser-sync');
const cache = require('gulp-cached');
const del = require('del');
const eslint = require('gulp-eslint');
const fs = require('fs');
const gulp = require('gulp');
const gutil = require('gulp-util');
const imagemin = require('gulp-imagemin');
const minifyCss = require('gulp-minify-css');
const nodemon = require('nodemon');
const path = require('path');
const plumber = require('gulp-plumber');
const prefix = require('gulp-autoprefixer');
const pretty = require('prettysize');
const reload = browserSync.reload;
const rev = require('gulp-rev');
const runSequence = require('run-sequence');
const sass = require('gulp-sass');
const size = require('gulp-size');
const sourcemaps = require('gulp-sourcemaps');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
// Import internal modules
const config = require('./config');
// Create an instance of the client compiler for caching
const webpackDevConfig = require('./webpack.dev.client');
const webpackDevCompiler = webpack(webpackDevConfig);
// Boolean for whether we're running webpack-dev-server
let isRunningDevServer = false;
/**
* Compile our images
*/
gulp.task('build:images', () =>
gulp.src(config.files.images.src)
.pipe(imagemin())
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.images.out}`))
.pipe(reload({ stream: true }))
);
/**
* Compile our CSS files
*/
gulp.task('build:css', () =>
gulp.src(config.files.css.entry)
.pipe(plumber())
.pipe(sass(config.build.sass))
.pipe(prefix(config.build.autoprefixer))
.pipe(size({ title: 'CSS' }))
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.css.out}`))
.pipe(reload({ stream: true }))
);
/**
* Compile our CSS files for production. This minifies our CSS as well.
*/
gulp.task('build:css:prod', () =>
gulp.src(config.files.css.entry)
.pipe(sass(config.build.sass))
.pipe(prefix(config.build.autoprefixer))
.pipe(minifyCss())
.pipe(size({ title: 'CSS' }))
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.css.out}`))
);
/**
* Lint all our JS files.
*/
gulp.task('build:lint', () =>
gulp.src([config.files.client.src, config.files.tests.src])
.pipe(cache('build:lint'))
.pipe(eslint())
.pipe(eslint.format())
);
/**
* Lint all our JS files, and fail on error. Useful on CI machines and build scripts.
*/
gulp.task('build:lint:prod', () =>
gulp.src([config.files.client.src, config.files.tests.src])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError())
);
/**
* Compile our server files.
*/
gulp.task('build:server', () =>
gulp.src(config.files.server.src)
.pipe(cache('src:server'))
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(babel(config.build.babel.server.dev))
.pipe(sourcemaps.write('.'))
.pipe(size({ title: 'Server JS' }))
.pipe(gulp.dest(config.files.server.out))
);
/**
* Compile our server files for production.
*/
gulp.task('build:server:prod', () =>
gulp.src(config.files.server.src)
.pipe(cache('src:server'))
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(babel(config.build.babel.server.prod))
.pipe(sourcemaps.write('.'))
.pipe(size({ title: 'Server JS' }))
.pipe(gulp.dest(config.files.server.out))
);
/**
* Compile our JS files for development and launch webpack-dev-server.
*/
gulp.task('build:client', callback => {
// Run webpack
webpackDevCompiler.run(err => {
if (err) throw new gutil.PluginError('build:client', err);
// Emulate gulp-size and ignore errors
try {
const outputConfig = webpackDevConfig.output;
const jsFilePath = path.join(outputConfig.path, outputConfig.filename);
gutil.log(`${gutil.colors.cyan('Client JS')} ${gutil.colors.green('all files ')}` +
`${gutil.colors.magenta(pretty(fs.statSync(jsFilePath).size))}`);
} catch (e) {
// Continue regardless of error
}
// Set boolean to true if we're not running the server.
if (!isRunningDevServer) {
isRunningDevServer = true;
// Start the dev server. We have to make sure we send a new instance of the webpack compiler.
const devServer = new WebpackDevServer(webpack(webpackDevConfig), webpackDevConfig.devServer);
devServer.listen(config.ports.webpack, 'localhost', serverErr => {
if (serverErr) throw new gutil.PluginError('webpack-dev-server', serverErr);
});
}
// Call callback when done
callback();
});
});
/**
* Compile our JS files for production.
*/
gulp.task('build:client:prod', callback => {
const webpackProdConfig = require('./webpack.client');
const webpackProdCompiler = webpack(webpackProdConfig);
// Run webpack
webpackProdCompiler.run(err => {
if (err) throw new gutil.PluginError('build:client:prod', err);
// Emulate gulp-size
const outputConfig = webpackProdConfig.output;
const jsFilePath = path.join(outputConfig.path, outputConfig.filename);
gutil.log(`'${gutil.colors.cyan('Client Prod JS')}' ${gutil.colors.green('all files ')}` +
`${gutil.colors.magenta(pretty(fs.statSync(jsFilePath).size))}`);
callback();
});
});
/**
* Duplicate our CSS and JS files with hashes append to their names, so we can enable long term
* caching.
*/
gulp.task('build:cache', () => {
gulp.src(`${config.files.staticAssets}${config.files.css.out}/*.css`)
.pipe(rev())
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.css.out}`))
.pipe(rev.manifest())
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.css.out}`));
gulp.src(`${config.files.staticAssets}${config.files.client.out}/*.js`)
.pipe(rev())
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.client.out}`))
.pipe(rev.manifest())
.pipe(gulp.dest(`${config.files.staticAssets}${config.files.client.out}`));
});
/**
* Clean out build folder so we are sure we're not building from some cache.
*/
gulp.task('clean', callback => {
del(['build']).then(() => {
callback();
});
});
/**
* Task to compile our files for production.
*/
gulp.task('compile', callback => {
runSequence('clean', 'build:lint:prod', [
'build:images',
'build:css:prod',
'build:client:prod',
'build:server:prod',
], 'build:cache', callback);
});
/**
* Task to compile our files for production, ignoring linting.
*/
gulp.task('compile:nolint', callback => {
runSequence('clean', [
'build:images',
'build:css:prod',
'build:client:prod',
'build:server:prod',
], 'build:cache', callback);
});
/**
* Watch the necessary directories and launch BrowserSync.
*/
gulp.task('watch', ['clean'], callback => {
runSequence(
'build:lint', [
'build:images',
'build:css',
'build:client',
'build:server',
], () => {
// Watch files
gulp.watch(config.files.client.src, ['build:client']);
gulp.watch(config.files.server.src, ['build:server']);
gulp.watch(config.files.client.src, ['build:lint']);
gulp.watch(config.files.css.src, ['build:css']);
gulp.watch(config.files.images.src, ['build:images']);
// Launch Nodemon
nodemon({
env: { NODE_ENV: 'development' },
watch: [config.files.server.out],
ignore: [config.files.staticAssets],
});
// Boolean to check if BrowserSync has started.
let isBrowserSyncStarted = false;
// Perform action right when nodemon starts
nodemon.on('start', () => {
// Only perform action when boolean is false
if (!isBrowserSyncStarted) {
isBrowserSyncStarted = true;
// Set a timeout of 500 ms so that the server has time to start
setTimeout(() => {
// Launch BrowserSync
browserSync({
proxy: `localhost:${config.ports.express}`,
open: false,
});
// Call callback function to end gulp task
callback();
}, 500);
}
});
}
);
});