-
Notifications
You must be signed in to change notification settings - Fork 20
/
gulpfile.js
307 lines (237 loc) · 6.41 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var browserify = require('browserify'),
shim = require('browserify-shim'),
chalk = require('chalk'),
del = require('del'),
gulp = require('gulp'),
bump = require('gulp-bump'),
connect = require('gulp-connect'),
deploy = require("gulp-gh-pages"),
git = require("gulp-git"),
less = require('gulp-less'),
rename = require('gulp-rename'),
streamify = require('gulp-streamify'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
merge = require('merge-stream'),
reactify = require('reactify'),
source = require('vinyl-source-stream'),
watchify = require('watchify');
/**
* Constants
*/
var SRC_PATH = 'src';
var DIST_PATH = 'dist';
var PACKAGE_FILE = 'Ajax.js';
var PACKAGE_NAME = 'react-ajax';
var COMPONENT_NAME = 'Ajax';
var DEPENDENCIES = ['react'];
var EXAMPLE_SRC_PATH = 'example/src';
var EXAMPLE_DIST_PATH = 'example/dist';
var EXAMPLE_APP = 'app.js';
var EXAMPLE_COPY = 'data.json';
var EXAMPLE_LESS = 'app.less';
var EXAMPLE_FILES = [
'index.html'
];
/**
* Bundle helpers
*/
function doBundle(target, name, dest) {
return target.bundle()
.on('error', function(e) {
gutil.log('Browserify Error', e);
})
.pipe(source(name))
.pipe(gulp.dest(dest))
.pipe(connect.reload());
}
function watchBundle(target, name, dest) {
return watchify(target)
.on('update', function (scriptIds) {
scriptIds = scriptIds
.filter(function(i) { return i.substr(0,2) !== './' })
.map(function(i) { return chalk.blue(i.replace(__dirname, '')) });
if (scriptIds.length > 1) {
gutil.log(scriptIds.length + ' Scripts updated:\n* ' + scriptIds.join('\n* ') + '\nrebuilding...');
} else {
gutil.log(scriptIds[0] + ' updated, rebuilding...');
}
doBundle(target, name, dest);
})
.on('time', function (time) {
gutil.log(chalk.green(name + ' built in ' + (Math.round(time / 10) / 100) + 's'));
});
}
/**
* Prepare task for examples
*/
gulp.task('prepare:examples', function(done) {
del([EXAMPLE_DIST_PATH], done);
});
/**
* Build example files
*/
function buildExampleFiles() {
return gulp.src(EXAMPLE_FILES.map(function(i) { return EXAMPLE_SRC_PATH + '/' + i }))
.pipe(gulp.dest(EXAMPLE_DIST_PATH))
.pipe(connect.reload());
}
gulp.task('dev:build:example:files', buildExampleFiles);
gulp.task('build:example:files', ['prepare:examples'], buildExampleFiles);
/**
* Build example css from less
*/
function buildExampleCSS() {
return gulp.src(EXAMPLE_SRC_PATH + '/' + EXAMPLE_LESS)
.pipe(less())
.pipe(gulp.dest(EXAMPLE_DIST_PATH))
.pipe(connect.reload());
}
gulp.task('dev:build:example:css', buildExampleCSS);
gulp.task('build:example:css', ['prepare:examples'], buildExampleCSS);
/**
* Build example scripts
*
* Returns a gulp task with watchify when in development mode
*/
function buildExampleScripts(dev) {
var dest = EXAMPLE_DIST_PATH;
var opts = dev ? watchify.args : {};
opts.debug = dev ? true : false;
opts.hasExports = true;
return function() {
var common = browserify(opts),
bundle = browserify(opts).require('./' + SRC_PATH + '/' + PACKAGE_FILE, { expose: PACKAGE_NAME }),
example = browserify(opts).exclude(PACKAGE_NAME).add('./' + EXAMPLE_SRC_PATH + '/' + EXAMPLE_APP);
DEPENDENCIES.forEach(function(pkg) {
common.require(pkg);
bundle.exclude(pkg);
example.exclude(pkg);
});
if (dev) {
watchBundle(common, 'common.js', dest);
watchBundle(bundle, 'bundle.js', dest);
watchBundle(example, 'app.js', dest);
}
return merge(
doBundle(common, 'common.js', dest),
doBundle(bundle, 'bundle.js', dest),
doBundle(example, 'app.js', dest)
);
}
};
gulp.task('dev:build:example:copy', function(){
return gulp.src(EXAMPLE_SRC_PATH + '/' + EXAMPLE_COPY)
.pipe(gulp.dest(EXAMPLE_DIST_PATH))
.pipe(connect.reload());
})
gulp.task('dev:build:example:scripts', buildExampleScripts(true));
gulp.task('build:example:scripts', ['prepare:examples'], buildExampleScripts());
/**
* Build examples
*/
gulp.task('build:examples', [
'build:example:files',
'build:example:css',
'build:example:scripts'
]);
gulp.task('watch:examples', [
'dev:build:example:files',
'dev:build:example:css',
'dev:build:example:scripts',
'dev:build:example:copy'
], function() {
gulp.watch(EXAMPLE_FILES.map(function(i) { return EXAMPLE_SRC_PATH + '/' + i }), ['dev:build:example:files']);
gulp.watch([EXAMPLE_SRC_PATH + '/' + EXAMPLE_LESS], ['dev:build:example:css']);
});
/**
* Serve task for local development
*/
gulp.task('dev:server', function() {
connect.server({
root: 'example/dist',
port: 9999,
livereload: true
});
});
/**
* Development task
*/
gulp.task('dev', [
'dev:server',
'watch:examples'
]);
/**
* Build task
*/
gulp.task('prepare:dist', function(done) {
del([DIST_PATH], done);
});
gulp.task('build:dist', ['prepare:dist'], function() {
var standalone = browserify('./' + SRC_PATH + '/' + PACKAGE_FILE, {
standalone: COMPONENT_NAME
})
.transform(reactify)
.transform(shim);
DEPENDENCIES.forEach(function(pkg) {
standalone.exclude(pkg);
});
return standalone.bundle()
.on('error', function(e) {
gutil.log('Browserify Error', e);
})
.pipe(source(PACKAGE_NAME + '.js'))
.pipe(gulp.dest(DIST_PATH))
.pipe(rename(PACKAGE_NAME + '.min.js'))
.pipe(streamify(uglify()))
.pipe(gulp.dest(DIST_PATH));
});
gulp.task('build', [
'build:dist',
'build:examples'
]);
/**
* Version bump tasks
*/
function getBumpTask(type) {
return function() {
return gulp.src(['./package.json', './bower.json'])
.pipe(bump({ type: type }))
.pipe(gulp.dest('./'));
};
}
gulp.task('bump', getBumpTask('patch'));
gulp.task('bump:minor', getBumpTask('minor'));
gulp.task('bump:major', getBumpTask('major'));
/**
* Git tag task
* (version *must* be bumped first)
*/
gulp.task('publish:tag', function(done) {
var pkg = require('./package.json');
var v = 'v' + pkg.version;
var message = 'Release ' + v;
git.tag(v, message, function (err) {
if (err) throw err;
git.push('origin', v, function (err) {
if (err) throw err;
done();
});
});
});
/**
* npm publish task
* * (version *must* be bumped first)
*/
gulp.task('publish:npm', function(done) {
require('child_process')
.spawn('npm', ['publish'], { stdio: 'inherit' })
.on('close', done);
});
/**
* Deploy tasks
*/
gulp.task('publish:examples', ['build:examples'], function() {
return gulp.src(EXAMPLE_DIST_PATH + '/**/*').pipe(deploy());
});
gulp.task('release', ['publish:tag', 'publish:npm', 'publish:examples']);