-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
157 lines (141 loc) · 4.67 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
'use strict';
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
clean = require('gulp-clean'),
cleanCSS = require('gulp-clean-css'),
concat = require('gulp-concat'),
copy = require('gulp-copy'),
csscomb = require('gulp-csscomb'),
imagemin = require('gulp-imagemin'),
less = require('gulp-less'),
rename = require("gulp-rename"),
replace = require('gulp-replace'),
runSequence = require('run-sequence'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
wrap = require('gulp-wrap');
var config = require('./gulpconfig.json');
gulp.task('default', function () {
runSequence(
'build',
'watch'
);
});
gulp.task('build', function () {
runSequence(
'build-clean',
[
'build-fonts',
'build-favicons',
'build-images'
],
[
'build-less',
'build-css',
'build-scripts',
'build-scripts-libraries'
],
[
'build-html'
],
buildEnd
);
});
gulp.task('build-clean', function () {
return gulp.src(config.public, {read: false})
.pipe(clean());
});
gulp.task('build-fonts', function () {
return gulp.src(getFiles(config.fonts.files))
.pipe(gulp.dest(config.public + config.fonts.out));
});
gulp.task('build-favicons', function () {
return gulp.src(getFiles(config.images.favicons.files))
.pipe(imagemin())
.pipe(gulp.dest(config.public + config.images.favicons.out));
});
gulp.task('build-images', function () {
return gulp.src(getFiles(config.images.single.files))
.pipe(imagemin())
.pipe(gulp.dest(config.public + config.images.single.out));
});
gulp.task('build-less', function () {
return gulp.src(getFiles(config.styles.less.files))
.pipe(csscomb())
.pipe(gulp.dest(function (file) {
return file.base;
}))
.pipe(sourcemaps.init())
.pipe(less())
.pipe(autoprefixer({
browsers: config.styles.autoprefixer,
cascade: false
}))
.pipe(cleanCSS())
.pipe(rename(config.styles.less.css))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.public + config.styles.out));
});
gulp.task('build-css', function () {
return gulp.src(getFiles(config.styles.css.files))
.pipe(sourcemaps.init())
.pipe(concat(config.styles.css.css))
.pipe(cleanCSS())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.public + config.styles.out));
});
gulp.task('build-scripts', function () {
return gulp.src(getFiles(config.scripts.app.files))
.pipe(sourcemaps.init())
.pipe(wrap('(function () {\n\'use strict\';\n<%= contents %>\n})();'))
.pipe(concat(config.scripts.app.js))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.public + config.scripts.out));
});
gulp.task('build-scripts-libraries', function () {
return gulp.src(getFiles(config.scripts.vendors.files))
.pipe(sourcemaps.init())
.pipe(concat(config.scripts.vendors.js))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.public + config.scripts.out));
});
gulp.task('build-html', function () {
var now = new Date(),
version = '?v' + now.getTime();
return gulp.src(getFiles(config.html.files))
.pipe(replace('?version', version))
.pipe(gulp.dest(config.public + config.html.out));
});
gulp.task('watch', function () {
gulp.watch(getFiles(config.styles.css.files), ['build-css', 'build-html']);
gulp.watch(getFiles(config.styles.less.watch), ['build-less', 'build-html']);
gulp.watch(getFiles(config.scripts.app.files), ['build-scripts', 'build-html']);
gulp.watch(getFiles(config.scripts.vendors.files), ['build-scripts-libraries', 'build-html']);
gulp.watch(getFiles(config.fonts.files), ['build-fonts']);
gulp.watch(getFiles(config.html.files), ['build-html']);
gulp.watch(getFiles(config.images.favicons.files), ['build-favicons']);
gulp.watch(getFiles(config.images.single.files), ['build-images']);
});
function buildEnd() {
console.log('_____________________________________________________');
console.log('Выполнена очистка и сборка проекта');
console.log('Запущена задача на отслеживание изменений в проекте');
}
function getFiles(files, dir) {
if (dir === undefined) {
dir = config.private;
}
var result;
if (files instanceof Array) {
result = [];
files.forEach(function (item, i) {
result[i] = dir + item;
});
}
else {
result = dir + files;
}
return result;
}