-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
218 lines (196 loc) · 5.13 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
206
207
208
209
210
211
212
213
214
215
216
217
218
// Dependencies
const gulp = require('gulp')
const pkg = require('./package.json')
const sass = require('gulp-sass')
const autoprefixer = require('gulp-autoprefixer')
const header = require('gulp-header')
const pixrem = require('gulp-pixrem')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const minifyCss = require('gulp-clean-css')
const frontMatter = require('gulp-front-matter')
const data = require('gulp-data')
const sourcemaps = require('gulp-sourcemaps')
const del = require('del')
const browserSync = require('browser-sync')
const reload = browserSync.reload
const browserify = require('browserify')
const imagemin = require('gulp-imagemin')
const notify = require('gulp-notify')
const plumber = require('gulp-plumber')
const gulpIf = require('gulp-if')
const babelify = require('babelify')
const gulpUtil = require('gulp-util')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const runSequence = require('run-sequence')
const nunjucksRender = require('gulp-nunjucks-render')
const nunjucksFilters = require('./filters')
// Banner
const banner = [
'/**',
' * @name <%= pkg.name %>: <%= pkg.description %>',
' * @version <%= pkg.version %>: <%= new Date().toUTCString() %>',
' * @author <%= pkg.author %>',
' * @license <%= pkg.license %>',
' */'
].join('\n')
// Error notification
function onError(err) {
notify.onError({
title: 'Gulp',
subtitle: 'Failure!',
message: 'Error: <%= error.message %>',
sound: 'Beep'
})(err)
this.emit('end')
}
// ------------------------
// Configuration
// ------------------------
// Autoprefixer settings
const AUTOPREFIXER_BROWSERS = [
'ie >= 8',
'ie_mob >= 10',
'ff >= 20',
'chrome >= 4',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
]
// Build root destination / webroot for serve
const outputDir = './build'
// Asset destination base path
const assetPath = '/assets'
// Paths for source and destinations
const paths = {
src: {
css: './src/scss/',
js: './src/js/',
html: './src/templates/',
img: './src/img/',
fonts: './src/fonts/'
},
dest: {
css: `${outputDir}${assetPath}/css/`,
js: `${outputDir}${assetPath}/js/`,
html: outputDir,
img: `${outputDir}${assetPath}/img/`,
fonts: `${outputDir}${assetPath}/fonts/`
}
}
// ------------------------
// Tasks
// ------------------------
function clean() {
return del(`${paths.dest}`)
}
function jsCore() {
return browserify({
entries: `${paths.src.js}app.js`,
debug: !gulpUtil.env.production
})
.transform(babelify, {
presets: ['es2015']
})
.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulpIf(!!gulpUtil.env.production, uglify()))
.pipe(gulp.dest(paths.dest.js))
}
function html() {
var manageEnvironment = function(env) {
Object.keys(nunjucksFilters).forEach(function (filterName) {
env.addFilter(filterName, nunjucksFilters[filterName])
})
}
return gulp
.src(`${paths.src.html}views/**/*.html`)
.pipe(plumber({ errorHandler: onError }))
.pipe(frontMatter({ property: 'data' }))
.pipe(
data(() => {
return {
assetPath: assetPath
}
})
)
.pipe(nunjucksRender({
path: paths.src.html,
manageEnv: manageEnvironment
}))
.pipe(gulp.dest(paths.dest.html))
}
function scss() {
return gulp
.src([
`${paths.src.css}**/*.scss`,
`!${paths.src.css}{fonts,kss}/*.*`])
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe(pixrem())
.pipe(header(banner, { pkg: pkg }))
.pipe(sourcemaps.write())
.pipe(gulpIf(!!gulpUtil.env.production, minifyCss()))
.pipe(gulp.dest(paths.dest.css))
}
function img() {
return gulp
.src(`${paths.src.img}**/*`)
.pipe(
imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{ removeViewBox: true }]
})
)
.pipe(gulp.dest(paths.dest.img))
}
function fonts() {
return gulp.src(`${paths.src.fonts}**/*.*`).pipe(gulp.dest(paths.dest.fonts))
}
function serve() {
browserSync({
notify: false,
// https: true,
server: [outputDir],
tunnel: false,
open: false
})
watch(reload)
}
function watch(cb) {
const watchers = [
{ glob: `${paths.src.html}**/*.html`, tasks: ['html'] },
{ glob: `${paths.src.css}**/*.scss`, tasks: ['scss'] },
{ glob: `${paths.src.img}**/*`, tasks: ['img'] },
{ glob: `${paths.src.js}**/*`, tasks: ['js'] }
]
watchers.forEach(watcher => {
cb && watcher.tasks.push(cb)
gulp.watch(watcher.glob, watcher.tasks)
})
}
// ------------------------
// Gulp API
// ------------------------
gulp.task('compile', () => {
runSequence('clean', ['js', 'scss', 'img', 'html', 'fonts'])
})
gulp.task('jsCore', jsCore)
gulp.task('clean', clean)
gulp.task('js', ['jsCore'])
gulp.task('html', html)
gulp.task('scss', scss)
gulp.task('img', img)
gulp.task('fonts', fonts)
gulp.task('serve', () => {
runSequence('clean', ['js', 'scss', 'img', 'html', 'fonts'], serve)
})
gulp.task('watch', watch)
gulp.task('default', ['serve'])