-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
gulpfile.js
190 lines (166 loc) · 4.76 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
const { series, watch, src, dest, parallel } = require('gulp')
const pump = require('pump')
const del = require('del')
const rename = require('gulp-rename')
const replace = require('gulp-replace')
// gulp plugins and utils
const livereload = require('gulp-livereload')
const beeper = require('beeper')
const postcss = require('gulp-postcss')
const zip = require('gulp-zip')
const gulpif = require('gulp-if')
const sourcemaps = require('gulp-sourcemaps')
const header = require('gulp-header')
// Babel
const browserify = require('browserify')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const uglify = require('gulp-uglify')
//
const merge = require('merge-stream')
// postcss plugins
const cssnano = require('cssnano')
const autoprefixer = require('autoprefixer')
const comments = require('postcss-discard-comments')
const tailwindcss = require('tailwindcss')
// sass
const sass = require('gulp-sass')(require('sass'))
// environment
const isProduction = process.argv.includes('--production') || process.env.NODE_ENV === 'production'
// Data collection on the theme
const { name, version, author, license, repository } = require('./package.json')
// Build Comments
const BuildComments = `/*!
* ${name} v${version}
* Copyright ${new Date().getFullYear()} ${author.name} <${author.email}> (${repository.url})
* Licensed under ${license}
*/`
// clean assets
const clean = () => {
return del([
'assets',
'partials/main-styles.hbs',
'dist'
], { force: true })
}
function serve (done) {
livereload.listen()
done()
}
const handleError = done => {
return function (err) {
if (err) {
beeper()
}
return done(err)
}
}
// hbs
function hbs (done) {
pump([
src([
'*.hbs',
'partials/**/*.hbs'
]),
livereload()
], handleError(done))
}
// style
function styles (done) {
pump([
src('src/sass/*.sass'),
gulpif(!isProduction, sourcemaps.init()),
sass({ outputStyle: 'expanded' }).on('error', sass.logError),
gulpif(!isProduction, postcss([tailwindcss()])),
gulpif(isProduction, postcss([tailwindcss(), autoprefixer(), cssnano(), comments({ removeAll: true })])),
gulpif(isProduction, header(BuildComments)),
gulpif(!isProduction, sourcemaps.write('./map')),
dest('assets/styles'),
livereload()
], handleError(done))
}
// Scripts
function scripts (done) {
const files = ['main', 'post', 'search', 'prismjs', 'pagination']
merge(files.map(function (file) {
return pump([
browserify({
basedir: '.',
debug: true,
entries: `src/js/${file}.js`,
cache: {},
packageCache: {}
}).transform('babelify', {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime']
}).bundle(),
source(`${file}.js`),
buffer(),
gulpif(!isProduction, sourcemaps.init()),
gulpif(isProduction, uglify()),
gulpif(isProduction, header(BuildComments)),
gulpif(!isProduction, sourcemaps.write('./map')),
dest('assets/scripts'),
livereload()
], handleError(done))
}))
}
// Image
function images (done) {
pump([
src('./src/img/**/*.*'),
dest('assets/images'),
livereload()
], handleError(done))
}
function copyAmpStyle (done) {
pump([
src('assets/styles/amp.css'),
replace('@charset "UTF-8";', ''),
postcss([cssnano(), comments({ removeAll: true })]),
rename('amp-styles.hbs'),
dest('partials/amp')
], handleError(done))
}
function copyMainStyle (done) {
pump([
src('assets/styles/main.css'),
replace('@charset "UTF-8";', ''),
postcss([cssnano(), comments({ removeAll: true })]),
rename('main-styles.hbs'),
dest('partials')
], handleError(done))
}
// ZIP
function zipper (done) {
const filename = `${name}-v${version}.zip`
pump([
src([
'**',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!src', '!src/**',
'!docs', '!docs/**',
'!gulpfile.js',
'!.stylelintrc.json',
'!tailwind.config.js',
'!routes.yaml',
'!CONTRIBUTING.md',
'!yarn-error.log',
'!yarn.lock'
]),
zip(filename),
dest('dist/')
], handleError(done))
}
const cssWatcher = () => watch('src/sass/**', styles)
const jsWatcher = () => watch('src/js/**', scripts)
const imgWatcher = () => watch('src/img/**', images)
const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], hbs)
const compile = parallel(styles, scripts, images)
// const watcher = parallel(cssWatcher, jsWatcher, imgWatcher)
const watcher = parallel(cssWatcher, jsWatcher, imgWatcher, hbsWatcher)
const build = series(clean, compile)
const production = series(build, copyAmpStyle, copyMainStyle, zipper)
const development = series(build, serve, watcher)
module.exports = { build, development, production }