-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
236 lines (189 loc) · 5.16 KB
/
gulpfile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Gulp.js configuration.
'use strict'
const
// Gulp and plugins.
gulp = require('gulp'),
newer = require('gulp-newer'),
hb = require('gulp-hb'),
htmlmin = require('gulp-htmlmin'),
imagemin = require('gulp-imagemin'),
webp = require('gulp-webp'),
autoprefixer = require('gulp-autoprefixer'),
sass = require('gulp-sass'),
cleanCSS = require('gulp-clean-css'),
babel = require('gulp-babel'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
sourcemaps = require('gulp-sourcemaps'),
rename = require('gulp-rename'),
gutil = require('gulp-util'),
// Source and build folders.
dir = {
src: './src',
build: './public'
}
var browsersync = false
// -------------------------------------------------------------------
// HTML
const html = {
src : dir.src + '/index.hbs',
partials: dir.src + '/partials/**/*.hbs',
build : dir.build,
}
const compileMarkup = () => {
return gulp.src(html.src)
.pipe(newer(html.build))
.pipe(hb().partials(html.partials))
.pipe(rename('index.html'))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest(html.build))
}
const watchMarkup = () => {
let watch = gulp.watch(html.src)
watch.on('all', () => {
compileMarkup()
browsersync ? browsersync.reload : {}
})
}
// -------------------------------------------------------------------
// Styles
var css = {
src : dir.src + '/sass/**/*',
build: dir.build + '/css'
}
const compileStyle = () => {
return gulp.src(css.src)
.pipe(newer(css.build))
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
// .pipe(sourcemaps.init())
.pipe(cleanCSS())
.pipe(concat('bundle.css'))
// .pipe(sourcemaps.write())
.pipe(gulp.dest(css.build))
.pipe(browsersync ? browsersync.reload({ stream: true }) : gutil.noop())
}
const watchStyle = () => {
let watch = gulp.watch(css.src)
watch.on('all', () => {
compileStyle()
})
}
// -------------------------------------------------------------------
// Scripts
const js = {
src: dir.src + '/js/**/*',
build: dir.build + '/js'
}
const compileScript = () => {
return gulp.src([
'./src/js/lazysizes.min.js',
'./src/js/index.js'
])
.pipe(newer(js.build))
.pipe(babel({
presets: ['@babel/env']
}))
// .pipe(sourcemaps.init())
.pipe(uglify())
.pipe(concat('bundle.js'))
// .pipe(sourcemaps.write())
.pipe(gulp.dest(js.build))
.pipe(browsersync ? browsersync.reload({ stream: true }) : gutil.noop())
}
const watchScript = () => {
let watch = gulp.watch(js.src)
watch.on('all', () => {
compileScript()
})
}
// -------------------------------------------------------------------
// Images
const img = {
src : dir.src + '/img/**/*',
build: dir.build + '/img'
}
const compileImage = () => {
return gulp.src(img.src)
.pipe(newer(img.build))
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false}
]
})
]))
.pipe(gulp.dest(img.build))
.pipe(browsersync ? browsersync.reload({ stream: true }) : gutil.noop())
}
const watchImage = () => {
let watch = gulp.watch(img.src)
watch.on('all', () => {
compileImage()
})
}
// Anime Covers
const covers = {
src : dir.src + '/img/covers/**/*',
build: dir.build + '/img/covers'
}
const compileCover = () => {
return gulp.src(covers.src)
// .pipe(newer(covers.build))
.pipe(webp())
.pipe(gulp.dest(covers.build))
// .pipe(browsersync ? browsersync.reload({ stream: true }) : gutil.noop())
}
// -------------------------------------------------------------------
// JSON
const json = {
src : './anime.json',
build : dir.build,
}
const compileJSON = () => {
return gulp.src(json.src)
.pipe(newer(json.build))
.pipe(gulp.dest(json.build))
}
// -------------------------------------------------------------------
// BrowserSync
const syncOpts = {
files : dir.build + '**/*',
open: false,
server: {
baseDir: dir.build,
}
}
const startServer = () => {
browsersync = require('browser-sync').create()
browsersync.init(syncOpts)
}
// -------------------------------------------------------------------
const compile = gulp.parallel(compileMarkup, compileScript, compileStyle, compileCover, compileImage, compileJSON)
compile.description = 'compile all sources'
const serve = gulp.series(compile, startServer)
serve.description = 'serve compiled source on local server at port 3000'
const watch = gulp.parallel(watchMarkup, watchScript, watchStyle, watchImage)
watch.description = 'watch for changes to all source'
const defaultTasks = gulp.parallel(serve, watch)
defaultTasks.description = 'serve & watch for changes to all source'
module.exports = {
compile,
compileMarkup,
compileScript,
compileStyle,
compileImage,
compileCover,
compileJSON,
serve,
watch,
watchMarkup,
watchScript,
watchStyle,
watchImage,
default: defaultTasks
}