1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
var gulp = require('gulp'), |
4
|
|
|
postCss = require('gulp-postcss'), |
5
|
|
|
reporter = require('postcss-reporter'), |
6
|
|
|
styleLint = require('stylelint'), |
7
|
|
|
postScss = require('postcss-scss'), |
8
|
|
|
watch = require('gulp-watch'), |
9
|
|
|
prefixer = require('gulp-autoprefixer'), |
10
|
|
|
sass = require('gulp-sass'), |
11
|
|
|
sourceMaps = require('gulp-sourcemaps'), |
12
|
|
|
cssImport = require('gulp-cssimport'), |
13
|
|
|
cssMin = require('gulp-clean-css'), |
14
|
|
|
uglify = require('gulp-uglify'), |
15
|
|
|
imageMin = require('gulp-imagemin'), |
16
|
|
|
pngQuant = require('imagemin-pngquant'), |
17
|
|
|
gulpIf = require('gulp-if'), |
18
|
|
|
rigger = require('gulp-rigger'), |
19
|
|
|
browserSync = require('browser-sync'), |
20
|
|
|
runSequence = require('run-sequence'), |
21
|
|
|
rimraf = require('rimraf'), |
22
|
|
|
argv = require('yargs').argv, |
23
|
|
|
reload = browserSync.reload, |
24
|
|
|
prod = argv.prod, |
25
|
|
|
dev = !argv.prod; |
26
|
|
|
|
27
|
|
|
var path = { |
28
|
|
|
build: { |
29
|
|
|
html: 'build/', |
30
|
|
|
js: 'build/js/', |
31
|
|
|
styles: 'build/styles/', |
32
|
|
|
img: 'build/img/', |
33
|
|
|
fonts: 'build/fonts/' |
34
|
|
|
}, |
35
|
|
|
src: { |
36
|
|
|
html: 'src/**/*.html', |
37
|
|
|
js: 'src/js/main.js', |
38
|
|
|
styles: 'src/styles/main.scss', |
39
|
|
|
img: 'src/img/**/*.*', |
40
|
|
|
fonts: 'src/fonts/**/*.*' |
41
|
|
|
}, |
42
|
|
|
watch: { |
43
|
|
|
html: 'src/**/*.html', |
44
|
|
|
js: 'src/js/**/*.js', |
45
|
|
|
styles: 'src/styles/**/*.scss', |
46
|
|
|
img: 'src/img/**/*.*', |
47
|
|
|
fonts: 'src/fonts/**/*.*' |
48
|
|
|
}, |
49
|
|
|
clean: './build' |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
gulp.task('webServer', function () { |
53
|
|
|
var config = { |
54
|
|
|
server: { |
55
|
|
|
baseDir: "./build" |
56
|
|
|
}, |
57
|
|
|
// tunnel: true, |
58
|
|
|
host: 'localhost', |
59
|
|
|
port: 9000, |
60
|
|
|
logPrefix: "frontend" |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
browserSync(config); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
gulp.task('clean', function (cb) { |
67
|
|
|
rimraf(path.clean, cb); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
gulp.task('html:build', function () { |
71
|
|
|
gulp.src(path.src.html) |
72
|
|
|
.pipe(rigger()) |
73
|
|
|
.pipe(reload({stream: true})) |
74
|
|
|
.pipe(gulp.dest(path.build.html)); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
gulp.task('js:build', function () { |
78
|
|
|
gulp.src(path.src.js) |
79
|
|
|
.pipe(rigger()) |
80
|
|
|
.pipe(gulpIf(dev, (sourceMaps.init()))) |
81
|
|
|
.pipe(gulpIf(prod, uglify())) |
82
|
|
|
.pipe(gulpIf(dev, (sourceMaps.write()))) |
83
|
|
|
.pipe(gulp.dest(path.build.js)) |
84
|
|
|
.pipe(reload({stream: true})); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
gulp.task('scss-lint', function () { |
88
|
|
|
var config = require('./stylelintrc.config.js'); |
89
|
|
|
var processors = [ |
90
|
|
|
styleLint(config), |
91
|
|
|
reporter({ |
92
|
|
|
clearAllMessages: true |
93
|
|
|
}) |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
gulp.src('src/styles/**/*.scss') |
97
|
|
|
.pipe(gulpIf(dev, postCss(processors, {syntax: postScss}))); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
gulp.task('styles:build', function () { |
101
|
|
|
gulp.src(path.src.styles) |
102
|
|
|
.pipe(gulpIf(dev, sourceMaps.init())) |
103
|
|
|
.pipe(sass({ |
104
|
|
|
includePaths: ['src/styles/'], |
105
|
|
|
outputStyle: 'compressed', |
106
|
|
|
sourceMap: true, |
107
|
|
|
errLogToConsole: true |
108
|
|
|
})) |
109
|
|
|
.pipe(cssImport()) |
110
|
|
|
.pipe(prefixer()) |
111
|
|
|
.pipe(cssMin()) |
112
|
|
|
.pipe(gulpIf(dev, sourceMaps.write())) |
113
|
|
|
.pipe(gulp.dest(path.build.styles)) |
114
|
|
|
.pipe(reload({stream: true})); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
gulp.task('image:build', function () { |
118
|
|
|
gulp.src(path.src.img) |
119
|
|
|
.pipe(gulpIf(prod, imageMin({ |
120
|
|
|
progressive: true, |
121
|
|
|
svgoPlugins: [{removeViewBox: false}], |
122
|
|
|
use: [pngQuant()], |
123
|
|
|
interlaced: true |
124
|
|
|
}))) |
125
|
|
|
.pipe(gulp.dest(path.build.img)); |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
gulp.task('fonts:build', function() { |
129
|
|
|
gulp.src(path.src.fonts) |
130
|
|
|
.pipe(gulp.dest(path.build.fonts)); |
131
|
|
|
}); |
132
|
|
|
|
133
|
|
|
gulp.task('build', function (cb) { |
134
|
|
|
runSequence('clean',[ |
135
|
|
|
'html:build', |
136
|
|
|
'js:build', |
137
|
|
|
'scss-lint', |
138
|
|
|
'styles:build', |
139
|
|
|
'fonts:build', |
140
|
|
|
'image:build' |
141
|
|
|
], cb) |
142
|
|
|
}); |
143
|
|
|
|
144
|
|
|
gulp.task('watch', function(){ |
145
|
|
|
watch([path.watch.html], function() { |
146
|
|
|
gulp.start('html:build'); |
147
|
|
|
}); |
148
|
|
|
watch([path.watch.js], function() { |
149
|
|
|
gulp.start('js:build'); |
150
|
|
|
}); |
151
|
|
|
watch([path.watch.styles], function() { |
152
|
|
|
gulp.start('scss-lint'); |
153
|
|
|
}); |
154
|
|
|
watch([path.watch.styles], function() { |
155
|
|
|
gulp.start('styles:build'); |
156
|
|
|
}); |
157
|
|
|
watch([path.watch.img], function() { |
158
|
|
|
gulp.start('image:build'); |
159
|
|
|
}); |
160
|
|
|
watch([path.watch.fonts], function() { |
161
|
|
|
gulp.start('fonts:build'); |
162
|
|
|
}); |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
gulp.task('default', function (cb) { |
166
|
|
|
runSequence('build',[ |
167
|
|
|
'webServer', |
168
|
|
|
'watch' |
169
|
|
|
], cb); |
170
|
|
|
}); |