-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
151 lines (134 loc) · 3.35 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
const { src, dest, watch, series } = require('gulp');
const del = require('del');
const browserSync = require('browser-sync').create();
const $ = require('gulp-load-plugins')();
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const webpackDevMiddleware = require('webpack-dev-middleware');
const puppy = require('@upstatement/puppy');
const stream = require('stream');
const util = require('util');
const isProduction = process.env.NODE_ENV === 'production';
const bundler = webpack(webpackConfig);
// eslint-disable-next-line
console.log('Build Mode: %s', isProduction ? 'Production' : 'Development');
/**
* Compile HTML
*
* - Extract site/page meta with Puppy
* - Compile Twig templates
* - Minify HTML for optimized builds
*/
const html = async function() {
const pipeline = util.promisify(stream.pipeline);
const pages = await puppy({
publicPath: '/',
pages: 'src/pages/**/*',
data: 'src/data/**/*',
});
const twig = $.twig({
namespaces: { templates: 'src/templates' },
useFileContents: true,
});
const minify = $.if(
isProduction,
$.if(
'*.html',
$.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
}),
),
);
const dist = dest('dist');
return pipeline(pages, twig, minify, dist);
};
/**
* Copy public assets to build directory
*/
const publicFiles = function() {
return src('public/**/*').pipe(dest('dist'));
};
/**
* Bundle scripts and styles with Webpack.
*/
const bundle = function() {
return new Promise((resolve, reject) => {
bundler.run((err, stats) => {
if (err) {
reject(err);
} else {
// eslint-disable-next-line
console.log(
stats.toString({
chunks: false,
colors: true,
}),
);
resolve();
}
});
});
};
/**
* Serve for local development.
*/
const serve = function() {
browserSync.init({
notify: false,
reloadDelay: 500,
open: false,
server: {
baseDir: 'dist',
},
middleware: [
webpackDevMiddleware(bundler, {
stats: 'minimal',
writeToDisk: true,
}),
],
plugins: ['bs-fullscreen-message'],
});
function reload(done) {
browserSync.reload();
done();
}
// Reload browser after Webpack compilation.
bundler.hooks.done.tap('serve', stats => {
if (stats.hasErrors() || stats.hasWarnings()) {
browserSync.sockets.emit('fullscreen:message', {
title: 'Webpack Error',
body: stats.toString(),
timeout: 100000,
});
return;
}
browserSync.reload();
});
// Recompile templates if any content changes.
watch(['src/pages/**/*', 'src/templates/**/*', 'src/data/**/*'], series(html, reload));
// Trigger static task when files in the public directory are changed.
watch('public/**/*', series(publicFiles, reload));
};
/**
* Clean build directory
*/
const clean = function() {
return del(['dist']);
};
/**
* Build task.
*/
const build = series(clean, publicFiles, bundle, html);
module.exports = {
clean,
build,
serve,
default: series(build, serve),
};