-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
124 lines (111 loc) · 2.79 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
import gulp from "gulp";
import gulpSass from "gulp-sass";
import * as dartSass from "sass";
import postcss from "gulp-postcss";
import autoprefixer from "autoprefixer";
import mqpacker from "css-mqpacker";
import { deleteAsync } from "del";
import browserSync from "browser-sync";
import webpack from "webpack-stream";
import fileInclude from "gulp-file-include";
import changed from "gulp-changed";
import imagemin, { svgo, mozjpeg } from "gulp-imagemin";
import imageminPngquant from "imagemin-pngquant";
import gulpif from "gulp-if";
const isBuild = process.argv[2] === "build";
const { src, dest, series, parallel, watch, task } = gulp;
const sass = gulpSass(dartSass);
const paths = {
src: {
root: "./src/",
styles: "./src/scss/**/*.scss",
js: "./src/js/main.js",
assets: "./src/assets/**/*",
},
dist: {
root: "./dist/",
styles: "./dist/css",
js: "./dist/js",
assets: "./dist/assets",
},
};
function styles() {
const postcssConfig = [
autoprefixer({ cascade: false }),
mqpacker({
sort: function (a, b) {
a = a.replace(/\D/g, "");
b = b.replace(/\D/g, "");
return b - a;
},
}),
];
return src(paths.src.styles)
.pipe(sass().on("error", sass.logError))
.pipe(postcss(postcssConfig))
.pipe(dest(paths.dist.styles))
.pipe(browserSync.stream());
}
function scripts() {
return src(paths.src.js)
.pipe(
webpack({
mode: "production",
output: {
filename: "main.js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: {
loader: "babel-loader",
options: {
presets: [["@babel/preset-env", { targets: "defaults" }]],
},
},
},
],
},
})
)
.pipe(dest(paths.dist.js))
.pipe(browserSync.stream());
}
async function clean() {
return await deleteAsync(["dist/*"]);
}
function html() {
return src(paths.src.root + "**/*.html")
.pipe(fileInclude())
.pipe(dest(paths.dist.root));
}
function copyAssets() {
return src(paths.src.assets)
.pipe(changed(paths.dist.assets))
.pipe(
gulpif(
isBuild,
imagemin([
mozjpeg({ quality: 80 }),
imageminPngquant({ quality: [0.8, 0.9] }),
svgo(),
])
)
)
.pipe(dest(paths.dist.assets));
}
function watchFiles() {
browserSync.init({
server: {
baseDir: "./dist",
},
open: false,
});
watch(paths.src.styles, styles);
watch(paths.src.js, scripts);
watch(paths.src.assets, copyAssets);
watch("./src/**/*.html", html).on("change", browserSync.reload);
}
task("build", series(clean, parallel(styles, scripts, html, copyAssets)));
task("dev", series("build", watchFiles));