-
Notifications
You must be signed in to change notification settings - Fork 182
/
gulpfile.mjs
159 lines (138 loc) · 3.8 KB
/
gulpfile.mjs
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
import gulp from 'gulp';
import browserSync from 'browser-sync';
import { deleteSync } from 'del';
import gulpAutoprefixer from 'gulp-autoprefixer';
import gulpCSSBase64 from 'gulp-css-base64';
import gulpCSSO from 'gulp-csso';
import gulpESLint from 'gulp-eslint';
import gulpHTMLMin from 'gulp-htmlmin';
import gulpRename from 'gulp-rename';
import gulpReplace from 'gulp-replace';
import gulpRework from 'gulp-rework';
import gulpSmoosher from 'gulp-smoosher';
import gulpUnCSS from 'gulp-uncss';
import gulpUseRef from 'gulp-useref';
import reworkNpm from 'rework-npm';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const pkg = require('./package.json');
const dirs = pkg['h5bp-configs'].directories;
const reload = browserSync.reload;
const browserSyncOptions = {
logPrefix: 'H5BP',
notify: false,
port: 8080
};
async function cleanBefore() {
deleteSync([dirs.dist]);
}
async function cleanAfter() {
deleteSync([
dirs.dist + '/{css,css/**}',
dirs.dist + '/{img,/img/**}',
dirs.dist + '/{js,/js/**}'
]);
}
function copyHTML() {
console.log('copyHTML');
return gulp.src(dirs.src + '/index.html')
.pipe(gulpReplace(/{{h5bp-version}}/g, pkg.version))
.pipe(gulpUseRef())
.pipe(gulp.dest('docs'));
}
function copyCSS() {
console.log('copyCSS');
return gulp.src(dirs.src + '/css/main.css')
.pipe(gulp.dest(dirs.dist + '/css/'));
}
function copyMisc() {
console.log('copyMisc');
return gulp.src([
// Copy all files
dirs.src + '/**',
// Exclude the following files
// (other tasks will handle the copying of these files)
'!' + dirs.src + '/index.html',
'!' + dirs.src + '/{css,css/**}'
], {
encoding: false,
// Include hidden files by default
dot: true
}).pipe(gulp.dest(dirs.dist));
}
function generateMainCSS() {
return gulp.src(dirs.src + '/css/index.css')
.pipe(gulpRework(reworkNpm()))
.pipe(gulpAutoprefixer())
.pipe(gulpCSSBase64())
.pipe(gulpUnCSS({
html: [dirs.src + '/index.html']
}))
.pipe(gulpCSSO())
.pipe(gulpRename('main.css'))
.pipe(gulp.dest(dirs.src + '/css/'));
}
function lintJS() {
return gulp.src([
'gulpfile.mjs',
dirs.src + '/js/**/*.js',
]).pipe(gulpESLint())
.pipe(gulpESLint.results(results => {
console.log(`Total Results: ${results.length}`);
console.log(`Total Warnings: ${results.warningCount}`);
console.log(`Total Errors: ${results.errorCount}`);
}))
.pipe(gulpESLint.failAfterError());
}
function minifyHTML() {
// In-depth information about the `htmlmin` options:
// https://github.com/kangax/html-minifier#options-quick-reference
var htmlminOptions = {
collapseBooleanAttributes: true,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
};
return gulp
.src([dirs.dist + "/index.html"])
.pipe(gulpSmoosher())
.pipe(gulpHTMLMin(htmlminOptions))
.pipe(gulp.dest(dirs.dist));
}
function browserSyncFn() {
browserSyncOptions.server = dirs.src;
browserSync(browserSyncOptions);
gulp.watch([dirs.src + "/**/*.html"], reload);
gulp.watch(
[
dirs.src + "/css/**/*.css",
dirs.src + "/img/**/*",
"!" + dirs.src + "/css/main.css",
],
gulp.series(generateMainCSS)
);
gulp.watch([dirs.src + "/js/**/*.js", "gulpfile.mjs"], gulp.parallel(reload));
}
gulp.task("serve", gulp.series(generateMainCSS, browserSyncFn));
gulp.task(
"build",
gulp.series(
gulp.parallel(cleanBefore),
generateMainCSS,
copyHTML,
copyCSS,
copyMisc,
minifyHTML,
cleanAfter
)
);
gulp.task(
"serve:build",
gulp.series("build", function () {
browserSyncOptions.server = dirs.dist;
browserSync(browserSyncOptions);
})
);