-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·196 lines (180 loc) · 5.67 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
const devBuild = (process.env.NODE_ENV || '').trim().toLowerCase() === 'development'
const gulp = require('gulp')
const sass = require('gulp-sass')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const handlebars = require('gulp-hb')
const browserSync = require('browser-sync')
const watch = require('gulp-watch')
const sourcemaps = require('gulp-sourcemaps')
const sequence = require('run-sequence')
const postcss = require('gulp-postcss')
const plumber = require('gulp-plumber')
const gulpif = require('gulp-if')
const log = require('log-utils')
const cssnext = require('postcss-cssnext')
const cssnano = require('cssnano')({ autoprefixer: false })
const data = require('gulp-data')
const fm = require('front-matter')
const rev = require('gulp-rev')
const revReplace = require('gulp-rev-replace')
const del = require('del')
const webpack = require('webpack-stream')
const named = require('vinyl-named')
const layouts = require('handlebars-layouts')
const fs = require('fs')
const webpackConfig = require('./webpack.config')
const pkg = require('./package.json')
const paths = {
src: { root: 'src' },
dist: { root: 'dist' },
init: function () {
this.src.sass = `${this.src.root}/scss/index.scss`
this.src.templates = `${this.src.root}/**/*.hbs`
this.src.js = [`${this.src.root}/js/**/*.js`, `!${this.src.root}/js/libs/*.js`]
this.src.libs = `${this.src.root}/js/libs/*.js`
this.src.static = `${this.src.root}/static/**/*.*`
this.src.files = `${this.src.root}/*.{html,txt}`
this.dist.css = `${this.dist.root}/css`
this.dist.static = `${this.dist.root}/static`
this.dist.js = `${this.dist.root}/js`
this.dist.libs = `${this.dist.root}/js/libs`
return this
}
}.init()
const handleError = function (error) {
console.log(
log.red(`${log.symbol.error} Error (${error.plugin}): ${error.message}`)
)
this.emit('end')
}
// Browsersync
gulp.task('serve', function () {
browserSync.init({
server: paths.dist.root,
open: false,
notify: false,
online: false,
logFileChanges: false,
logPrefix: pkg.name
})
})
// Hash CSS and JS
gulp.task('rev', function () {
return gulp.src([`${paths.dist.css}/*.css`, `${paths.dist.js}/*.js`], {base: paths.dist.root})
.pipe(gulp.dest(paths.dist.root))
.pipe(rev())
.pipe(gulp.dest(paths.dist.root))
.pipe(rev.manifest())
.pipe(gulp.dest(paths.dist.root))
})
// Re-write paths to rev'd CSS and JS
gulp.task('revreplace', ['rev'], function () {
const manifest = gulp.src(`./${paths.dist.root}/rev-manifest.json`)
return gulp.src(`./${paths.dist.root}/**/*.html`)
.pipe(revReplace({manifest: manifest}))
.pipe(gulp.dest(paths.dist.root))
})
// Styles (Sass)
gulp.task('styles', function () {
const plugins = [
cssnext(),
cssnano
]
return gulp.src([paths.src.sass])
.pipe(gulpif(devBuild, plumber(handleError)))
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: ['src/scss']
}))
.pipe(postcss(plugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist.css))
.pipe(browserSync.reload({stream: true}))
})
// Compile handlebars/partials into html
gulp.task('templates', function () {
return gulp.src([paths.src.root + '/**/*.hbs', '!**/_*.hbs', `!${paths.src.root}/partials/**`])
.pipe(gulpif(devBuild, plumber(handleError)))
.pipe(
data(function () {
return JSON.parse(fs.readFileSync('./src/data/globals.json'))
})
)
.pipe(
data(function (file) {
const content = fm(String(file.contents))
file.contents = Buffer.from(content.body)
return content.attributes
})
)
.pipe(handlebars({ debug: false })
.partials('./src/partials/**/*.hbs'))
.helpers(layouts)
.helpers(`./${paths.src.root}/helpers/**/*.js`)
.data('./src/data/**/*.{js,json}')
.pipe(rename({
extname: '.html'
}))
.pipe(gulp.dest(paths.dist.root))
.pipe(browserSync.reload({stream: true}))
})
// Bundle JS (Webpack & Babel)
gulp.task('scripts:bundle', function () {
return gulp.src('./src/js/*.js')
.pipe(gulpif(devBuild, plumber(handleError)))
.pipe(named())
.pipe(webpack(webpackConfig))
.pipe(gulp.dest(paths.dist.js))
.pipe(browserSync.reload({stream: true}))
})
// Minify vendor libs
gulp.task('scripts:libs', function () {
return gulp.src([paths.src.libs])
.pipe(uglify())
.pipe(gulp.dest(paths.dist.libs))
.pipe(browserSync.reload({stream: true}))
})
// Copy Static Assets
gulp.task('copy:static', function () {
return gulp.src([paths.src.static])
.pipe(gulp.dest(paths.dist.static))
.pipe(browserSync.reload({stream: true}))
})
// Copy Static HTML and TXT
gulp.task('copy:files', function () {
return gulp.src([paths.src.files])
.pipe(gulp.dest(paths.dist.root))
.pipe(browserSync.reload({stream: true}))
})
// Delete Dist
gulp.task('clean', function () {
return del([paths.dist.root])
})
// Watch
gulp.task('watch', function () {
watch(paths.src.static, function () {
gulp.start('copy:static')
})
watch(paths.src.files, function () {
gulp.start('copy:files')
})
watch(`${paths.src.root}/data/**`, function () {
gulp.start('templates')
})
gulp.watch('src/scss/**/*.scss', ['styles'])
gulp.watch(paths.src.js, ['scripts:bundle'])
gulp.watch(paths.src.libs, ['scripts:libs'])
gulp.watch(paths.src.templates, ['templates'])
})
// Default task
gulp.task('default', ['watch', 'serve', 'copy:static', 'copy:files', 'styles', 'scripts:libs', 'scripts:bundle', 'templates'])
// Production Build
gulp.task('build', function (callback) {
sequence(
'clean',
['copy:static', 'copy:files', 'styles', 'scripts:libs', 'scripts:bundle', 'templates'],
'revreplace',
callback
)
})