-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
192 lines (170 loc) · 4.9 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
/**
* gulpfile.js
*/
var gulp = require('gulp'),
browserify = require('browserify'),
watchify = require('watchify'),
sass = require('gulp-sass'),
livereload = require('livereload'),
changed = require('gulp-changed'),
imagemin = require('gulp-imagemin'),
notify = require('gulp-notify'),
source = require('vinyl-source-stream');
/**
* config
*
*/
var dest = "./build";
var src = '.';
var config = {
browserSync: {
server: {
// We're serving the src folder as well
// for sass sourcemap linking
baseDir: [dest, src]
},
files: [
dest + "/**",
// Exclude Map files
"!" + dest + "/**.map"
]
},
sass: {
src: src + "/scss/*.{sass,scss}",
dest: dest
},
images: {
src: src + "/images/**",
dest: dest + "/images"
},
markup: {
src: src + "/docs/**",
dest: dest
},
browserify: {
// Enable source maps
debug: true,
// Additional file extentions to make optional
extensions: ['.coffee'],
// A separate bundle will be generated for each
// bundle config in the list below
bundleConfigs: [{
entries: src + '/js/app.js',
dest: dest,
outputName: 'bundle.js'
}]
}
};
/* bundleLogger
------------
Provides gulp style logs to the bundle method in browserify.js
*/
var gutil = require('gulp-util');
var prettyHrtime = require('pretty-hrtime');
var startTime;
var bundleLogger = {
start: function(filepath) {
startTime = process.hrtime();
gutil.log('Bundling', gutil.colors.green(filepath) + '...');
},
end: function(filepath, bytes) {
var taskTime = process.hrtime(startTime);
var prettyTime = prettyHrtime(taskTime);
gutil.log('Bundled', gutil.colors.green(filepath), 'in', gutil.colors.magenta(prettyTime), bytes, "bytes.");
}
};
function handleErrors() {
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
};
// images
gulp.task('images', function() {
return gulp.src(config.images.src)
.pipe(changed(config.images.dest)) // Ignore unchanged files
.pipe(imagemin()) // Optimize
.pipe(gulp.dest(config.images.dest));
});
// sass, scss
gulp.task('sass', ['images'], function () {
return gulp.src(config.sass.src)
.pipe(sass({
compass: false,
bundleExec: true,
sourcemap: true,
sourcemapPath: '../scss'
}))
.on('error', handleErrors)
.pipe(gulp.dest(config.sass.dest));
});
// Hack to enable configurable watchify watching
gulp.task('setWatch', function() {
global.isWatching = true;
});
// browserify task
gulp.task('browserify', function(callback) {
var bundleQueue = config.browserify.bundleConfigs.length;
var browserifyThis = function(bundleConfig) {
var bundler = browserify({
// Required watchify args
cache: {}, packageCache: {}, fullPaths: true,
// Specify the entry point of your app
entries: bundleConfig.entries,
// Add file extentions to make optional in your requires
extensions: config.browserify.extensions,
// Enable source maps!
debug: config.browserify.debug
});
var bundle = function() {
// Log when bundling starts
bundleLogger.start(bundleConfig.outputName);
return bundler
.bundle()
// Report compile errors
.on('error', handleErrors)
// Use vinyl-source-stream to make the
// stream gulp compatible. Specifiy the
// desired output filename here.
.pipe(source(bundleConfig.outputName))
// Specify the output destination
.pipe(gulp.dest(bundleConfig.dest))
.on('end', reportFinished);
};
if(global.isWatching) {
// Wrap with watchify and rebundle on changes
bundler = watchify(bundler);
// Rebundle on update
bundler.on('update', bundle);
}
var bytes;
bundler.on('bytes', function (b) { bytes = b });
var reportFinished = function() {
// Log when bundling completes
bundleLogger.end(bundleConfig.outputName, bytes)
if(bundleQueue) {
bundleQueue--;
if(bundleQueue === 0) {
// If queue is empty, tell gulp the task is complete.
// https://github.com/gulpjs/gulp/blob/master/docs/API.md#accept-a-callback
callback();
}
}
};
return bundle();
};
// Start bundling with Browserify for each bundleConfig specified
config.browserify.bundleConfigs.forEach(browserifyThis);
});
// Rerun tasks when a file changes
gulp.task('watch', ['setWatch', 'browserify'], function() {
gulp.watch(config.sass.src, ['sass'])
gulp.watch(config.images.src, ['images'])
});
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['watch'])
gulp.task('build', ['browserify', 'sass', 'images']);