-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·232 lines (213 loc) · 6.23 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const gulp = require('gulp')
const gutil = require('gulp-util')
const webpackStream = require('webpack-stream')
const pug = require('gulp-pug')
const watch = require('gulp-watch')
const sass = require('gulp-sass')
const connect = require('gulp-connect');
const connectRewrite = require('http-rewrite-middleware')
const debounce = require('gulp-debounce');
const uglify = require('gulp-uglify')
const open = require('gulp-open')
const del = require('del')
const notify = require('gulp-notify')
const ghPages = require('gulp-gh-pages')
const changed = require('gulp-changed')
const runSequence = require('run-sequence')
const autoprefixer = require('gulp-autoprefixer')
const cssmin = require('gulp-cssmin')
const webpack = require('webpack')
const imagemin = require('gulp-imagemin')
const imageminJpegRecompress = require('imagemin-jpeg-recompress')
const MISC_FILES = ['./code/CNAME', './code/**/*.mp4', './code/**/*.ogv', './code/**/*.webm', './code/**/*.eot', './code/**/*.ttf', './code/**/*.woff', './code/**/*.woff2', './code/scripts/prism/**/*']
const PUG_FILES = ['./code/**/*.pug', './code/**/*.jade']
const SASS_FILES = ['./code/**/*.scss']
const SASS_INCLUDE_PATHS = [
'node_modules/prismjs/themes',
'node_modules/susy/sass'
]
const FAVICON_BASE = ['./code/favicons']
const FAVICON_FILES = [(FAVICON_BASE + '/**/*')]
const IMAGE_FILES = ['./code/**/*.png','./code/**/*.jpg','./code/**/*.gif','./code/**/*.jpeg', './code/**/*.svg', '!./code/images/favicons/**/*']
const WEBPACKABLE_FILES = './code/scripts/index.js'
const LIB_FILES = ['./code/lib/prism/**/*']
const BUILD_SRC = './code/'
const BUILD_DEST = './dist/'
const BUILT_FILES = BUILD_DEST + '**/*'
const webpackConfig = {
output: {
filename: 'index.js',
devtoolModuleFilenameTemplate: '[resource-path]'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
devtool: 'source-map',
devServer: {
stats: {
colors: true,
chunks: false
}
},
plugins: [
new webpack.DefinePlugin({
"process.env": { "NODE_ENV": JSON.stringify("production") }
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
stats: {
hash: false,
version: false,
timings: false,
assets: false,
chunks: true,
chunkModules: false,
modules: false,
children: false,
cached: false,
reasons: false,
source: false,
chunkOrigins: false
}
}
const logError = function(error) {
var errorString = error.toString()
notify.onError({
title: 'Build Error',
message: errorString
})(error)
console.log(errorString)
this.emit('end')
}
// ---------------------------------
// --------- BUILD TASKS -----------
// ---------------------------------
gulp.task('clean', (callback) => {
return del(BUILT_FILES, callback)
})
gulp.task('favicons', () => {
return gulp.src(FAVICON_FILES, {cwd: FAVICON_BASE})
.pipe(gulp.dest(BUILD_DEST))
.on('error', logError)
})
gulp.task('misc', () => {
return gulp.src(MISC_FILES)
.pipe(changed(BUILD_DEST))
.pipe(gulp.dest(BUILD_DEST))
.on('error', logError)
})
gulp.task('lib', () => {
return gulp.src(LIB_FILES, { base: './code' })
.pipe(changed(BUILD_DEST))
.pipe(gulp.dest(BUILD_DEST))
.on('error', logError)
})
gulp.task('templates', () => {
return gulp.src(PUG_FILES)
.pipe(pug({
pretty: true
}))
.on('error', logError)
.pipe(gulp.dest(BUILD_DEST))
})
gulp.task('styles', () => {
return gulp.src(SASS_FILES)
.pipe(sass({ includePaths: SASS_INCLUDE_PATHS }))
.on('error', logError)
.pipe(autoprefixer({
browsers: ['> 1%', 'last 2 versions', 'IE 9']
}))
.on('error', logError)
.pipe(cssmin())
.on('error', logError)
.pipe(gulp.dest(BUILD_DEST))
})
gulp.task('images', () => {
return gulp.src(IMAGE_FILES)
.pipe(changed(BUILD_DEST))
.pipe(imagemin([
imageminJpegRecompress({quality: 'veryhigh'})
]))
.on('error', logError)
.pipe(gulp.dest(BUILD_DEST))
})
gulp.task("app_scripts", () => {
return gulp.src(WEBPACKABLE_FILES)
.pipe(webpackStream(webpackConfig))
.on('error', logError)
.pipe(gulp.dest(BUILD_DEST+'scripts/'))
})
gulp.task('reload', () => {
return gulp.src(BUILT_FILES)
.pipe(debounce({ wait: 1000 })) // Avoids double/triple page reloads
.pipe(connect.reload())
})
// ---------------------------------
// --------- WATCH TASKS -----------
// ---------------------------------
gulp.task('watch', () => {
watch(FAVICON_FILES, () => gulp.start('favicons'))
watch(MISC_FILES, () => gulp.start('misc'))
watch(SASS_FILES, () => gulp.start('styles'))
watch(IMAGE_FILES, () => gulp.start('images'))
watch(PUG_FILES, () => gulp.start('templates'))
watch(LIB_FILES, () => gulp.start('lib'))
webpackConfig.watch = true
gulp.src(WEBPACKABLE_FILES)
.pipe(webpackStream(webpackConfig))
.on('error', logError)
.pipe(gulp.dest(BUILD_DEST+'scripts/'))
watch(BUILT_FILES, () => gulp.start('reload'))
})
// ----------------------------------
// --------- SERVER TASKS -----------
// ----------------------------------
gulp.task('server', () => {
const middleware = connectRewrite.getMiddleware([
{from: '^([^.]+[^/])$', to: '$1.html'}
])
connect.server({
port: 8081,
root: 'dist',
livereload: true,
middleware: (connect, options) => {
return [middleware]
}
})
return gulp.src('./dist/index.html')
.pipe(open('', {
url: 'http://localhost:8081',
app: 'google chrome'
}))
})
// ----------------------------------
// --------- DEPLOY TASKS -----------
// ----------------------------------
gulp.task('deploy', () => {
return gulp.src(BUILT_FILES)
.pipe(ghPages())
.on('error', logError)
})
// ----------------------------------
// --------- COMPOSITE TASKS --------
// ----------------------------------
gulp.task('build', (cb) => {
return runSequence('clean', ['misc', 'lib', 'favicons', 'templates', 'styles', 'images', 'app_scripts'], cb)
})
gulp.task('start', (cb) => {
return runSequence('clean', ['misc', 'lib', 'favicons', 'templates', 'styles', 'images'], 'watch', 'server', cb)
})