-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
294 lines (251 loc) · 9.69 KB
/
Copy pathgulpfile.js
File metadata and controls
294 lines (251 loc) · 9.69 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
const gulp = require('gulp'),
babel = require('gulp-babel'),
cssnano = require('gulp-cssnano'),
autoprefixer = require('gulp-autoprefixer'),
gulpif = require('gulp-if'),
sourcemaps = require('gulp-sourcemaps'),
sass = require('gulp-sass'),
watch = require('gulp-watch'),
csslint = require('gulp-csslint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
imacss = require('gulp-imacss'),
base64 = require('gulp-base64'),
pug = require('gulp-pug2'),
notify = require('gulp-notify'),
rename = require('gulp-rename'),
browserSync = require('browser-sync').create(),
fpath = require('path'),
argv = require('yargs').argv,
iconDir = fpath.join(__dirname, 'options/img/'),
reload = browserSync.reload;
// Костыли и велосипеды.
// --------------------------------------
/* небольшой костыль с помощью которого
можем фильтровать файлы по разрешению,
юзаем с gulp-if.
*/
let filter = ext => {
let reg = new RegExp( ext, 'i');
return file => {
return !!file.path.match(reg);
}
}
if ( argv.server){
if ( typeof argv.server === 'string') {
let theme = argv.server.split(':');
var projectName = theme[0];
var drupal = `w:/${projectName}/sites/all/themes/${theme[1]}`;
}
else {
return console.error('Ошибка: ты забыл указать проект и тему через символ ":" (например project:theme_name).');
}
}
// Пути
// -------------------
var path = {
source: {
html: './source/*.{html,pug}',
styles: './source/css/main.scss',
images: './source/images/**/*.*',
svg2base: './source/images/**/*.svg',
js: './source/js/**.js',
fonts: './source/fonts/**/*.*',
base64: './source/css/base64.css'
},
watch: {
html: './source/**/*.{html,pug}',
styles: './source/css/**/*.scss',
images: './source/images/**/*.*',
js: './source/js/**/*.js',
fonts: './source/fonts/**/*.*',
base64: './source/css/base64.css'
},
production: {
html: './production/',
defaultStyles: './production/css/',
defaultImages: './production/images/',
defaultJs: './production/js/',
defaultFonts: './production/fonts/',
styles: argv.server ? `${ drupal }/css/`:'./production/css/',
images: argv.server ? `${ drupal }/images/`:'./production/images/',
js: argv.server ? `${ drupal }/js/`:'./production/js/',
fonts: argv.server ? `${ drupal }/fonts/`:'./production/fonts/'
}
}
var isTogether = argv.together;
// Запускаем наш сервер
// --------------------
gulp.task('local:server', () => {
return browserSync.init({
server: {
baseDir: './production'
}
});
});
/* таск ниже пригодиться когда будем тянуть
данные с red сервера. Удобно при разработке,
не нужно будет обновлять страницу.
*/
gulp.task('proxy:server', () => {
return browserSync.init({
proxy: `http://${projectName}.raduga.red/`
});
});
// HTML build
// ----------------------
/* сборка html,также пережует Jade-файлы.
*/
gulp.task('build:html', () => {
return gulp.src(path.source.html)
.pipe( pug({
pretty: true,
compileDebug: true,
debug: false
}).on('error', notify.onError({
title: 'Jade: Fail!',
icon: iconDir + 'jade.png',
message: (err) => {return 'Обнаружен говнокод ' + err.message}
})))
.pipe(gulp.dest(path.production.html))
.pipe(reload({stream:true}));
});
// Styles build
// ----------------------
/* Сборка стилей: пропускаем через sourcemap,
компилируем scss в привычный css, добавляем
горстку автопрефиксов, просмотрим нужно ли нам
какие-либо картинки закатать в base64.
*/
gulp.task('build:styles', () => {
return gulp.src(path.source.styles)
.pipe( gulpif(!argv.production, sourcemaps.init({loadMaps: true})))
.pipe(sass()
.on('error', notify.onError({
title: 'CSS: Fail!',
icon: iconDir + 'notify.png',
message: (err) => {return 'Обнаружен говнокод в стилях ' + err.message}
})))
.pipe(autoprefixer({
browsers: ['ie 9', '> 1% in RU', 'last 10 versions'],
cascade: false
}))
.pipe(base64({
baseDir: './source/images_base64/',
extensions: [ /#base64/i],
exclude: [/\.server\.(com|net)\/dynamic\//, '--live.jpg'],
maxImageSize: 8*1024, // bytes
debug: true
}))
// .pipe(csslint())
// .pipe(csslint.reporter())
.pipe(gulpif(!argv.production , sourcemaps.write()))
.pipe(gulp.dest(path.production.styles))
.pipe(gulpif( isTogether && !argv.proxy , gulp.dest(path.production.defaultStyles)))
.pipe(browserSync.stream());
});
// Images build
// ----------------------
/* Сборка изображений(png, jpg, svg), оптимизирует
их, ужимает отправляет куда надо.
*/
gulp.task('build:images', () => {
return gulp.src(path.source.images)
.pipe(imagemin(
[
imagemin.gifsicle(),
imagemin.jpegtran(),
imagemin.optipng(),
imagemin.svgo({
plugins: [
{
cleanupIDs: false
}
]
})
], {
optimizationLevel: 3,
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(path.production.images))
.pipe(gulpif( isTogether && !argv.proxy , gulp.dest(path.production.defaultImages)))
.pipe(reload({stream: true}));
});
/* кодируем картинки в base64.
*/
gulp.task('build:base64', () =>{
return gulp.src(path.source.base64)
.pipe(base64({
baseDir: './production/images/',
extensions: [ /#base64/i],
exclude: [/\.server\.(com|net)\/dynamic\//, '--live.jpg'],
maxImageSize: 8*1024, // bytes
debug: true
}))
.pipe(gulp.dest(path.production.styles))
});
// JS build
// ----------------------
/* Сборка JS, потоком отправляет его в продакш сборку,
сделав дополнительно минифицированную копию
*/
gulp.task('build:js', () => {
return gulp.src(path.source.js)
.pipe( gulpif(argv.babel, babel({
presets: ['es2015']
}).on('error', notify.onError({
title: 'JS: fail',
// icon: fpath.join(__dirname, 'options/img/js-1.png'),
icon: iconDir + 'js-1.png',
message: (err) => {return 'С этим кодом что-то пошло не так... ' + err.message}
}))))
.pipe(gulp.dest(path.production.js))
.pipe(gulpif( isTogether && !argv.proxy , gulp.dest(path.production.defaultJs)))
.pipe(reload({stream:true}));
});
// Fonts
// ----------------------
/* Сборка Fonts, собственно сборки здесь как таковой нет,
при поступлении шрифтов, gulp перемещает в продакш директорию,
сообщая серверу что надо бы обновить страницу.
*/
gulp.task('build:fonts', () => {
return gulp.src(path.source.fonts)
.pipe(gulp.dest(path.production.fonts))
.pipe(gulpif( isTogether && !argv.proxy , gulp.dest(path.production.defaultFonts)))
.pipe(reload({stream:true}));
});
// Watchers
// ----------------------
gulp.task('build:watch', ()=> {
watch ( [path.watch.images], (event, cb) => gulp.start('build:images'));
watch ( [path.watch.html], (event, cb) => gulp.start('build:html'));
watch ( [path.watch.js], (event, cb) => gulp.start('build:js'));
watch ( [path.watch.styles], (event, cb) => gulp.start('build:styles'));
watch ( [path.watch.fonts], (event, cb) => gulp.start('build:fonts'));
watch ( [path.watch.base64], (event, cb) => gulp.start('build:base64'));
});
gulp.task('watcher', function(){
console.log('watcher run');
watch (['./sourse/**/*.*'], {}, (event, cb) => console.log(event, cb));
});
// Runners
// ----------------------
/* В зависимости от флагов запускает тот или иной набор задач
*/
gulp.task('build:build', ['build:images', 'build:styles', 'build:js', 'build:fonts'], () => {
if (!argv.server)gulp.start('build:html');
});
gulp.task( 'default',['build:build'], () => {
if (argv.server && argv.proxy && argv.together ){
gulp.start('proxy:server', 'build:watch');
} else if (argv.server && argv.proxy ){
gulp.start('proxy:server', 'build:watch');
} else if(argv.server && !argv.together){
gulp.start('build:watch');
}else {
gulp.start('local:server', 'build:watch');
}
});