-
Notifications
You must be signed in to change notification settings - Fork 48
/
gulpfile.babel.js
171 lines (150 loc) · 4.35 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
'use strict';
import path from 'path';
import cp from 'child_process';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import del from 'del';
import mkdirp from 'mkdirp';
import runSequence from 'run-sequence';
import webpack from 'webpack';
import minimist from 'minimist';
import eslint from 'gulp-eslint';
const $ = gulpLoadPlugins();
const argv = minimist(process.argv.slice(2));
const src = Object.create(null);
let watch = false;
let browserSync;
// The default task
gulp.task('default', ['sync']);
// Clean output directory
gulp.task('clean', cb => {
del(['.tmp', 'build/*', '!build/.git'], {dot: true}, () => {
mkdirp('build/master/static', cb);
});
});
gulp.task('lint', function () {
return gulp.src(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.js'])
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
// Static files
gulp.task('assets', () => {
src.assets = 'src/public/**';
return gulp.src(src.assets)
.pipe($.changed('build/master/static'))
.pipe(gulp.dest('build/master/static'))
.pipe($.size({title: 'assets'}));
});
// Resource files
gulp.task('resources', () => {
src.resources = [
'package.json',
'src/templates/**'
];
return gulp.src(src.resources)
.pipe($.changed('build/master/static'))
.pipe(gulp.dest('build/master/static'))
.pipe($.size({title: 'resources'}));
});
// Bundle
gulp.task('bundle', cb => {
const config = require('./webpack.config.js');
const bundler = webpack(config);
const verbose = !!argv.verbose;
let bundlerRunCount = 0;
function bundle(err, stats) {
if (err) {
throw new $.util.PluginError('webpack', err);
}
console.log(stats.toString({
colors: $.util.colors.supportsColor,
hash: verbose,
version: verbose,
timings: verbose,
chunks: verbose,
chunkModules: verbose,
cached: verbose,
cachedAssets: verbose
}));
if (++bundlerRunCount === (watch ? config.length : 1)) {
return cb();
}
}
if (watch) {
bundler.watch(200, bundle);
} else {
bundler.run(bundle);
}
});
// Build the app from source code
gulp.task('build', cb => {
runSequence(['assets', 'resources'], ['bundle'], cb);
});
// Build and start watching for modifications
gulp.task('build:watch', cb => {
watch = true;
runSequence('build', () => {
gulp.watch(src.assets, ['assets']);
gulp.watch(src.resources, ['resources']);
cb();
});
});
// Launch a Node.js/Express server
gulp.task('serve', ['build:watch'], cb => {
src.server = [
'build/server.js',
'build/master/static/**/*'
];
let started = false;
let server = (function startup() {
const child = cp.fork('build/server.js', {
env: Object.assign({NODE_ENV: 'development'}, process.env)
});
child.once('message', message => {
if (message.match(/^online$/)) {
if (browserSync) {
browserSync.reload();
}
if (!started) {
started = true;
gulp.watch(src.server, function() {
$.util.log('Restarting development server.');
server.kill('SIGTERM');
server = startup();
});
cb();
}
}
});
return child;
})();
process.on('exit', () => server.kill('SIGTERM'));
});
// Launch BrowserSync development server
gulp.task('sync', ['serve'], cb => {
browserSync = require('browser-sync');
browserSync({
logPrefix: 'RSK',
notify: false,
// Run as an https by setting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
https: false,
// Informs browser-sync to proxy our Express app which would run
// at the following location
proxy: 'localhost:5000'
}, cb);
process.on('exit', () => browserSync.exit());
gulp.watch(['build/**/*.*'].concat(
src.server.map(file => '!' + file)
), file => {
browserSync.reload(path.relative(__dirname, file.path));
});
});