-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
182 lines (156 loc) · 4.17 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
/* eslint-disable no-console */
const browserify = require('browserify');
const connect = require('gulp-connect');
const cssmin = require('gulp-cssmin');
const merge = require('merge-stream');
const notify = require('gulp-notify');
const concat = require('gulp-concat');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const ghPages = require('gulp-gh-pages');
const source = require('vinyl-source-stream');
const streamify = require('gulp-streamify');
const uglify = require('gulp-uglify');
const watchify = require('watchify');
const files = {
dependencies: [],
browserify: [
'./src/main.js',
],
css: [
'src/styles/base.css',
],
};
const browserifyTask = function (options) {
let bundler = browserify({
entries: [options.src],
transform: [
['babelify', {
presets: ['es2015', 'stage-1'],
plugins: ['transform-class-properties'],
}],
],
debug: options.development,
cache: {}, // Requirement of watchify
packageCache: {}, // Requirement of watchify
fullPaths: options.development,
extensions: ['.js', '.json'],
});
const rebundle = function () {
const 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())))
.on('error', gutil.log)
.pipe(gulp.dest(options.dest))
.pipe(gulpif(options.development, connect.reload()))
.pipe(notify(() => {
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();
};
const browserifyDepsTask = function (options) {
const vendorsBundler = browserify({
debug: options.development,
require: files.dependencies,
});
const 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(() => {
console.log(`VENDORS bundle built in ${Date.now() - start}ms`);
}));
};
const cssTask = function (options) {
const 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(() => {
console.log(`CSS bundle built in ${Date.now() - start}ms`);
}));
};
gulp.task('ghpages', ['deploy'], () => {
return gulp.src('./build/**/*')
.pipe(ghPages());
});
gulp.task('deploy', () => {
process.env.NODE_ENV = 'production';
const browserifyDepsOpt = {
development: false,
src: files.dependencies,
output: 'vendors.js',
dest: './build/scripts',
};
const browserifyOpt = {
development: false,
src: files.browserify,
output: 'bundle.js',
dest: './build/scripts',
};
const cssOpt = {
development: false,
src: files.css,
output: 'styles.css',
dest: './build/styles',
};
return merge(
browserifyDepsTask(browserifyDepsOpt),
browserifyTask(browserifyOpt),
cssTask(cssOpt)
);
});
gulp.task('default', () => {
process.env.NODE_ENV = 'development';
const browserifyDepsOpt = {
development: true,
src: files.dependencies,
output: 'vendors.js',
dest: './build/scripts',
};
const browserifyOpt = {
development: true,
src: files.browserify,
output: 'bundle.js',
dest: './build/scripts',
};
const cssOpt = {
development: true,
src: files.css,
output: 'styles.css',
dest: './build/styles',
};
const serverOpt = {
root: './build',
port: 8080,
livereload: true,
};
connect.server(serverOpt);
gulp.watch(files.css, () => {
cssTask(cssOpt);
});
return merge(
browserifyDepsTask(browserifyDepsOpt),
browserifyTask(browserifyOpt),
cssTask(cssOpt)
);
});