-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
130 lines (115 loc) · 3.1 KB
/
gulpfile.babel.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
'use strict'
const gulp = require('gulp')
const imagemin = require('gulp-imagemin')
const concat = require('gulp-concat')
const sourcemaps = require('gulp-sourcemaps')
const include = require('gulp-include')
const browsersync = require('browser-sync').create()
const { src, series, parallel, dest, watch } = require('gulp')
const cfg = require('./gulp.config')
/** css */
const postcss = require('gulp-postcss')
const preCSS = require('precss')
const rucksack = require('rucksack-css')
const cssnano = require('cssnano')
const cssPresetEnv = require('postcss-preset-env')
const customProperties = require('postcss-custom-properties')
const postcssCustomMedia = require('postcss-custom-media');
const customSelector = require('postcss-custom-selectors')
const autoprefixer = require('autoprefixer')
const processors = [preCSS(), cssPresetEnv(), rucksack(), customSelector(), customProperties(), cssnano(), autoprefixer(), postcssCustomMedia()]
/** js */
const eslint = require('gulp-eslint')
const webpack = require('webpack-stream')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
function handleError(err) {
console.log(err.toString())
this.emit('end')
}
function buildHtml() {
return src(`${cfg.src.main}/*.html`)
.pipe(include())
.on('error', handleError)
.pipe(gulp.dest(cfg.dist.main))
}
function imgTask() {
return src(`${cfg.src.img}/*`).pipe(imagemin()).pipe(gulp.dest(cfg.dist.img))
}
function jsTask() {
return src(`${cfg.src.js.main}/**/*.js`)
.pipe(sourcemaps.init())
.pipe(eslint())
.pipe(eslint.format())
.pipe(
webpack({
mode: 'production',
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
babelrc: false,
presets: ['@babel/preset-env']
}
}
]
},
entry: `${cfg.src.js.main}/app`,
output: {
filename: 'app.js'
},
plugins: [new UglifyJsPlugin()]
})
)
.on('error', handleError)
.pipe(sourcemaps.write('.'))
.pipe(dest(cfg.dist.js))
}
function cssTask() {
return src(`${cfg.src.css}/**/*.css`)
.pipe(sourcemaps.init())
.pipe(concat('style.css'))
.pipe(postcss(processors))
.on('error', handleError)
.pipe(sourcemaps.write('.'))
.pipe(dest(cfg.dist.css))
}
function browsersyncServe(cb) {
browsersync.init({
server: {
baseDir: cfg.dist.main
}
})
cb()
}
function browsersyncReload(cb) {
browsersync.reload()
cb()
}
function watchTask() {
watch([
`${cfg.src.main}/**/*.html`],
{ interval: 1000 },
series(buildHtml, browsersyncReload)
)
watch([
`${cfg.src.img}/*`],
{ interval: 1000 },
imgTask
)
watch([
`${cfg.src.css}/**/*.css`, `${cfg.src.js.main}/**/*.js`],
{ interval: 1000 },
series(parallel(cssTask, jsTask), browsersyncReload)
)
}
exports.cssTask = cssTask
exports.jsTask = jsTask
exports.imgTask = imgTask
exports.buildHtml = buildHtml
exports.default = series(
parallel(buildHtml, imgTask, jsTask, cssTask),
browsersyncServe,
watchTask
)