forked from code-troopers/code-troopers.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
executable file
·106 lines (93 loc) · 2.81 KB
/
gulpfile.babel.js
File metadata and controls
executable file
·106 lines (93 loc) · 2.81 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
import 'process';
import path from 'path';
import BrowserSync from 'browser-sync';
import cp from 'child_process';
import gulp from 'gulp';
import gutil from 'gulp-util';
import del from 'del';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from './webpack.conf';
import webpackDevConfig from './webpack.conf.dev';
const hugoBin = 'hugo';
const defaultArgs = ['-d', '.tmp', '--config', 'config.yml', '-v', '-t', 'code-troopers'];
const browserSync = BrowserSync.create();
gulp.task('build', ['js']);
gulp.task('serve-prod', ['build'], () => {
browserSync.init({
notify: false,
open: false,
server: {
baseDir: './dist'
}
});
});
gulp.task('serve-dev', ['hugo-dev'], () => {
const compiler = webpack(webpackDevConfig());
const webpackMiddleware = webpackDevMiddleware(compiler, {
publicPath: '/',
reporter: webpackReporter
});
browserSync.init({
notify: false,
open: false,
server: {
baseDir: './dist',
middleware: [
webpackMiddleware,
webpackHotMiddleware(compiler)
]
}
});
process.env.SKIP_CLEAN = true
gulp.watch('./site/**/*', ['hugo-dev', () => webpackMiddleware.invalidate()]);
});
gulp.task('hugo-dist', ['clean'], (cb) => buildSite(cb, ['-d', '.tmp']));
gulp.task('hugo-dev', ['clean'], (cb) => buildSite(cb, ['-d', 'dist', '--buildDrafts', '--buildFuture'], { WEBPACK_HOT: true }));
gulp.task('clean', () => {
if (process.env.SKIP_CLEAN){
return;
}
return del(['dist/**', '.tmp/**', '!dist', '!.tmp']);
});
gulp.task('js', ['hugo-dist'], (cb) => {
webpack(webpackConfig(), (err, stats) => {
if (err) throw new gutil.PluginError('webpack', err);
gutil.log('[webpack]', stats.toString({
colors: true,
progress: true
}));
cb();
});
});
function buildSite(cb, options, env = null) {
const args = options ? defaultArgs.concat(options) : defaultArgs;
var nodeEnv = env && Object.assign({}, process.env, env) || process.env;
nodeEnv['PATH'] = `${path.join(__dirname, 'scripts')}:${nodeEnv['PATH']}`
return cp.spawn(hugoBin, args, { stdio: 'inherit', env: nodeEnv }).on('close', (code) => {
if (code === 0) {
browserSync.reload();
cb();
} else {
browserSync.notify('Hugo build failed :(');
cb('Hugo build failed');
}
});
}
function webpackReporter(evt) {
const statsOptions = {
colors: true,
progress: true
};
if (evt.state) {
if (evt.stats.hasErrors()) {
gutil.log('[webpack]', 'Failed to compile.');
} else if (evt.stats.hasWarnings()) {
gutil.log('[webpack]', 'Compiled with warnings.');
}
gutil.log('[webpack]', evt.stats.toString(statsOptions));
} else {
gutil.log('[webpack]', 'Compiling...');
}
}