-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
184 lines (168 loc) · 4.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
183
184
const path = {
build: {
html: 'build/',
style: 'build/style/',
fonts: 'build/fonts/',
script: 'build/js/',
img: 'build/img/',
},
src: {
indexHtml: 'source/index.html',
html: 'source/**/*.html',
style: 'source/style/*.css',
fonts: 'source/fonts/',
script: 'source/**/*.js',
img: 'source/img/*',
},
clean: 'build/**',
};
const gulp = require('gulp');
const env = require('gulp-env'); // переменные окружения
const del = require('del'); // zero client tool
const babel = require('gulp-babel'); // для трансшпиляции
const concat = require('gulp-concat'); // для объединения файлов
const uglify = require('gulp-uglify'); // для минификации js-файлов
const gulpif = require('gulp-if'); // для минификации js-файлов
const cssnano = require('gulp-cssnano'); // для минификации css-файлов
const sourcemaps = require('gulp-sourcemaps');
const browserSync = require('browser-sync').create();
const rigger = require('gulp-rigger');
const ttf2woff = require('gulp-ttf2woff');
const ttf2woff2 = require('gulp-ttf2woff2');
const imagemin = require('gulp-imagemin'); // compress images
const autoprefixer = require('gulp-autoprefixer');
const ghPages = require('gh-pages');
const nodePath = require('path');
env({
file: '.env',
type: 'ini',
});
/* zero client */
gulp.task('clean', () => del(path.clean));
/* сборка html */
gulp.task('build-html', () => {
gulp.src(path.src.indexHtml)
.pipe(rigger())
.pipe(gulp.dest(path.build.html));
});
/* сборка стилей в один файл style-min.css */
gulp.task('build-styles', () => {
gulp.src(path.src.style)
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.init()))
.pipe(gulpif(process.env.PRODUCTION === 'switch-on', autoprefixer({
cascade: false,
})))
.pipe(concat('style-min.css'))
.pipe(gulpif(process.env.PRODUCTION === 'switch-on', cssnano()))
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.write()))
.pipe(gulp.dest(path.build.style));
});
/* сборка шрифтов */
gulp.task('build-fonts', () => {
gulp.src(`${ path.src.fonts }*.ttf`)
.pipe(ttf2woff())
.pipe(gulp.dest(path.src.fonts));
return gulp.src(`${ path.src.fonts }*.ttf`)
.pipe(ttf2woff2())
.pipe(gulp.dest(path.src.fonts));
});
gulp.task('copy-fonts', () => {
gulp.src(`${ path.src.fonts }*.*[^".css"]`)
.pipe(gulp.dest(path.build.fonts));
gulp.src(`${ path.src.fonts }*.css`)
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.init()))
.pipe(autoprefixer({
cascade: false,
}))
.pipe(concat('fonts.css'))
.pipe(gulpif(process.env.PRODUCTION === 'switch-on', cssnano({
minifyFontValues: false,
discardUnused: false,
})))
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.write()))
.pipe(gulp.dest(path.build.fonts));
});
/* сборка скриптов в один файл *-min.js */
gulp.task('build-scripts', () => {
gulp.src(path.src.script)
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.init()))
.pipe(concat('scripts-min.js'))
.pipe(gulpif(process.env.BABEL === 'switch-on', babel({
presets: [
'@babel/env',
],
})))
.pipe(gulpif(process.env.PRODUCTION === 'switch-on', uglify()))
.pipe(gulpif(process.env.SOURCEMAPS === 'switch-on', sourcemaps.write()))
.pipe(gulp.dest(path.build.script));
});
// compress images
gulp.task('build-images', () => {
gulp.src(path.src.img)
.pipe(imagemin())
.pipe(gulp.dest(path.build.img));
});
gulp.task(
'default', [
'build-fonts',
'copy-fonts',
'build-html',
'build-styles',
'build-scripts',
'build-images',
'browser-sync',
]);
// build for prod
gulp.task(
'build', [
'clean',
'build-fonts',
'copy-fonts',
'build-html',
'build-styles',
'build-scripts',
'build-images',
]);
gulp.task('browser-sync', () => {
browserSync.init({
server: {
baseDir: './build/',
},
port: 3006,
});
gulp.watch(path.src.fonts, [
'watch-fonts',
]);
gulp.watch(path.src.html, [
'watch-html',
]);
gulp.watch(path.src.style, [
'watch-styles',
]);
gulp.watch(path.src.script, [
'watch-scripts',
]);
gulp.watch(path.src.img, [
'watch-images',
]);
});
gulp.task('watch-fonts', [
'build-fonts',
'copy-fonts',
], () => browserSync.reload());
gulp.task('watch-html', [
'build-html',
], () => browserSync.reload());
gulp.task('watch-styles', [
'build-styles',
], () => browserSync.reload());
gulp.task('watch-scripts', [
'build-scripts',
], () => browserSync.reload());
gulp.task('watch-images', [
'build-images',
], () => browserSync.reload());
gulp.task('deploy', function deploy (cb) {
ghPages.publish(nodePath.join(process.cwd(), './build'), cb);
exports.deploy = deploy;
});