forked from vigetlabs/blendid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
225 lines (194 loc) · 7.71 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
var gulp = require('gulp'),
browserify = require('browserify'),
watchify = require('watchify'),
mergeStream = require('merge-stream'),
source = require('vinyl-source-stream'),
_ = require('lodash'),
changed = require('gulp-changed'),
imagemin = require('gulp-imagemin'),
karma = require('karma'),
minifyCSS = require('gulp-minify-css'),
size = require('gulp-filesize'),
browserSync = require('browser-sync'),
sass = require('gulp-sass'),
iconfont = require('gulp-iconfont'),
rename = require('gulp-rename'),
swig = require('gulp-swig'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
prettyHrtime = require('pretty-hrtime'),
notify = require("gulp-notify"),
reactify = require('reactify'),
plumber = require('gulp-plumber'), //Prevent pipe breaking caused by errors from gulp plugins
startTime
;
var config = require('./gulpconfig');
gulp.task('default', ['sass', 'images', 'markup', 'watch']);
gulp.task('sass', function () {
return gulp.src(config.sass.src)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass(config.sass.settings))
.on('error', handleErrors)
.pipe(sourcemaps.write())
.pipe(autoprefixer({ browsers: ['last 2 version'] }))
.pipe(gulp.dest(config.sass.dest))
.pipe(browserSync.reload({stream:true}));
});
// Run this to compress all the things!
gulp.task('production', ['karma'], function(){
// This runs only if the karma tests pass
gulp.start(['markup', 'images', 'iconFont', 'minifyCss', 'uglifyJs'])
});
//TODO src: ['../*.php', '../inc/*.php', '../templates/*.php']
gulp.task('markup', function() {
return gulp.src(config.markup.src)
//.pipe(gulp.dest(config.markup.dest))
.pipe(browserSync.reload({stream:true}));
});
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))
.pipe(browserSync.reload({stream:true}));
});
gulp.task('minifyCss', ['sass'], function() {
return gulp.src(config.production.cssSrc)
.pipe(minifyCSS({keepBreaks:true}))
.pipe(gulp.dest(config.production.cssDest))
.pipe(size());
});
gulp.task('uglifyJs', ['browserify'], function() {
return gulp.src(config.production.jsSrc)
.pipe(uglify())
.pipe(gulp.dest(config.production.jsDest))
.pipe(size());
});
gulp.task('iconFont', function() {
return gulp.src(config.iconFonts.src)
.pipe(iconfont(config.iconFonts.options))
.on('codepoints', generateIconSass)
.pipe(gulp.dest(config.iconFonts.dest));
});
gulp.task('watch', ['watchify','browserSync'], function() {
gulp.watch(config.sass.src, ['sass']);
gulp.watch(config.images.src, ['images']);
gulp.watch(config.markup.src, ['markup']);
// Watchify will watch and recompile our JS, so no need to gulp.watch it
});
gulp.task('browserSync', function() {
browserSync(config.browserSync);
});
gulp.task('browserify', function() {
return browserifyTask(config.browserify, false)
});
//rebuild browserify bundles
gulp.task('watchify', function() {
// Start browserify task with devMode === true
return browserifyTask(config.browserify, true);
});
//can run karma start (if installed globally) in terminal and it will autowatch
gulp.task('karma', function(){
return karmaTask();
});
var karmaTask = function(done) {
karma.server.start({
configFile: process.cwd() + '/karma.conf.js',
singleRun: true
}, done);
};
var generateIconSass = function(codepoints, options) {
gulp.src(config.iconFonts.template)
.pipe(swig({
data: {
icons: codepoints.map(function(icon) {
return {
name: icon.name,
code: icon.codepoint.toString(16)
}
}),
fontName: config.iconFonts.options.fontName,
fontPath: config.iconFonts.fontPath,
className: config.iconFonts.className,
comment: 'DO NOT EDIT DIRECTLY!\n Generated by gulp/tasks/iconFont.js\n from ' + config.iconFonts.template
}
}))
.pipe(rename(config.iconFonts.sassOutputName))
.pipe(gulp.dest(config.iconFonts.sassDest));
};
/*
Bundle javascripty things with browserify!
This task is set up to generate multiple separate bundles, from
different sources, and to use Watchify when run from the default task.
*/
var browserifyTask = function(config, devMode) {
var browserifyThis = function(bundleConfig) {
if(devMode) {
// Add watchify args and debug (sourcemaps) option
_.extend(bundleConfig, watchify.args, { debug: true });
// A watchify require/external bug that prevents proper recompiling, so (for now) we'll ignore these
// options during development. Running `gulp browserify` directly will properly require and externalize.
bundleConfig = _.omit(bundleConfig, ['external', 'require']);
}
var b = browserify(bundleConfig);
var bundle = function() {
// Log when bundling starts
bundleLogger.start(bundleConfig.outputName);
return b
.bundle()
// Report compile errors
.on('error', handleErrors)
// Use vinyl-source-stream to make the stream gulp compatible. Specify the desired output filename here.
.pipe(source(bundleConfig.outputName))
// Specify the output destination
.pipe(gulp.dest(bundleConfig.dest))
.pipe(browserSync.reload({
stream: true
}));
};
if(devMode) {
// Wrap with watchify and rebundle on changes
b = watchify(b);
// Rebundle on update
b.on('update', bundle);
bundleLogger.watch(bundleConfig.outputName);
} else {
// Sort out shared dependencies. b.require exposes modules externally
if(bundleConfig.require) b.require(bundleConfig.require);
// b.external excludes modules from the bundle, and expects they'll be available externally
if(bundleConfig.external) b.external(bundleConfig.external);
}
return bundle();
};
// Start bundling with Browserify for each bundleConfig specified
return mergeStream.apply(gulp, _.map(config.bundleConfigs, browserifyThis));
};
//Gulp Style Logs
var bundleLogger = {
start: function(filepath) {
startTime = process.hrtime();
gutil.log('Bundling', gutil.colors.green(filepath) + '...');
},
watch: function(bundleName) {
gutil.log('Watching files required by', gutil.colors.yellow(bundleName));
},
end: function(filepath) {
var taskTime = process.hrtime(startTime);
var prettyTime = prettyHrtime(taskTime);
gutil.log('Bundled', gutil.colors.green(filepath), 'in', gutil.colors.magenta(prettyTime));
}
};
//Better Error message
var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: "Compile Error",
message: "<%= error %>"
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
};