forked from htmlacademy/html2-basic-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
208 lines (185 loc) · 5.21 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
205
206
207
208
import { readFileSync, rmSync } from 'node:fs';
import gulp from 'gulp';
import plumber from 'gulp-plumber';
import htmlmin from 'gulp-htmlmin';
import * as dartSass from 'sass';
import gulpSass from 'gulp-sass';
import postcss from 'gulp-postcss';
import postUrl from 'postcss-url';
import lightningcss from 'postcss-lightningcss';
import { createGulpEsbuild } from 'gulp-esbuild';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import sharp from 'gulp-sharp-responsive';
import svgo from 'gulp-svgmin';
import { stacksvg } from 'gulp-stacksvg';
import server from 'browser-sync';
import bemlinter from 'gulp-html-bemlinter';
const { src, dest, watch, series, parallel } = gulp;
const sass = gulpSass(dartSass);
const PATH_TO_SOURCE = './source/';
const PATH_TO_DIST = './build/';
const PATH_TO_RAW = './raw/';
const PATHS_TO_STATIC = [
`${PATH_TO_SOURCE}fonts/**/*.{woff2,woff}`,
`${PATH_TO_SOURCE}*.ico`,
`${PATH_TO_SOURCE}*.webmanifest`,
`${PATH_TO_SOURCE}favicons/**/*.{png,svg}`,
`${PATH_TO_SOURCE}vendor/**/*`,
`${PATH_TO_SOURCE}images/**/*`,
`!${PATH_TO_SOURCE}**/README.md`,
];
let isDevelopment = true;
export function processMarkup () {
return src(`${PATH_TO_SOURCE}**/*.html`)
.pipe(htmlmin({ collapseWhitespace: !isDevelopment }))
.pipe(dest(PATH_TO_DIST))
.pipe(server.stream());
}
export function lintBem () {
return src(`${PATH_TO_SOURCE}**/*.html`)
.pipe(bemlinter());
}
export function processStyles () {
return src(`${PATH_TO_SOURCE}styles/*.scss`, { sourcemaps: isDevelopment })
.pipe(plumber())
.pipe(sass().on('error', sass.logError))
.pipe(postcss([
postUrl([
{
filter: '**/*',
assetsPath: '../',
},
{
filter: '**/icons/**/*.svg',
url: (asset) => asset.url.replace(
/icons\/(.+?)\.svg$/,
(match, p1) => `icons/stack.svg#${p1.replace(/\//g, '_')}`
),
multi: true,
},
]),
lightningcss({
lightningcssOptions: {
minify: !isDevelopment,
},
})
]))
.pipe(dest(`${PATH_TO_DIST}styles`, { sourcemaps: isDevelopment }))
.pipe(server.stream());
}
export function processScripts () {
const gulpEsbuild = createGulpEsbuild({ incremental: isDevelopment });
return src(`${PATH_TO_SOURCE}scripts/*.js`)
.pipe(gulpEsbuild({
bundle: true,
format: 'esm',
// splitting: true,
platform: 'browser',
minify: !isDevelopment,
sourcemap: isDevelopment,
target: browserslistToEsbuild(),
}))
.pipe(dest(`${PATH_TO_DIST}scripts`))
.pipe(server.stream());
}
export function optimizeRaster () {
const RAW_DENSITY = 2;
const TARGET_FORMATS = [undefined, 'webp']; // undefined — initial format: jpg or png
function createOptionsFormat() {
const formats = [];
for (const format of TARGET_FORMATS) {
for (let density = RAW_DENSITY; density > 0; density--) {
formats.push(
{
format,
rename: { suffix: `@${density}x` },
width: ({ width }) => Math.ceil(width * density / RAW_DENSITY),
jpegOptions: { progressive: true },
},
);
}
}
return { formats };
}
return src(`${PATH_TO_RAW}images/**/*.{png,jpg,jpeg}`)
.pipe(sharp(createOptionsFormat()))
.pipe(dest(`${PATH_TO_SOURCE}images`));
}
export function optimizeVector () {
return src([`${PATH_TO_RAW}**/*.svg`])
.pipe(svgo())
.pipe(dest(PATH_TO_SOURCE));
}
export function createStack () {
return src(`${PATH_TO_SOURCE}icons/**/*.svg`)
.pipe(stacksvg())
.pipe(dest(`${PATH_TO_DIST}icons`));
}
export function copyStatic () {
return src(PATHS_TO_STATIC, { base: PATH_TO_SOURCE })
.pipe(dest(PATH_TO_DIST));
}
export function startServer () {
const serveStatic = PATHS_TO_STATIC
.filter((path) => path.startsWith('!') === false)
.map((path) => {
const dir = path.replace(/(\/\*\*\/.*$)|\/$/, '');
const route = dir.replace(PATH_TO_SOURCE, '/');
return { route, dir };
});
server.init({
server: {
baseDir: PATH_TO_DIST
},
serveStatic,
cors: true,
notify: false,
ui: false,
}, (err, bs) => {
bs.addMiddleware('*', (req, res) => {
res.write(readFileSync(`${PATH_TO_DIST}404.html`));
res.end();
});
});
watch(`${PATH_TO_SOURCE}**/*.{html,njk}`, series(processMarkup));
watch(`${PATH_TO_SOURCE}styles/**/*.scss`, series(processStyles));
watch(`${PATH_TO_SOURCE}scripts/**/*.js`, series(processScripts));
watch(`${PATH_TO_SOURCE}icons/**/*.svg`, series(createStack, reloadServer));
watch(PATHS_TO_STATIC, series(reloadServer));
}
function reloadServer (done) {
server.reload();
done();
}
export function removeBuild (done) {
rmSync(PATH_TO_DIST, {
force: true,
recursive: true,
});
done();
}
export function buildProd (done) {
isDevelopment = false;
series(
removeBuild,
parallel(
processMarkup,
processStyles,
processScripts,
createStack,
copyStatic,
),
)(done);
}
export function runDev (done) {
series(
removeBuild,
parallel(
processMarkup,
processStyles,
processScripts,
createStack,
),
startServer,
)(done);
}