-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
204 lines (181 loc) · 5.23 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
const { src, dest, watch, series } = require('gulp');
const del = require('del');
const groupBy = require('lodash.groupby');
const browserSync = require('browser-sync').create();
const $ = require('gulp-load-plugins')();
const log = require('gulplog');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const webpackDevMiddleware = require('webpack-dev-middleware');
const puppy = require('@upstatement/puppy');
const gulpScreenshot = require('@upstatement/puppy/lib/gulp/screenshots');
const stream = require('stream');
const util = require('util');
const isProduction = process.env.NODE_ENV === 'production';
const bundler = webpack(webpackConfig);
const pipeline = util.promisify(stream.pipeline);
log.info('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 pages = await puppy({
publicPath: '/',
pages: 'src/pages/**/*',
data: 'src/data/**/*',
screenshots: 'dist/thumbnails/**/*',
});
const twig = $.twig({
namespaces: { templates: 'src/templates' },
useFileContents: true,
filters: [
{
name: 'group',
func(collection, args) {
return groupBy(collection, ...args);
},
},
],
});
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 {
log.info(
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));
};
/**
* Generate page screenshots.
*/
const capture = async function() {
const pages = await puppy({ pages: 'src/pages/**/*' });
// Helper function to determine if the page `thumbnail` property is set to `auto`.
const hasAutoConfig = page =>
typeof page.thumbnail === 'string' && page.thumbnail.match(/auto/i) !== null;
// Helper function to determine if the page `thumbnail` property is set to a config object.
const hasPageCaptureOptions = page =>
page.thumbnail !== null && typeof page.thumbnail === 'object';
const screenshot = gulpScreenshot({
// Global options for `Page.setViewport()`
// https://pptr.dev/#?product=Puppeteer&version=v2.1.1&show=api-pagesetviewportviewport
viewport: {
width: 1500,
height: 1000,
deviceScaleFactor: 1,
},
// Global options for `Page.goto()
// https://pptr.dev/#?product=Puppeteer&version=v2.1.1&show=api-pagegotourl-options
goto: {
waitUntil: 'networkidle2',
},
// Global options for `Page.screenshot()
// https://pptr.dev/#?product=Puppeteer&version=v2.1.1&show=api-pagescreenshotoptions
screenshot: {
type: 'png',
},
// Extract page-specific screenshot options from front-matter data.
pageCaptureOptions: page => (hasPageCaptureOptions(page) ? page.thumbnail : null),
// Determine whether or not a given page should be excluded from automated screenshots.
exclude: page => !page.thumbnail || (!hasAutoConfig(page) && !hasPageCaptureOptions(page)),
});
return pipeline(pages, screenshot, dest('dist/thumbnails'));
};
/**
* Clean build directory
*/
const clean = function() {
return del(['dist']);
};
/**
* Exported tasks.
*/
const screenshot = series(capture, publicFiles, html);
const build = series(clean, publicFiles, bundle, html, screenshot);
module.exports = {
clean,
build,
serve,
default: series(build, serve),
};