-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathgulpfile.js
More file actions
241 lines (177 loc) · 5.33 KB
/
gulpfile.js
File metadata and controls
241 lines (177 loc) · 5.33 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
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
237
238
239
240
241
const fs = require('fs')
const path = require('path')
const sharp = require('sharp')
const { Transform } = require('stream')
const { finished } = require('stream/promises')
const {task, src, dest, parallel, series, watch} = require('gulp')
// general
const config = require('./gulpconfig.js')
// html
const pug = require('gulp-pug')
// css
const sass = require('gulp-sass')(require('sass'))
// js
const babel = require('gulp-babel')
const terser = require('gulp-terser')
let autoprefixerPromise
function loadAutoprefixer () {
if (!autoprefixerPromise) {
autoprefixerPromise = import('gulp-autoprefixer').then(module => module.default)
}
return autoprefixerPromise
}
function renameFiles (transformPath) {
return new Transform({
objectMode: true,
transform (file, enc, callback) {
file.path = transformPath(file.path)
callback(null, file)
}
})
}
function filterFiles (predicate) {
return new Transform({
objectMode: true,
transform (file, enc, callback) {
if (predicate(file)) {
callback(null, file)
return
}
callback()
}
})
}
function setExtension (filePath, extension) {
const parsed = path.parse(filePath)
return path.join(parsed.dir, `${parsed.name}${extension}`)
}
function toHiddenConfig (filePath) {
const parsed = path.parse(filePath)
return path.join(parsed.dir, `.${parsed.name}`)
}
function convertToWebp () {
return new Transform({
objectMode: true,
transform (file, enc, callback) {
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
callback(new Error('Streaming image conversion is not supported'))
return
}
sharp(file.path)
.webp()
.toBuffer()
.then(buffer => {
file.contents = buffer
file.path = setExtension(file.path, '.webp')
callback(null, file)
})
.catch(error => {
callback(new Error(`${file.path}: ${error.message}`))
})
}
})
}
async function walkFiles (directory) {
const entries = await fs.promises.readdir(directory, { withFileTypes: true })
const files = await Promise.all(entries.map(async entry => {
const entryPath = path.join(directory, entry.name)
if (entry.isDirectory()) {
return walkFiles(entryPath)
}
return entryPath
}))
return files.flat()
}
async function copyFiles (files, sourceRoot, destinationRoot) {
await Promise.all(files.map(async file => {
const relativePath = path.relative(sourceRoot, file)
const destinationPath = path.join(destinationRoot, relativePath)
await fs.promises.mkdir(path.dirname(destinationPath), { recursive: true })
await fs.promises.copyFile(file, destinationPath)
}))
}
task('configText', () => {
return src(config.config.text.src)
.pipe(dest(config.config.text.dest))
})
task('configHidden', () => {
return src(config.config.hidden.src, { allowEmpty: true })
.pipe(filterFiles(file => path.extname(file.path) !== '.txt'))
.pipe(renameFiles(toHiddenConfig))
.pipe(dest(config.config.hidden.dest))
})
task('config', parallel('configText', 'configHidden'))
task('themes', () => {
return src(config.themes.src)
.pipe(dest(config.themes.dest))
})
task('pug', () => {
return src(config.pug.src)
.pipe(pug(config.pug.opts.pug))
.pipe(dest(config.pug.dest))
})
task('sass', async () => {
const autoprefixer = await loadAutoprefixer()
const stream = src(config.sass.src)
.pipe(sass(config.sass.opts.sass).on('error', sass.logError))
.pipe(autoprefixer(config.sass.opts.autoprefixer))
.pipe(renameFiles(filePath => setExtension(filePath, config.sass.ext)))
.pipe(dest(config.sass.dest))
await finished(stream)
})
task('js', () => {
return src(config.js.src)
.pipe(babel(config.js.opts.babel))
.pipe(dest(config.js.dest))
.pipe(terser())
.pipe(renameFiles(filePath => setExtension(filePath, config.js.ext)))
.pipe(dest(config.js.dest))
})
task('convertImages', () => {
return src(config.images.toConvert)
.pipe(convertToWebp())
.pipe(dest(config.images.dest))
})
task('copyImages', () => {
return walkFiles(config.images.source)
.then(files => files.filter(file => {
const extension = path.extname(file).toLowerCase()
return config.images.copyExtensions.includes(extension)
}))
.then(files => copyFiles(files, config.images.source, config.images.dest))
})
task('copyFavicon', () => {
const destinationPath = path.join(config.images.faviconDest, path.basename(config.images.favicon))
return fs.promises.mkdir(path.dirname(destinationPath), { recursive: true })
.then(() => fs.promises.copyFile(config.images.favicon, destinationPath))
})
task('images', parallel('convertImages', 'copyImages', 'copyFavicon'))
task('fonts', () => {
const fontsPath = path.join(config.watchSource, 'fonts')
if (!fs.existsSync(fontsPath)) {
return Promise.resolve()
}
return src(config.fonts.src)
.pipe(dest(config.fonts.dest))
})
task('default', parallel(
'config',
'pug',
'sass',
'js',
'images',
'fonts',
'themes'
))
task('watchPug', () => { watch(`${config.watchSource}/pug/**/*`, series('pug')) })
task('watchSass', () => { watch(`${config.watchSource}/scss/**/*`, series('sass')) })
task('watchJS', () => { watch(`${config.watchSource}/js/**/*`, series('js')) })
task('watch', parallel(
'watchPug',
'watchSass',
'watchJS'
))