-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
76 lines (65 loc) · 2.24 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
import path from 'path'
import fs from 'fs'
import { glob } from 'glob'
import { src, dest, watch, series } from 'gulp'
import * as dartSass from 'sass'
import gulpSass from 'gulp-sass'
import terser from 'gulp-terser'
import sharp from 'sharp'
const sass = gulpSass(dartSass)
const paths = {
scss: 'src/scss/**/*.scss',
js: 'src/js/**/*.js'
}
export function css( done ) {
src(paths.scss, {sourcemaps: true})
.pipe( sass({
outputStyle: 'compressed'
}).on('error', sass.logError) )
.pipe( dest('./public/build/css', {sourcemaps: '.'}) );
done()
}
export function js( done ) {
src(paths.js)
.pipe(terser())
.pipe(dest('./public/build/js'))
done()
}
export async function imagenes(done) {
const srcDir = './src/img';
const buildDir = './public/build/img';
const images = await glob('./src/img/**/*')
images.forEach(file => {
const relativePath = path.relative(srcDir, path.dirname(file));
const outputSubDir = path.join(buildDir, relativePath);
procesarImagenes(file, outputSubDir);
});
done();
}
function procesarImagenes(file, outputSubDir) {
if (!fs.existsSync(outputSubDir)) {
fs.mkdirSync(outputSubDir, { recursive: true })
}
const baseName = path.basename(file, path.extname(file))
const extName = path.extname(file)
if (extName.toLowerCase() === '.svg') {
// If it's an SVG file, move it to the output directory
const outputFile = path.join(outputSubDir, `${baseName}${extName}`);
fs.copyFileSync(file, outputFile);
} else {
// For other image formats, process them with sharp
const outputFile = path.join(outputSubDir, `${baseName}${extName}`);
const outputFileWebp = path.join(outputSubDir, `${baseName}.webp`);
const outputFileAvif = path.join(outputSubDir, `${baseName}.avif`);
const options = { quality: 80 };
sharp(file).jpeg(options).toFile(outputFile);
sharp(file).webp(options).toFile(outputFileWebp);
sharp(file).avif().toFile(outputFileAvif);
}
}
export function dev() {
watch( paths.scss, css );
watch( paths.js, js );
watch('src/img/**/*.{png,jpg}', imagenes)
}
export default series( js, css, imagenes, dev )