-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
139 lines (125 loc) · 3.78 KB
/
gulpfile.js
File metadata and controls
139 lines (125 loc) · 3.78 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
const gulp = require('gulp')
const plumber = require('gulp-plumber')
const notify = require('gulp-notify')
const gutil = require('gulp-util')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const pug = require('gulp-pug')
const inlinesource = require('gulp-inline-source')
const htmlmin = require('gulp-htmlmin');
const compress = require('compression')
const sass = require('gulp-sass')(require('sass'))
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const minifyCSS = require('gulp-csso')
const sourcemaps = require('gulp-sourcemaps')
const browserify = require('browserify')
const babelify = require('babelify')
const uglify = require('gulp-uglify')
const browserSync = require('browser-sync').create()
gulp.task('pug', function(){
return gulp.src('templates/page/*.pug')
.pipe(plumber({errorHandler: notify.onError({
message: "<%= error.message %>",
title: "Template compilation"
})}))
.pipe(pug({
basedir: '.'
}))
.pipe(gulp.dest('.'))
.pipe(browserSync.stream())
})
gulp.task('scss', function(){
return gulp.src('css/src/**/*.scss')
.pipe(plumber({errorHandler: notify.onError({
message: "<%= error.message %>",
title: "CSS preprocessing"
})}))
.pipe(sass())
.pipe(gulp.dest('css'))
.pipe(browserSync.stream({match: '**/*.css'}))
})
gulp.task('js', () => {
return browserify('./js/src/main.js', {debug: true})
.transform(babelify.configure({
presets: ["@babel/preset-env"], sourceMaps: true
}))
.on('error', notify.onError({
message: "<%= error.message %>",
title: "Babelify JS"
}))
.bundle()
.on('error', notify.onError({
message: "<%= error.message %>",
title: "JS compilation"
}))
.pipe(source('main.js'))
.pipe(buffer())
.pipe(gulp.dest('js'))
.pipe(browserSync.stream())
})
gulp.task('minifyCss', gulp.series('scss', function(){
return gulp.src('css/*.css')
.pipe(plumber({errorHandler: notify.onError({
message: "<%= error.message %>",
title: "CSS preprocessing"
})}))
.pipe(sourcemaps.init())
.pipe(postcss([autoprefixer()]))
.pipe(minifyCSS())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('css'))
}))
gulp.task('minifyHtml', gulp.series('minifyCss', 'pug', function(){
return gulp.src('./*.html')
.pipe(plumber({errorHandler: notify.onError({
message: "<%= error.message %>",
title: "Template compilation"
})}))
.pipe(inlinesource({rootpath: './'}))
.pipe(htmlmin({
removeComments: true,
collapseWhitespace: true,
sortAttributes: true,
sortClassName: true
}))
.pipe(gulp.dest('.'))
}))
gulp.task('minifyJs', () => {
return browserify('./js/src/main.js')
.transform(babelify.configure({
presets: ["@babel/preset-env"]
}))
.on('error', notify.onError({
message: "<%= error.message %>",
title: "Babelify JS"
}))
.bundle()
.on('error', notify.onError({
message: "<%= error.message %>",
title: "JS compilation"
}))
.pipe(source('main.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('js'))
})
gulp.task('serve', function() {
browserSync.init({
server: "./",
middleware: [compress()],
open: false
})
let watcher = {
scss : gulp.watch('css/src/**/*.scss'),
js : gulp.watch('js/src/**/*.js'),
pug : gulp.watch('templates/**/*.pug'),
assets : gulp.watch('img/**/*.svg')
}
watcher.scss.on('all', gulp.parallel('scss'))
watcher.js.on('all', gulp.parallel('js'))
watcher.pug.on('all', gulp.parallel('pug'))
watcher.assets.on('all', gulp.parallel('pug'))
})
gulp.task('build', gulp.parallel('minifyHtml', 'minifyCss', 'minifyJs'))
gulp.task('default', gulp.parallel( 'serve', 'js', 'scss', 'pug'))