This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
258 lines (230 loc) · 6.84 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
//npm install gulp gulp-jade gulp-stylus autoprefixer-stylus gulp-imagemin browser-sync gulp-cssbeautify gulp-util gulp-imagemin browser-sync gulp-cssbeautify gulp-util gulp-newer gulp-include gulp-rename gulp-uglify imagemin-pngquant gulp-csscomb gulp-csso gulp-filter gulp-plumber del gulp-jade-inheritance gulp-if gulp-cached gulp-changed gulp-filter gulp-stylint run-sequence gulp-watch
// Инициализируем плагины
var gulp = require('gulp'),
jade = require('gulp-jade'),
stylus = require('gulp-stylus'),
autoprefixer = require('autoprefixer-stylus'),
imagemin = require('gulp-imagemin'),
browserSync = require('browser-sync').create(),
cssbeautify = require('gulp-cssbeautify'),
gutil = require('gulp-util'),
newer = require('gulp-newer'),
include = require('gulp-include'),
rename = require("gulp-rename"),
uglify = require('gulp-uglify'),
imageminPngquant = require('imagemin-pngquant'),
csscomb = require('gulp-csscomb'),
csso = require('gulp-csso'),
gulpFilter = require('gulp-filter'),
plumber = require('gulp-plumber'),
del = require('del'),
runSequence = require('run-sequence'),
watch = require('gulp-watch'),
// gulpZip = require('gulp-zip'),
// nodePath = require('path'),
jadeInheritance = require('gulp-jade-inheritance'),
gulpif = require('gulp-if'),
cached = require('gulp-cached'),
changed = require('gulp-changed'),
filter = require('gulp-filter'),
stylint = require('gulp-stylint');
var errorHandler = function(err) {
gutil.log([(err.name + ' in ' + err.plugin).bold.red, '', err.message, ''].join('\n'));
if (gutil.env.beep) {
gutil.beep();
}
this.emit('end');
};
// Имена папок
var config = {
path: {
source: 'source',
dist: 'public',
assets: 'assets',
partials: 'blocks',
js: 'js',
css: 'css',
images: 'img'
}
};
// Настройки плагинов
var plugins = {
browserSync: {
options: {
server: {
baseDir: './public'
}
}
}
,
autoprefixer: {
options: {
browsers: [
'last 2 version',
'Chrome >= 20',
'Firefox >= 20',
'Opera >= 12',
'Android 2.3',
'Android >= 4',
'iOS >= 6',
'Safari >= 6',
'Explorer >= 8'
],
cascade: false
}
},
stylus: {
options: {}
},
cssbeautify: {
options: {
indent: ' ',
autosemicolon: true
}
},
jade: {
options: {
pretty: '\t',
basedir: config.path.source
}
},
jadeInheritance: {
options: {basedir: config.path.source}
},
imagemin: {
options: {
optimizationLevel: 3,
progressive: true,
interlaced: true,
svgoPlugins: [{removeViewBox: false}],
use: [imageminPngquant()]
}
},
rename: {
options: {
suffix: ".min"
}
}
};
// Пути к файлам
var path = {
source: {
html: [
config.path.source + '/**/*.jade',
'!' + config.path.source + '/' + config.path.partials + '/**/*.jade'
],
css: [
config.path.source + '/**/*.styl',
'!' + config.path.source + '/**/_*.styl',
'!' + config.path.source + '/' + config.path.css + '/lib/**/*.styl'
],
img: config.path.source + '/' + config.path.images + '/**/*.{jpg,jpeg,png,gif,svg}',
js: config.path.source + '/' + config.path.js + '/**/*.js',
copy: config.path.assets + '/**/*'
},
dest: {
html: config.path.dist,
css: config.path.dist,
img: config.path.dist + '/' + config.path.images,
js: config.path.dist + '/' + config.path.js,
copy: config.path.dist
},
watch: {
html: config.path.source + '/**/*.jade',
css: config.path.source + '/**/*.styl',
img: config.path.source + '/' + config.path.images + '/**/*.{jpg,jpeg,png,gif,svg}',
js: config.path.source + '/**/*.js',
copy: config.path.assets + '/**/*'
}
};
// Локальный сервер
gulp.task('browser-sync', function () {
return browserSync.init(plugins.browserSync.options);
});
gulp.task('build', function (cb) {
return runSequence(
[
'stylus',
'jade',
'images',
'plugins',
'copy'
],
cb
);
});
// Собираем Stylus
gulp.task('stylus', function() {
return gulp.src(path.source.css)
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(stylint())
.pipe(stylint.reporter({config: '.stylintrc'}))
.pipe(stylus({
use: [
autoprefixer(plugins.autoprefixer.options)
]
}))
.pipe(cssbeautify(plugins.cssbeautify.options))
.pipe(csscomb())
.pipe(gulp.dest(path.dest.css))
.pipe(browserSync.stream())
.pipe(csso())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(path.dest.css));
});
// Собираем html из Jade
gulp.task('jade', function () {
return gulp.src('source/**/*.jade')
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(cached('jade'))
.pipe(gulpif(global.isWatching, jadeInheritance({basedir: 'source'})))
.pipe(filter(function (file) {
return !/source[\\\/]blocks/.test(file.path);
}))
.pipe(jade(plugins.jade.options))
.pipe(gulp.dest(path.dest.html));
});
// Копируем и минимизируем изображения
gulp.task('images', function () {
return gulp.src(path.source.img)
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(newer(path.dest.img))
.pipe(imagemin(plugins.imagemin.options))
.pipe(gulp.dest(path.dest.img));
});
// Собираем JS
gulp.task('plugins', function () {
return gulp.src(path.source.js)
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(include())
.pipe(gulp.dest(path.dest.js))
.pipe(uglify().on('error', gutil.log))
.pipe(rename(plugins.rename.options))
.pipe(gulp.dest(path.dest.js));
});
// Копируем файлы
gulp.task('copy', function () {
return gulp.src(path.source.copy)
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(newer(path.dest.copy))
.pipe(gulp.dest(path.dest.copy))
.pipe(gulpFilter(['**/*.js', '!**/*.min.js']))
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(path.dest.css));
});
// Отчистка папки public
gulp.task('cleanup', function (cb) {
return del(config.path.dist + '/*', cb);
});