-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
145 lines (129 loc) · 5.01 KB
/
gulpfile.js
File metadata and controls
145 lines (129 loc) · 5.01 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
'use strict';
var gulp = require('gulp'),
watch = require('gulp-watch'),
prefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
cssmin = require('gulp-cssmin'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
rigger = require('gulp-rigger'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
rimraf = require('rimraf'),
connect = require('gulp-connect'),
opn = require('opn');
var path = {
build: { //Тут мы укажем куда складывать готовые после сборки файлы
html: 'build/',
js: 'build/js/',
css: 'build/css/',
img: 'build/img/',
fonts: 'build/fonts/'
},
src: { //Пути откуда брать исходники
html: 'src/**/[^_]*.html',//Синтаксис src/**/[^_]*.html говорит gulp,
js: [
'src/js/[^_]*.js',
'bower_components/bootstrap-sass/assets/javascripts/bootstrap.min.js',
'bower_components/jquery/dist/jquery.min.js'
],// что мы хотим взять все файлы с расширением .html, которые НЕ начинаются с подчеркивания
style: 'src/style/[^_]*.scss',//В стилях и скриптах так же
img: 'src/img/**/*.*', //Синтаксис img/**/*.* означает - взять все файлы всех расширений из папки и из вложенных каталогов
fonts: [
'src/fonts/**/*.*',
'bower_components/bootstrap-sass/assets/fonts/**/*.*',
'bower_components/fontawesome/fonts/**/*.*'
]
},
watch: { //Тут мы укажем, за изменением каких файлов мы хотим наблюдать
html: 'src/**/*.html',
js: 'src/js/**/*.js',
style: 'src/style/**/*.scss',
img: 'src/img/**/*.*',
fonts: 'src/fonts/**/*.*'
},
clean: './build'
};
var server = {
host: 'localhost',
port: '9000'
};
gulp.task('build:html', function () {
gulp.src(path.src.html) //Выберем файлы по нужному пути
.pipe(rigger()) //Прогоним через rigger
.pipe(gulp.dest(path.build.html)) //Выплюнем их в папку build
.pipe(connect.reload()); //И перезагрузим наш сервер для обновлений
});
gulp.task('build:js', function () {
gulp.src(path.src.js) //Найдем наш main файл
.pipe(rigger()) //Прогоним через rigger
//.pipe(sourcemaps.init()) //Инициализируем sourcemap
//.pipe(uglify()) //Сожмем наш js
//.pipe(sourcemaps.write()) //Пропишем карты
.pipe(gulp.dest(path.build.js)) //Выплюнем готовый файл в build
.pipe(connect.reload()); //И перезагрузим сервер
});
gulp.task('build:style', function () {
gulp.src(path.src.style) //Выберем наш main.scss
.pipe(sourcemaps.init()) //То же самое что и с js
.pipe(sass({errLogToConsole: true})) //Скомпилируем
.pipe(prefixer()) //Добавим вендорные префиксы
//.pipe(cssmin()) //Сожмем
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.build.css)) //И в build
.pipe(connect.reload());
});
gulp.task('build:image', function () {
gulp.src(path.src.img) //Выберем наши картинки
.pipe(imagemin({ //Сожмем их
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()],
interlaced: true
}))
.pipe(gulp.dest(path.build.img)) //И бросим в build
.pipe(connect.reload());
});
gulp.task('build:fonts', function() {
gulp.src(path.src.fonts)
.pipe(gulp.dest(path.build.fonts))
});
gulp.task('build', [
'build:html',
'build:js',
'build:style',
'build:fonts',
'build:image'
]);
gulp.task('watch', function(){
watch([path.watch.html], function(event, cb) {
gulp.start('build:html');
});
watch([path.watch.style], function(event, cb) {
gulp.start('build:style');
});
watch([path.watch.js], function(event, cb) {
gulp.start('build:js');
});
watch([path.watch.img], function(event, cb) {
gulp.start('build:image');
});
watch([path.watch.fonts], function(event, cb) {
gulp.start('build:fonts');
});
});
gulp.task('webserver', function() {
connect.server({
host: server.host,
port: server.port,
livereload: true
});
});
gulp.task('clear', function (cb) {
rimraf(path.clean, cb);
});
gulp.task('openbrowser', function() {
opn( 'http://' + server.host + ':' + server.port + '/build' );
});
gulp.task('start', ['build', 'webserver', 'watch', 'openbrowser']);
gulp.task('default', ['webserver', 'watch']);