-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
gulpfile.js
205 lines (179 loc) · 5.12 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
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 simpleExtend = require('postcss-extend')
const tailwindcss = require('tailwindcss')
// const lol = require('postcss-advanced-variables')
const postImport = require('postcss-import')
const precss = require('precss')
const postNesting = require('tailwindcss/nesting') // postcss-nested
// 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/styles',
'dis'
], { 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))
// }
const postcssPluginsDev = [
postImport(),
simpleExtend(),
precss(),
postNesting(),
tailwindcss()
]
const postcssPluginsPro = [
autoprefixer(),
cssnano(),
comments({ removeAll: true })
]
// style
function styles (done) {
pump([
src('src/css/*.css'),
gulpif(!isProduction, sourcemaps.init()),
// sass({ outputStyle: 'expanded' }).on('error', sass.logError),
gulpif(!isProduction, postcss(postcssPluginsDev)),
gulpif(isProduction, postcss(postcssPluginsDev.concat(postcssPluginsPro))),
gulpif(isProduction, header(BuildComments)),
gulpif(!isProduction, sourcemaps.write('./map')),
dest('assets/styles'),
livereload()
], handleError(done))
}
// Scripts
function scripts (done) {
const files = ['main']
merge(files.map(function (file) {
return pump([
browserify({
basedir: '.',
debug: true,
entries: `./src/js/${file}.js`,
cache: {},
packageCache: {}
}).transform('babelify', {
presets: ['@babel/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([
'assets/**',
'locales/*.json',
'*.hbs',
'partials/**',
'LICENSE',
'package.json',
'README.md',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!src', '!src/**'
], { base: '.' }),
zip(filename),
dest('dist')
], handleError(done))
}
const cssWatcher = () => watch('src/css/**', styles)
const jsWatcher = () => watch(['src/js/**', '*.js'], scripts)
const imgWatcher = () => watch('src/img/**', images)
// const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], hbs)
const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], styles)
const compile = parallel(styles, scripts, images)
const watcher = parallel(cssWatcher, jsWatcher, imgWatcher, hbsWatcher)
const build = series(clean, compile)
const production = series(build, copyAmpStyle, copyMainStyle, zipper)
// const production = series(build)
const development = series(build, serve, watcher)
module.exports = { build, development, production }