-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
104 lines (79 loc) · 2.37 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
const { src, dest, watch, parallel } = require('gulp');
// CSS
const sass = require('gulp-sass')(require('sass'));
const plumber = require('gulp-plumber');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const postcss = require('gulp-postcss');
const sourcemaps = require('gulp-sourcemaps');
// Imágenes
const cache = require('gulp-cache');
const imagemin = require('gulp-imagemin');
const webp = require('gulp-webp');
const avif = require('gulp-avif');
// Javascript
const terser = require('gulp-terser-js');
// Función de compilado de CSS
function css( done ) {
console.log('Compilando SASS...');
src('src/scss/**/*.scss') // Identificar los archivos scss a compilar
.pipe(sourcemaps.init())
.pipe( plumber())
.pipe( sass() ) // Compilarlo
.pipe( postcss([ autoprefixer(), cssnano() ]))
.pipe( sourcemaps.write('.'))
.pipe( dest('build/css') ) // Almacenarla en el disco duro
done();
}
// Función de procesado de imágenes
function imagenes(done){
const opciones = {
optimizationLevel: 3
}
src('src/img/**/*.{png,jpg}')
.pipe( cache( imagemin(opciones) ) )
.pipe( dest('build/img') )
done();
}
// Creación de versiones WebP
function versionWebp( done ) {
const opciones = {
quality: 50
};
src('src/img/**/*.{png,jpg}') // Añadir extensiones a procesar
.pipe( webp( opciones ) )
.pipe( dest('build/img') )
done();
}
// Creación de versiones Avif
// - Formato que aun no tiene suficiente soporte: https://caniuse.com/?search=avif
function versionAvif( done ) {
const opciones = {
quality: 50
};
src('src/img/**/*.{png,jpg}')
.pipe( avif( opciones ) )
.pipe( dest('build/img') )
done();
}
// Compilado y minificado de JS
function javascript( done ) {
src('src/js/**/*.js')
.pipe(sourcemaps.init())
.pipe( terser() )
.pipe(sourcemaps.write('.'))
.pipe(dest('build/js'));
done();
}
// Watch de cambios
function dev( done ) {
watch('src/scss/**/*.scss', css);
watch('src/js/**/*.js', javascript);
done();
}
exports.css = css;
exports.js = javascript;
exports.imagenes = imagenes;
exports.versionWebp = versionWebp;
exports.versionAvif = versionAvif;
exports.dev = parallel( imagenes, versionWebp, versionAvif, javascript, dev );