-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
149 lines (124 loc) · 4.06 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'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)
}
let suppressHugoErrors = false;
// --
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', () => {
suppressHugoErrors = true;
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) => {
let baseUrl = process.env.NODE_ENV === 'production' ? process.env.URL : process.env.DEPLOY_PRIME_URL;
let args = baseUrl ? ['-b', baseUrl] : [];
return spawn('hugo', args, { stdio: 'inherit' }).on('close', (code) => {
if (suppressHugoErrors || code === 0) {
browserSync.reload()
cb()
} else {
console.log('hugo command failed.');
cb('hugo command failed.');
}
})
})
gulp.task('hugo-preview', (cb) => {
let args = ['--buildDrafts', '--buildFuture'];
if (process.env.DEPLOY_PRIME_URL) {
args.push('-b')
args.push(process.env.DEPLOY_PRIME_URL)
}
return spawn('hugo', args, { stdio: 'inherit' }).on('close', (code) => {
if (suppressHugoErrors || code === 0) {
browserSync.reload()
cb()
} else {
console.log('hugo command failed.');
cb('hugo command failed.');
}
})
})
// --
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');
})
})