-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
191 lines (176 loc) · 5.19 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
const { src, dest, task, watch, series, parallel } = require('gulp')
const del = require('del')
const options = require('./config')
const browserSync = require('browser-sync').create()
const sass = require('gulp-sass')
const postcss = require('gulp-postcss')
const concat = require('gulp-concat')
const uglify = require('gulp-terser')
const imagemin = require('gulp-imagemin')
const cleanCSS = require('gulp-clean-css')
const purgeCSS = require('gulp-purgecss')
const logSymbols = require('log-symbols')
const panini = require('panini')
const browserify = require('browserify')
const babelify = require('babelify')
const source = require('vinyl-source-stream')
const eslint = require('gulp-eslint')
function livePreview(done) {
browserSync.init({
server: {
baseDir:
process.env.NODE_ENV === 'production'
? options.paths.build.base
: options.paths.dist.base,
},
port: options.config.port || 5000,
})
done()
}
function previewReload(done) {
console.log('\n\t' + logSymbols.info, 'Reloading Browser Preview.\n')
browserSync.reload()
done()
}
function compileHTML() {
console.log('\n\t' + logSymbols.info, 'Compiling HTML..\n')
panini.refresh()
return src('src/pages/**/*.html')
.pipe(
panini({
root: 'src/pages/',
layouts: 'src/layouts/',
partials: 'src/partials/',
helpers: 'src/helpers/',
data: 'src/data/',
})
)
.pipe(
process.env.NODE_ENV === 'production'
? dest(options.paths.build.base)
: dest(options.paths.dist.base)
)
.pipe(browserSync.stream())
}
function compileCSS() {
console.log('\n\t' + logSymbols.info, 'Compiling CSS..\n')
if (process.env.NODE_ENV === 'production') {
return src(`${options.paths.dist.css}/**/*`)
.pipe(
purgeCSS({
content: ['src/**/*.{html,js}'],
defaultExtractor: (content) => {
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
const innerMatches =
content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || []
return broadMatches.concat(innerMatches)
},
})
)
.pipe(
cleanCSS({ compatibility: 'ie8', level: { 1: { specialComments: 0 } } })
)
.pipe(concat({ path: 'app.css' }))
.pipe(dest(options.paths.build.css))
} else {
return src(`${options.paths.src.css}/**/*.scss`)
.pipe(sass().on('error', sass.logError))
.pipe(dest(options.paths.src.css))
.pipe(
postcss([
require('tailwindcss')(options.config.tailwindJS),
require('autoprefixer'),
])
)
.pipe(concat({ path: 'app.css' }))
.pipe(dest(options.paths.dist.css))
}
}
function compileJS() {
console.log('\n\t' + logSymbols.info, 'Compiling JS..\n')
return browserify({
entries: [`${options.paths.src.js}/main.js`],
transform: [babelify.configure({ presets: ['@babel/preset-env'] })],
})
.bundle()
.pipe(source('app.js'))
.pipe(
process.env.NODE_ENV === 'production'
? dest(options.paths.build.js)
: dest(options.paths.dist.js)
)
}
function copyIMAGES() {
console.log('\n\t' + logSymbols.info, 'Copy Image..\n')
if (process.env.NODE_ENV === 'production') {
return src(`${options.paths.src.img}/**/*`)
.pipe(imagemin())
.pipe(dest(options.paths.build.img))
} else {
return src(`${options.paths.src.img}/**/*`).pipe(
dest(options.paths.dist.img)
)
}
}
function copyData() {
return src([`${options.paths.src.data}/**/*`]).pipe(
process.env.NODE_ENV === 'production'
? dest(options.paths.build.data)
: dest(options.paths.dist.data)
)
}
function watchFiles() {
watch(
`${options.paths.src.base}/**/*.html`,
series(compileHTML, compileCSS, previewReload)
)
watch(
[options.config.tailwindJS, `${options.paths.src.css}/**/*.scss`],
series(compileHTML, compileCSS, previewReload)
)
watch(`${options.paths.src.js}/**/*.js`, series(compileJS, previewReload))
watch(`${options.paths.src.img}/**/*`, series(copyIMAGES, previewReload))
watch(`${options.paths.src.data}/**/*`, series(copyData, previewReload))
// watch('src/**/*.{js,html,scss}', lint)
console.log('\n\t' + logSymbols.info, 'Watching for Changes..\n')
}
function clean() {
if (process.env.NODE_ENV === 'production') {
console.log(
'\n\t' + logSymbols.info,
'Cleaning build folder for fresh start.\n'
)
return del([options.paths.build.base])
} else {
console.log(
'\n\t' + logSymbols.info,
'Cleaning dist folder for fresh start.\n'
)
return del([options.paths.dist.base])
}
}
function buildFinish(done) {
console.log(
'\n\t' + logSymbols.info,
`Production build is complete. Files are located at ${options.paths.build.base}\n`
)
done()
}
function lint() {
return src(['src/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError())
}
exports.default = series(
clean,
parallel(compileCSS, compileJS, copyIMAGES, compileHTML, copyData),
livePreview,
watchFiles
)
exports.build = series(
clean,
parallel(compileCSS, compileJS, copyIMAGES, compileHTML, copyData),
buildFinish
)
exports.start = series(livePreview)