-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
182 lines (138 loc) · 3.91 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
const {src, dest, watch, series, parallel} = require("gulp");
const fs = require("fs");
const browserSync = require("browser-sync").create();
const webpack = require("webpack-stream");
const plumber = require('gulp-plumber');
const notify = require("gulp-notify");
const scss = require("gulp-sass")(require("sass"));
const fileInclude = require("gulp-file-include");
const clean = require("gulp-clean");
const webp = require("gulp-webp");
const newer = require("gulp-newer");
const imagemin = require("gulp-imagemin");
const isProd = process.argv.includes("--prod");
const isDev = !isProd;
const srcPath = "src/";
const destPath = "dist/";
const webpackConfig = {
mode: isDev ? "development" : "production",
entry: {
main: "/src/js/main.js",
menu: "/src/js/menu.js",
preview: "/src/js/preview.js",
modal: "/src/js/modal.js",
theme: "/src/js/theme.js"
},
output: {
filename: "[name].bundle.js",
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
}
function cleanDest(done) {
if (fs.existsSync(`${destPath}`)) {
return src(`${destPath}`, {read: false})
.pipe(clean({force: true}))
}
done();
}
function copyImages() {
return src(`${srcPath}img/**/*`)
.pipe(dest(`${destPath}/img`))
}
function copyFonts() {
return src(`${srcPath}fonts/**/*`)
.pipe(newer(`${destPath}fonts/`))
.pipe(dest(`${destPath}/fonts`));
}
function convertToWebp() {
return src([`${srcPath}img/**/*`, `!${srcPath}img/**/*.{svg,webp}`])
.pipe(newer(`${destPath}img/`))
.pipe(webp())
.pipe(dest(`${destPath}img`))
}
function optimizeImages() {
convertToWebp();
return src([`${srcPath}img/**/*`, `!${srcPath}img/**/*.{svg,webp}`])
.pipe(newer(`${destPath}img/`))
.pipe(imagemin({verbose: true}))
.pipe(dest(`${destPath}img`))
}
function plumberNotify(Errortitle) {
return {
errorHandler: notify.onError({
title: Errortitle,
message: "Error: <%= error.message %>",
sound: false
})
}
}
function buildStyles() {
webpackConfig.mode = isDev ? "development" : "production";
return src(`${srcPath}scss/**/*.scss`, {sourcemaps: isDev})
.pipe(plumber(plumberNotify("SCSS")))
.pipe(scss())
.pipe(dest(`${destPath}css`, {sourcemaps: "."}))
.pipe(browserSync.stream())
}
function buildScripts() {
return src(`${srcPath}js/**/*.js`)
.pipe(plumber(plumberNotify("JS")))
.pipe(webpack(webpackConfig))
.pipe(dest(`${destPath}js/`))
.pipe(browserSync.stream())
}
function buildHTML() {
return src(`${srcPath}**/*.html`)
.pipe(fileInclude({
prefix: "@@",
basepath: "@file"
}))
.pipe(dest(`${destPath}`))
}
function browserSyncServer(done) {
browserSync.init({
server: {
baseDir: "./dist"
}
});
done();
}
function watchTasks(done) {
watch([`${srcPath}scss/**/*.scss`], buildStyles);
watch([`${srcPath}js/**/*.js`], buildScripts).on("change", browserSync.reload);
watch([`${srcPath}**/*.html`], buildHTML).on("change", browserSync.reload);
watch([`${srcPath}img`], optimizeImages)
watch([`${srcPath}fonts/**/*`], copyFonts)
}
const dev = series(
cleanDest,
copyImages,
copyFonts,
buildStyles,
buildScripts,
buildHTML,
parallel(browserSyncServer, watchTasks)
);
const prod = series(
cleanDest,
copyImages,
copyFonts,
optimizeImages,
buildStyles,
buildScripts,
buildHTML,
);
module.exports = {
dev,
prod,
default: dev
}
/* npm run dev - режим разработки */
/* npm run prod - режим сборки */