-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
214 lines (193 loc) · 5.37 KB
/
gulpfile.js
File metadata and controls
214 lines (193 loc) · 5.37 KB
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
209
210
211
212
213
214
import gulp from 'gulp';
import * as dartSass from 'sass';
import gulpSass from 'gulp-sass';
import sassGlob from 'gulp-sass-glob';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import {deleteAsync} from 'del';
import browserSync from 'browser-sync';
import browserslist from 'browserslist';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import esbuild from 'gulp-esbuild';
import sharp from 'sharp';
import through2 from 'through2';
import gulpNewer from 'gulp-newer';
import {optimize} from 'svgo'
const bs = browserSync.create();
const sass = gulpSass(dartSass);
const rasterSrc = 'src/img/*.{jpg,jpeg,png}';
const rasterDest = 'dist/img';
const svgSrc = 'src/img/*.svg';
const svgDest = 'dist/img';
const isProd = process.env.NODE_ENV === 'production';
const isDev = !isProd;
const paths = {
src: 'src',
dist: 'dist',
html: {
src: 'src/**/*.html',
dest: 'dist',
},
styles: {
entry: 'src/styles/main.scss',
watch: 'src/styles/**/*.scss',
dest: 'dist/css',
},
scripts: {
entry: 'src/js/index.js',
watch: 'src/js/**/*.js',
dest: 'dist/js',
},
assets: {
src: [
'src/fonts/**/*',
'src/favicons/**/*',
'src/site.webmanifest',
'src/favicon.ico',
],
base: 'src',
dest: 'dist',
}
}
export function clean() {
return deleteAsync([paths.dist]);
}
export function html() {
return gulp.src(paths.html.src)
.pipe(gulp.dest(paths.html.dest))
.pipe(bs.stream());
}
export function styles() {
const plugins = [autoprefixer()];
if (isProd) plugins.push(cssnano());
return gulp.src(paths.styles.entry, {sourcemaps: isDev})
.pipe(sassGlob())
.pipe(sass.sync({outputStyle: 'expanded'}).on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(gulp.dest(paths.styles.dest, {sourcemaps: isDev}))
.pipe(bs.stream());
}
export function optimizeRaster() {
return gulp.src(rasterSrc)
.pipe(gulpNewer(rasterDest))
.pipe(through2.obj(async (file, _, cb) => {
try {
const ext = extname(file.path).toLowerCase();
if (ext === '.jpg' || ext === '.jpeg') {
file.contents = await sharp(file.path)
.toColorspace('srgb')
.jpeg({ quality: 80, progressive: true, mozjpeg: true })
.toBuffer()
// оставляем .jpg / .jpeg как есть
} else if (ext === '.png') {
file.contents = await sharp(file.path)
.png({ compressionLevel: 9, adaptiveFiltering: true })
.toBuffer()
// оставляем .png
}
cb(null, file)
} catch {
cb() // пропускаем неподдерживаемые
}
}))
.pipe(gulp.dest(rasterDest))
}
export function createWebp () {
return gulp.src(rasterSrc)
.pipe(through2.obj(async (file, _, cb) => {
try {
const out = await sharp(file.path).webp({ quality: 80 }).toBuffer()
file.contents = out
file.path = file.path.replace(/\.(jpe?g|png)$/i, '.webp')
cb(null, file)
} catch {
cb() // пропускаем
}
}))
.pipe(gulp.dest(rasterDest))
}
export function optimizeVector() {
return gulp.src(svgSrc)
.pipe(gulpNewer(svgDest))
.pipe(through2.obj((file, _, cb) => {
if (!file.isBuffer()) return cb(null, file);
try {
const {data} = optimize(file.contents.toString(), {
multipass: true,
floatPrecision: 2,
plugins: [
{ name: 'removeViewBox', active: false },
]
})
file.contents = Buffer.from(data)
cb(null, file)
} catch (e) {
cb(e)
}
}))
.pipe(gulp.dest(svgDest))
}
// --- SCRIPTS: esbuild bundle
export function scripts() {
const target = browserslistToEsbuild(browserslist());
return gulp.src(paths.scripts.entry)
.pipe(esbuild({
bundle: true,
format: 'iife',
sourcemap: isDev,
minify: isProd,
target,
outfile: 'bundle.js' // имя итогового файла в dest
}))
.pipe(gulp.dest(paths.scripts.dest))
.pipe(bs.stream())
}
// --- ASSETS: просто копируем статику как есть
export function assets() {
return gulp.src(paths.assets.src, {base: paths.assets.base, allowEmpty: true})
.pipe(gulp.dest(paths.assets.dest))
.pipe(bs.stream());
}
// --- сервер разработки
export function serve(done) {
bs.init({
server: {baseDir: paths.dist},
notify: false,
open: false,
});
done();
}
// --- наблюдение
export function watcher() {
gulp.watch(paths.html.src, html);
gulp.watch(paths.styles.watch, styles);
gulp.watch('src/img/*.{jpg,jpeg,png}', optimizeRaster, createWebp);
gulp.watch('src/img/*.svg', optimizeVector)
gulp.watch(paths.scripts.watch, scripts);
gulp.watch(paths.assets.src, assets);
}
// --- вспомогательный таск: установить PROD для билда
export function setProd(done) {
process.env.NODE_ENV = 'production';
done();
}
// --- сборки
export const build = gulp.series(
setProd,
clean,
gulp.parallel(html, styles, scripts, assets),
optimizeRaster,
createWebp,
optimizeVector,
)
export const dev = gulp.series(
clean,
gulp.parallel(html, styles, scripts, assets),
optimizeRaster,
createWebp,
optimizeVector,
serve,
watcher,
)
export default dev