-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGulpfile.js
153 lines (139 loc) · 4.15 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
/**
* Copyright (c) Benedikt Meurer @bmeurer.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
"use strict";
const autoprefixer = require("autoprefixer");
const args = require("minimist")(process.argv);
const cssnano = require("cssnano");
const del = require("del");
const gulp = require("gulp");
const htmlmin = require("gulp-htmlmin");
const jsonminify = require("gulp-jsonminify");
const merge = require("merge-stream");
const noop = require("gulp-noop");
const postcss = require("gulp-postcss");
const sass = require("gulp-sass");
const shell = require("gulp-shell");
const terser = require("gulp-terser");
const util = require("util");
const workbox = require("workbox-build");
const destDir = `${__dirname}/dist`;
const srcDir = `${__dirname}/src`;
/** Builds (and optionally minifies) the HTML files */
function buildHTML() {
return util
.promisify(shell.task("eleventy", { quiet: true }))()
.then(() => {
// Minify the resulting HTML for production builds.
if (args.production) {
return gulp
.src(`${destDir}/**/*.html`)
.pipe(
htmlmin({
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: false,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
preserveLineBreaks: false,
preventAttributesEscaping: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false, // TODO(bmeurer): The <label> for the sidebar is empty.
removeRedundantAttributes: true,
removeTagWhitespace: false,
sortAttributes: true,
sortClassName: true
})
)
.pipe(gulp.dest(destDir));
}
});
}
/** Builds (and optionally minifies) the JSON files */
function buildJSON() {
return gulp
.src(`${srcDir}/manifest.json`)
.pipe(args.production ? jsonminify() : noop())
.pipe(gulp.dest(destDir));
}
/** Builds (and optionally minifies) the sw.js */
function buildServiceWorker() {
return workbox
.generateSW({
skipWaiting: true,
cacheId: "benediktmeurer.de",
offlineGoogleAnalytics: true,
globDirectory: destDir,
globPatterns: [
"css/main.css",
"favicon.ico",
"fonts/*.woff2",
"images/icons.svg",
"index.html",
"js/main.js"
],
swDest: `${destDir}/sw.js`,
templatedUrls: {
"/": ["index.html"]
}
})
.then(({ warnings }) => {
// Log any warnings from workbox-build.
for (const warning of warnings) {
console.warn(warning);
}
// Minify the resulting sw.js for production builds.
if (args.production) {
return gulp
.src(`${destDir}/sw.js`)
.pipe(terser())
.pipe(gulp.dest(`${destDir}`));
}
});
}
/** Builds (and optionally minifies) the JavaScript files */
function buildScripts() {
return gulp
.src(`${srcDir}/js/*.js`)
.pipe(args.production ? terser() : noop())
.pipe(gulp.dest(`${destDir}/js`));
}
/** Builds (and optionally minifies) the CSS files */
function buildStyles() {
return merge(
gulp.src(`${srcDir}/css/*.css`),
gulp.src(`${srcDir}/css/*.scss`).pipe(sass().on("error", sass.logError))
)
.pipe(postcss([autoprefixer()].concat(args.production ? [cssnano()] : [])))
.pipe(gulp.dest(`${destDir}/css`));
}
/** Copy resources */
function copyResources() {
return gulp
.src(`${srcDir}/{{files,fonts,images}/**/*.*,favicon.ico}`)
.pipe(gulp.dest(destDir));
}
/** Cleans the generated artifacts */
function clean() {
return del([destDir]);
}
const build = gulp.parallel(
buildJSON,
copyResources,
gulp.series(
gulp.parallel(buildHTML, buildScripts, buildStyles),
buildServiceWorker
)
);
exports.build = build;
exports.clean = clean;
exports.default = build;