-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
124 lines (105 loc) · 3.37 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
'use strict'
import gulp from 'gulp'
import del from 'del'
import runSequence from 'run-sequence'
import gulpLoadPlugins from 'gulp-load-plugins'
import { spawn } from "child_process"
import tildeImporter from 'node-sass-tilde-importer'
const $ = gulpLoadPlugins()
const browserSync = require('browser-sync').create()
const isProduction = process.env.NODE_ENV === 'production'
const onError = (err) => {
console.log(err)
}
// --
gulp.task('server', ['build'], () => {
gulp.start('init-watch')
$.watch(['archetypes/**/*', 'data/**/*', 'content/**/*', 'layouts/**/*', 'static/**/*', 'config.toml'], () => gulp.start('hugo'))
});
gulp.task('server:with-drafts', ['build-preview'], () => {
gulp.start('init-watch')
$.watch(['archetypes/**/*', 'data/**/*', 'content/**/*', 'layouts/**/*', 'static/**/*', 'config.toml'], () => gulp.start('hugo-preview'))
});
gulp.task('init-watch', () => {
browserSync.init({
server: {
baseDir: 'public'
},
open: false
})
$.watch('src/sass/**/*.scss', () => gulp.start('sass'))
$.watch('src/js/**/*.js', () => gulp.start('js-watch'))
$.watch('src/images/**/*', () => gulp.start('images'))
})
gulp.task('build', () => {
runSequence(['sass', 'js', 'fonts', 'images', 'pub-delete'], 'hugo')
})
gulp.task('build-preview', () => {
runSequence(['sass', 'js', 'fonts', 'images', 'pub-delete'], 'hugo-preview')
})
gulp.task('hugo', (cb) => {
return spawn('hugo', { stdio: 'inherit' }).on('close', (code) => {
browserSync.reload()
cb()
})
})
gulp.task('hugo-preview', (cb) => {
return spawn('hugo', ['--buildDrafts', '--buildFuture'], { stdio: 'inherit' }).on('close', (code) => {
browserSync.reload()
cb()
})
})
// --
gulp.task('sass', () => {
return gulp.src([
'src/sass/**/*.scss'
])
.pipe($.plumber({ errorHandler: onError }))
.pipe($.print())
.pipe($.sassLint())
.pipe($.sassLint.format())
.pipe($.sass({ precision: 5, importer: tildeImporter }))
.pipe($.autoprefixer(['ie >= 10', 'last 2 versions']))
.pipe($.if(isProduction, $.cssnano({ discardUnused: false, minifyFontValues: false })))
.pipe($.size({ gzip: true, showFiles: true }))
.pipe(gulp.dest('static/css'))
.pipe(browserSync.stream())
})
gulp.task('js-watch', ['js'], (cb) => {
browserSync.reload();
cb();
});
gulp.task('js', () => {
return gulp.src([
'src/js/**/*.js'
])
.pipe($.plumber({ errorHandler: onError }))
.pipe($.print())
.pipe($.babel())
.pipe($.concat('app.js'))
.pipe($.if(isProduction, $.uglify()))
.pipe($.size({ gzip: true, showFiles: true }))
.pipe(gulp.dest('static/js'))
})
gulp.task('fonts', () => {
return gulp.src('src/fonts/**/*.{woff,woff2}')
.pipe(gulp.dest('static/fonts'));
});
gulp.task('images', () => {
return gulp.src('src/images/**/*.{png,jpg,jpeg,gif,svg,webp,ico}')
.pipe($.newer('static/images'))
.pipe($.print())
.pipe($.imagemin())
.pipe(gulp.dest('static/images'));
});
gulp.task('cms-delete', () => {
return del(['static/admin'], { dot: true })
})
gulp.task('pub-delete', () => {
return del(['public/**', '!public'], {
// dryRun: true,
dot: true
}).then(paths => {
console.log('Files and folders deleted:\n', paths.join('\n'), '\nTotal Files Deleted: ' + paths.length + '\n');
})
})