/
gulpfile.js
154 lines (140 loc) · 4.17 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
const gulp = require("gulp");
const config = require("./env.paths.json");
const env = process.env.NODE_ENV;
// плагины галпа отдельно подключать не нужно,
// используем в пайпе как $gp.имяПлагина (без приставки gulp-)
const $gp = require("gulp-load-plugins")();
const browserSync = require("browser-sync").create();
const reload = browserSync.reload;
const $webpack = require("webpack-stream");
const webpack = require("webpack");
const del = require("del");
// стили
gulp.task("styles", () => {
return gulp
.src(`${config.SRC_DIR}/styles/main.scss`)
.pipe($gp.sourcemaps.init())
.pipe($gp.plumber())
.pipe($gp.postcss(require("./postcss.config")))
.pipe($gp.rename("main.min.css"))
.pipe($gp.if(env === "development", $gp.sourcemaps.write()))
.pipe(gulp.dest(`${config.DIST_DIR}`))
.pipe(reload({ stream: true }));
});
// переносим шрифты
gulp.task("fonts", () => {
return gulp
.src(`${config.SRC_DIR}/fonts/**`)
.pipe(gulp.dest(`${config.DIST_DIR}/assets/fonts/`));
});
// переносим медиа-файлы
gulp.task("media", () => {
return gulp
.src(`${config.SRC_DIR}/media/**`)
.pipe(gulp.dest(`${config.DIST_DIR}/assets/media/`));
});
// очистка
gulp.task("clean", () => {
return del(config.ROOT_PATH);
});
// собираем скрипты webpack
gulp.task("scripts", () => {
return gulp
.src(`${config.SRC_DIR}/scripts/*.js`)
.pipe($gp.plumber())
.pipe($webpack(require("./webpack.mpa.config"), webpack))
.pipe(gulp.dest(`${config.DIST_DIR}`))
.pipe(reload({ stream: true }));
});
//рендерим странички
gulp.task("pug", () => {
return gulp
.src(`${config.VIEWS_DIR}/pages/*.pug`)
.pipe($gp.plumber())
.pipe($gp.pug({pretty:true})) // 20180605 Добавил параметр pretty:true для форматирования выходных html-файлов
.pipe(gulp.dest(`${config.DIST_DIR}`))
.pipe(reload({ stream: true }));
});
// dev сервер + livereload (встроенный)
gulp.task("server", () => {
browserSync.init({
server: {
baseDir: `${config.DIST_DIR}`
},
open: false
});
});
// спрайт иконок
gulp.task("svg", done => {
return gulp
.src(`${config.SRC_DIR}/images/icons/*.svg`)
.pipe(
$gp.svgmin({
js2svg: {
pretty: true
}
})
)
.pipe(
$gp.cheerio({
run($) {
$("[fill], [stroke], [style], [width], [height]")
.removeAttr("fill")
.removeAttr("stroke")
.removeAttr("style")
.removeAttr("width")
.removeAttr("height");
},
parserOptions: { xmlMode: true }
})
)
.pipe($gp.replace(">", ">"))
.pipe(
$gp.svgSprite({
mode: {
symbol: {
sprite: "../sprite.svg"
}
}
})
)
.pipe(gulp.dest(`${config.DIST_DIR}/assets/images/icons`));
});
// просто переносим картинки
gulp.task("images", () => {
return gulp
.src([
`${config.SRC_DIR}/images/**/*.*`,
`!${config.SRC_DIR}/images/icons/*.*`
])
.pipe($gp.if(env === "production", $gp.imagemin()))
.pipe(gulp.dest(`${config.DIST_DIR}/assets/images/`));
});
// галповский вотчер
gulp.task("watch", () => {
gulp.watch(`${config.SRC_DIR}/styles/**/*.scss`, gulp.series("styles"));
gulp.watch(`${config.SRC_DIR}/images/**/*.*`, gulp.series("images"));
gulp.watch(`${config.SRC_DIR}/scripts/**/*.js`, gulp.series("scripts"));
gulp.watch(`${config.SRC_DIR}/fonts/*`, gulp.series("fonts"));
gulp.watch(`${config.SRC_DIR}/media/*`, gulp.series("media"));
gulp.watch(`${config.VIEWS_DIR}/**/*.pug`, gulp.series("pug"));
});
// GULP:DEV
gulp.task(
"default",
gulp.series(
"clean",
"svg",
gulp.parallel("styles", "pug", "images", "fonts", "media", "scripts"),
gulp.parallel("watch", "server")
)
);
// GULP:build
gulp.task(
"build",
gulp.series(
"clean",
"svg",
gulp.parallel("styles", "pug", "images", "fonts", "media", "scripts")
)
);