-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
192 lines (166 loc) · 4.4 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
/* eslint-disable no-console, no-var, prefer-arrow-callback, prefer-template */
var browserify = require('browserify');
var connect = require('gulp-connect');
var cssmin = require('gulp-cssmin');
var merge = require('merge-stream');
var notify = require('gulp-notify');
var concat = require('gulp-concat');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var ghPages = require('gulp-gh-pages');
var source = require('vinyl-source-stream');
var streamify = require('gulp-streamify');
var uglify = require('gulp-uglify');
var watchify = require('watchify');
var files = {
// Library dependencies.
dependencies: [
'react',
'react-dom',
],
browserify: [
'./src/main.js',
],
// CSS files here, if any.
css: [],
};
var browserifyTask = function (options) {
var bundler = browserify({
entries: [options.src],
transform: [
['babelify', {
presets: [
'es2015',
'stage-2',
'react',
],
plugins: [
'transform-class-properties',
'babel-plugin-transform-object-assign',
],
}],
],
debug: options.development,
cache: {}, // Requirement of watchify
packageCache: {}, // Requirement of watchify
fullPaths: options.development,
options: ['.js', '.jsx', '.json'],
});
var rebundle = function () {
var start = Date.now();
console.log('Building APP bundle');
return bundler
.bundle()
.on('error', gutil.log)
.pipe(source(options.output))
.pipe(gulpif(!options.development, streamify(uglify())))
.pipe(gulp.dest(options.dest))
.pipe(gulpif(options.development, connect.reload()))
.pipe(notify(function () {
console.log('APP bundle built in ' + (Date.now() - start) + 'ms');
}));
};
bundler.external(files.dependencies);
if (options.development) {
bundler = watchify(bundler);
bundler.on('update', rebundle);
}
return rebundle();
};
var browserifyDepsTask = function (options) {
var vendorsBundler = browserify({
debug: options.development,
require: files.dependencies,
});
var start = new Date();
console.log('Building VENDORS bundle');
return vendorsBundler
.bundle()
.on('error', gutil.log)
.pipe(source(options.output))
.pipe(gulpif(!options.development, streamify(uglify())))
.on('error', gutil.log)
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
console.log('VENDORS bundle built in ' + (Date.now() - start) + 'ms');
}));
};
var cssTask = function (options) {
var start = new Date();
console.log('Building CSS bundle');
return gulp.src(options.src)
.pipe(concat(options.output))
.pipe(gulpif(!options.development, cssmin()))
.pipe(gulp.dest(options.dest))
.pipe(gulpif(options.development, connect.reload()))
.pipe(notify(function () {
console.log('CSS bundle built in ' + (Date.now() - start) + 'ms');
}));
};
gulp.task('ghpages', ['deploy'], function () {
return gulp.src('./build/**/*')
.pipe(ghPages());
});
gulp.task('deploy', function () {
process.env.NODE_ENV = 'production';
var browserifyDepsOpt = {
development: false,
src: files.dependencies,
dest: './build/scripts',
output: 'vendors.js',
};
var browserifyOpt = {
development: false,
src: files.browserify,
dest: './build/scripts',
output: 'app.js',
};
var cssOpt = {
development: false,
src: files.css,
dest: './build/styles',
output: 'styles.css',
};
return merge(
browserifyDepsTask(browserifyDepsOpt),
browserifyTask(browserifyOpt),
cssTask(cssOpt)
);
});
gulp.task('default', function () {
process.env.NODE_ENV = 'development';
var browserifyDepsOpt = {
development: true,
src: files.dependencies,
dest: './build/scripts',
output: 'vendors.js',
};
var browserifyOpt = {
development: true,
src: files.browserify,
dest: './build/scripts',
output: 'app.js',
};
var cssOpt = {
development: true,
src: files.css,
dest: './build/styles',
output: 'styles.css',
};
var serverOpt = {
root: './build',
port: 8080,
livereload: true,
fallback: './build/404.html',
};
connect.server(serverOpt);
gulp.watch(files.css, function () {
cssTask(cssOpt);
});
return merge(
browserifyDepsTask(browserifyDepsOpt),
browserifyTask(browserifyOpt),
cssTask(cssOpt)
);
});