This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
162 lines (142 loc) · 5.26 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
// Load in our dependencies
var _ = require('underscore');
var browserify = require('browserify');
var gulp = require('gulp');
var gulpBuffer = require('gulp-buffer');
var gulpCsso = require('gulp-csso');
var gulpImagemin = require('gulp-imagemin');
var gulpSvgmin = require('gulp-svgmin');
var gulpLivereload = require('gulp-livereload');
var gulpNotify = require('gulp-notify');
var gulpSass = require('gulp-sass');
var gulpSourcemaps = require('gulp-sourcemaps');
var gulpUglify = require('gulp-uglify');
var gulpSizereport = require('gulp-sizereport');
var rimraf = require('rimraf');
var vinylSourceStream = require('vinyl-source-stream');
var watchify = require('watchify');
// Set up our configuration
var config = {
allowFailures: false,
minifyAssets: true
};
// Define our build tasks
gulp.task('build-clean', function clean (done) {
// Remove all compiled files in `dist/`
rimraf(__dirname + '/dist/', done);
});
gulp.task('build-css', function buildCss () {
// Generate a stream that compiles SCSS to CSS
// DEV: We return the pipe'd stream so gulp knows when we exit
var cssStream = gulp.src('browser/css/index.scss')
.pipe(gulpSass({
style: 'nested'
}));
// If we are allowing failures, then log them
// DEV: Desktop notifications are a personal preference
// If they get unwieldy, feel free to move to logging only
// But be sure to continue to emit an `end` event
if (config.allowFailures) {
cssStream.on('error', gulpNotify.onError());
}
// If we are minifying assets, then minify them
if (config.minifyAssets) {
cssStream = cssStream
.pipe(gulpCsso())
.pipe(gulpSizereport({gzip: true}));
}
// Output our CSS and notify LiveReload
return cssStream
.pipe(gulp.dest('dist/css'))
.pipe(gulpLivereload());
});
gulp.task('build-images-svg', function buildImagesSvg () {
// Optimize SVG files inline
return gulp.src('browser/images/**/*.svg')
.pipe(gulpSvgmin())
.pipe(gulpSizereport({gzip: true}))
.pipe(gulp.dest('browser/images'))
.pipe(gulp.dest('dist/images'))
.pipe(gulpLivereload());
});
gulp.task('build-images-non-svg', function buildImagesNonSvg () {
// Optimize PNG/JPG files inline
return gulp.src(['browser/images/**/*', '!browser/images/**/*.svg'])
.pipe(gulpImagemin())
.pipe(gulpSizereport({gzip: true}))
.pipe(gulp.dest('browser/images'))
.pipe(gulp.dest('dist/images'))
.pipe(gulpLivereload());
});
gulp.task('build-images-vendor', function buildImagesVendor () {
// Optimize vendor images and place them in the CSS folder
return gulp.src(['bower_components/chosen-bootstrap/chosen-sprite*'])
.pipe(gulpImagemin())
.pipe(gulpSizereport({gzip: true}))
.pipe(gulp.dest('dist/css'))
.pipe(gulpLivereload());
});
gulp.task('build-images', ['build-images-svg', 'build-images-non-svg', 'build-images-vendor']);
// Create a browserify instance
// https://github.com/gulpjs/gulp/blob/v3.9.1/docs/recipes/browserify-uglify-sourcemap.md
// https://github.com/substack/watchify/tree/v3.7.0#watchifyb-opts
exports.browserifyOptions = {
cache: {}, packageCache: {},
debug: true, // Enable source maps
transform: ['brfs', 'envify']
};
var browserifyObj = browserify(_.defaults({
entries: __dirname + '/browser/js/index.js'
}, exports.browserifyOptions));
gulp.task('build-js', function buildJs () {
// Bundle browserify content
var jsStream = browserifyObj.bundle();
// If we are allowing failures, then log them
if (config.allowFailures) {
jsStream.on('error', gulpNotify.onError());
}
// Coerce browserify output into a Vinyl object with buffer content
jsStream = jsStream
.pipe(vinylSourceStream('index.js'))
.pipe(gulpBuffer());
// Extract browserify inline sourcemaps into in-memory file
jsStream = jsStream.pipe(gulpSourcemaps.init({loadMaps: true}));
// If we are minifying assets, then minify them
if (config.minifyAssets) {
jsStream = jsStream
.pipe(gulpUglify())
.pipe(gulpSizereport({gzip: true}));
}
// Output sourcemaps in-memory to Vinyl file
jsStream = jsStream.pipe(gulpSourcemaps.write('./'));
// Return our stream
return jsStream
.pipe(gulp.dest('dist/js'))
.pipe(gulpLivereload());
});
gulp.task('build', ['build-css', 'build-images', 'build-js']);
// Define our development tasks
gulp.task('livereload-update', function livereloadUpdate () {
gulpLivereload.reload();
});
// DEV: `['build']` requires that our build task runs once
gulp.task('develop', ['build'], function develop () {
// Set up our tasks to allow failures
config.allowFailures = true;
config.minifyAssets = false;
// Start a livereload server
gulpLivereload.listen();
// Integrate watchify on browserify
browserifyObj.plugin(watchify);
browserifyObj.on('update', function handleBUpdate () {
// DEV: At some point `gulp.run` will be deprecated, move to `gulp.series` when it does
gulp.run('build-js');
});
// DEV: Trigger a browserify build to make watchify start watching files
browserifyObj.bundle().on('data', function () {});
// When one of our src files changes, re-run its corresponding task
gulp.watch('browser/css/**/*.scss', ['build-css']);
gulp.watch(['browser/images/**/*', '!browser/images/screenshots/**/*'],
['build-images']);
gulp.watch('server/**/*', ['livereload-update']);
});