-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
138 lines (115 loc) · 3.97 KB
/
index.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
var babelify = require('babelify');
var brfs = require('brfs');
var browserify = require('browserify');
var bytes = require('bytes');
var chalk = require('chalk');
var connect = require('gulp-connect');
var del = require('del');
var gutil = require('gulp-util');
var less = require('gulp-less');
var merge = require('merge-stream');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
var xtend = require('xtend');
var COMMON_PACKAGES = [
'react-addons-css-transition-group',
'react-container',
'react-dom',
'react-tappable',
'react',
];
module.exports = function (gulp) {
var wwwDir = './www';
function doBundle (target, name, dest) {
return target.bundle()
.on('error', function (err) {
var parts = err.message.split('.js: ');
var br = '\n ';
var msg = parts.length === 2 ? chalk.red('Browserify Error in ') + chalk.red.underline(parts[0] + '.js') + br + parts[1] : chalk.red('Browserify Error:') + br + err.message;
gutil.log(msg);
})
.pipe(source(name))
.pipe(gulp.dest(dest))
.pipe(connect.reload());
}
function watchBundle (bundle, name, dest) {
return watchify(bundle)
.on('log', function (message) {
message = message.replace(/(\d+) bytes/, function () {
return bytes.format(Number(arguments[1]));
});
gutil.log(chalk.grey(message));
})
.on('time', function (time) {
gutil.log(chalk.green('Application built in ' + (Math.round(time / 10) / 100) + 's'));
})
.on('update', function (ids) {
var changed = ids.map(function (x) { return chalk.blue(x.replace(__dirname, '')); });
if (changed.length > 1) {
gutil.log(changed.length + ' scripts updated:\n* ' + changed.join('\n* ') + '\nrebuilding...');
} else {
gutil.log(changed[0] + ' updated, rebuilding...');
}
doBundle(bundle, name, dest);
});
}
function buildApp (entries, transforms, dest, watch) {
var opts = xtend(watch && watchify.args, {
entries: entries,
debug: process.env.NODE_ENV !== 'production'
});
var app = browserify(opts);
var react = browserify();
transforms.forEach(function (transform) {
app.transform(transform);
});
COMMON_PACKAGES.forEach(function (pkg) {
app.exclude(pkg);
react.require(pkg);
});
if (watch) {
watchBundle(app, 'app.js', dest);
}
return merge(doBundle(react, 'react.js', dest), doBundle(app, 'app.js', dest));
}
function plumb (src, transforms, dest) {
var stream = gulp.src(src);
transforms.forEach(function (transform) {
stream = stream.pipe(transform());
});
if (dest) {
stream = stream.pipe(gulp.dest(dest));
}
return stream.pipe(connect.reload());
}
var babelifyTransform = babelify.configure({
plugins: [require('babel-plugin-object-assign')]
});
// Build
gulp.task('fonts', plumb.bind(null, 'src/fonts/**', [], wwwDir + '/fonts'));
gulp.task('html', plumb.bind(null, 'src/*.html', [], wwwDir));
gulp.task('images', plumb.bind(null, 'src/img/**', [], wwwDir + '/img'));
gulp.task('css', plumb.bind(null, 'src/css/*.css', [], wwwDir + '/css'));
gulp.task('less', plumb.bind(null, 'src/css/app.less', [less], wwwDir + '/css'));
gulp.task('scripts', buildApp.bind(null, ['./src/js/app.js'], [babelifyTransform, brfs], wwwDir + '/js', false));
gulp.task('build', ['html', 'images', 'fonts', 'css', 'less', 'scripts']);
// Clean
gulp.task('clean', function () { return del([wwwDir + '/*']); });
// Development
gulp.task('scripts-watch', buildApp.bind(null, ['./src/js/app.js'], [babelifyTransform, brfs], wwwDir + '/js', true));
gulp.task('watch', ['html', 'images', 'fonts', 'css', 'less', 'scripts-watch'], function () {
gulp.watch(['src/*.html'], ['html']);
gulp.watch(['src/img/**/*.*'], ['images']);
gulp.watch(['src/fonts/**/*.*'], ['fonts']);
gulp.watch(['src/css/**/*.css'], ['css']);
gulp.watch(['src/css/**/*.less'], ['less']);
});
gulp.task('serve', function () {
connect.server({
root: wwwDir,
port: 8000,
livereload: true
});
});
gulp.task('dev', ['serve', 'watch']);
};