/
styles.js
79 lines (70 loc) · 2.01 KB
/
styles.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
const { src, dest } = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const less = require('gulp-less')
const stylus = require('gulp-stylus')
const { source, output } = require('../paths')
const rename = require('gulp-rename')
const plumber = require('gulp-plumber')
const sourcemaps = require('gulp-sourcemaps')
const autoprefixer = require('gulp-autoprefixer')
const cleanCSS = require('gulp-clean-css')
const gulpIf = require('gulp-if')
const { notifyHandler } = require('./plumbers')
const args = require('../args')
const gcmq = require('gulp-group-css-media-queries')
const { wrongConfigParam } = require('../utils')
const compressCss = args.minimize.includes('css')
const handlers = {
sass() {
return sass({
outputStyle: 'expanded',
errLogToConsole: true,
}).on('error', sass.logError)
},
get scss() {
return this.sass
},
less() {
return less()
},
stylus() {
return stylus()
}
}
function stylesInit(browserSyncInstance) {
return function styles() {
let willCompress = args.isProduct && compressCss
if (!args.stylesExt) {
wrongConfigParam('styles_ext')
}
const styleHandler = args.stylesExt === 'css'
? null
: handlers[args.stylesExt]
let stream = src(`${source.styles}/**/*.${args.stylesExt}`)
.pipe(
plumber({
errorHandler: notifyHandler('Styles'),
})
)
.pipe(gulpIf(args.sourceMaps && willCompress, sourcemaps.init()))
if (styleHandler !== null) {
stream = stream.pipe(styleHandler())
}
return stream
.pipe(
gulpIf(
args.isProduct,
autoprefixer({
cascade: false,
})
)
)
.pipe(gulpIf(args.isProduct, gcmq()))
.pipe(gulpIf(willCompress, cleanCSS()))
.pipe(gulpIf(willCompress, rename({ suffix: '.min' })))
.pipe(gulpIf(args.sourceMaps && willCompress, sourcemaps.write('./')))
.pipe(dest(output.css))
.pipe(browserSyncInstance.stream())
}
}
module.exports = stylesInit