generated from flyntwp/flynt
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
124 lines (115 loc) · 4.4 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
const gulp = require('gulp')
const config = require('./build-config').gulp
function replaceVersion (config) {
gulp.task('replaceVersion', function (cb) {
const log = require('fancy-log')
const colors = require('ansi-colors')
const PluginError = require('plugin-error')
const pjson = require('./package.json')
const replace = require('replace-in-file')
try {
// read current version from package.json
log(`Replacing ${config.replaceVersion.php.from} with ${config.replaceVersion.php.to} in all PHP files.`)
config.replaceVersion.php.to = pjson.version
const changedFilesPhp = replace.sync(config.replaceVersion.php)
for (const file of changedFilesPhp) {
log(`Updated ${file}`)
}
log(`Replacing ${config.replaceVersion.js.from} with ${config.replaceVersion.js.to} in all JS files.`)
config.replaceVersion.js.to = pjson.version
const changedFilesJs = replace.sync(config.replaceVersion.js)
for (const file of changedFilesJs) {
log(`Updated ${file}`)
}
// replace WordPress theme version in style.css
log('Updating WordPress theme version.')
config.replaceVersion.wordpress.to += pjson.version
const changedFilesWp = replace.sync(config.replaceVersion.wordpress)
if (changedFilesWp.length > 0) {
for (const file of changedFilesWp) {
log(`Updated ${file}`)
}
} else {
log(colors.yellow('No changes made! Was the version already changed?'))
}
} catch (error) {
throw new PluginError('replaceVersion', error)
}
cb()
})
}
function revAssets (config) {
// 1) Add md5 hashes to assets referenced by CSS and JS files
gulp.task('revAssets', function () {
const path = require('path')
const rev = require('gulp-rev')
const revNapkin = require('gulp-rev-napkin')
// Ignore files that may reference assets. We'll rev them next.
return gulp.src(config.rev.assetSrc)
.pipe(rev())
.pipe(gulp.dest(config.dest))
.pipe(revNapkin({ verbose: false }))
.pipe(rev.manifest(path.join(config.dest, 'rev-manifest.json'), { merge: true, base: config.dest }))
.pipe(gulp.dest(config.dest))
})
}
function revRevvedFiles (config) {
// 3) Rev and compress CSS and JS files (this is done after assets, so that if a
// referenced asset hash changes, the parent hash will change as well
gulp.task('revRevvedFiles', function () {
const path = require('path')
const rev = require('gulp-rev')
const revNapkin = require('gulp-rev-napkin')
return gulp.src(config.rev.srcRevved)
.pipe(rev({
replaceInExtensions: config.rev.revvedFileExtensions
}))
.pipe(gulp.dest(config.dest))
.pipe(revNapkin({ verbose: false }))
.pipe(rev.manifest(path.join(config.dest, 'rev-manifest.json'), { merge: true, base: config.dest }))
.pipe(gulp.dest(config.dest))
})
}
function revStaticFiles (config) {
// 4) Update asset references in HTML
gulp.task('revStaticFiles', function () {
const path = require('path')
const rewrite = require('gulp-rev-rewrite')
var manifest = gulp.src(path.join(config.dest, '/rev-manifest.json'))
return gulp.src(config.rev.srcStatic)
.pipe(rewrite({
manifest: manifest,
replaceInExtensions: config.rev.staticFileExtensions
}))
.pipe(gulp.dest(config.dest))
})
}
function revUpdateReferences (config) {
// 2) Update asset references with reved filenames in compiled css + js
gulp.task('revUpdateReferences', function () {
const path = require('path')
const rewrite = require('gulp-rev-rewrite')
var manifest = gulp.src(path.join(config.dest, 'rev-manifest.json'))
return gulp.src(config.rev.srcRevved)
.pipe(rewrite({ manifest: manifest }))
.pipe(gulp.dest(config.dest))
})
}
function rev () {
gulp.task('rev', gulp.series([
// 1) Add md5 hashes to assets referenced by CSS and JS files
'revAssets',
// 2) Update asset references (images, fonts, etc) with reved filenames in compiled css + js
'revUpdateReferences',
// 3) Rev and compress CSS and JS files (this is done after assets, so that if a referenced asset hash changes, the parent hash will change as well
'revRevvedFiles',
// 4) Update asset references in HTML
'revStaticFiles'
]))
}
replaceVersion(config)
revAssets(config)
revRevvedFiles(config)
revStaticFiles(config)
revUpdateReferences(config)
rev()