-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
216 lines (187 loc) · 5.88 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
var gulp = require('gulp');
var tar = require('gulp-tar');
var gzip = require('gulp-gzip');
var server = require('gulp-develop-server');
var livereload = require('gulp-livereload');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var watchify = require('watchify');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var streamify = require('gulp-streamify');
var notify = require('gulp-notify');
var concat = require('gulp-concat');
var cssmin = require('gulp-cssmin');
var gutil = require('gulp-util');
var Rx = require('rx');
var less = require('gulp-less');
var babelify = require("babelify");
var babel = require("gulp-babel");
var filesToWatch = [
'./server/**/*',
'./client/build/**/*'
];
var startServer = function (options) {
server.listen(options, function () {
livereload.listen({port: 35739}); // change the live reload port
});
if (options.development) {
function restart(file) {
server.changed(function (error) {
if (!error) livereload.changed(file.path);
});
}
gulp.watch(filesToWatch).on('change', restart);
}
};
var browserifyTask = function (options) {
var subject = new Rx.Subject();
// Our app bundler
var appBundler = browserify({
entries: [options.src], // Only need initial file, browserify finds the rest
transform: [babelify],
debug: options.development, // Gives us sourcemapping
cache: {}, packageCache: {}, fullPaths: options.development // Requirement of watchify
});
// The rebundle process
var rebundle = function () {
var start = Date.now();
console.log('Building APP bundle');
appBundler.bundle()
.on('error', gutil.log)
.pipe(source('main.js'))
.pipe(gulpif(!options.development, streamify(uglify())))
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
subject.onNext('APP bundle built in ' + (Date.now() - start) + 'ms');
subject.onCompleted();
}));
};
rebundle();
// Fire up Watchify when developing
if (options.development) {
appBundler = watchify(appBundler);
appBundler.on('update', rebundle);
}
return subject;
};
var cssTask = function (options) {
var subject = new Rx.Subject();
var start = new Date();
if (options.development) {
var run = function () {
console.log(arguments);
console.log('Building CSS bundle src: ' + options.src);
gulp.src(options.src)
.pipe(less())
.pipe(concat('main.css'))
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
subject.onNext('CSS bundle built in ' + (Date.now() - start) + 'ms');
subject.onCompleted();
}));
};
run();
gulp.watch(options.watch || options.src, run);
} else {
gulp.src(options.src)
.pipe(less())
.pipe(concat('main.css'))
.pipe(cssmin())
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
subject.onNext('CSS bundle built in ' + (Date.now() - start) + 'ms');
subject.onCompleted();
}));
}
return subject;
};
var copyFonts = function(options) {
var subject = new Rx.Subject();
var start = new Date();
gulp
.src(options.from)
.pipe(gulp.dest(options.to))
.pipe(notify(function () {
subject.onNext('Fonts copied in ' + (Date.now() - start) + 'ms');
subject.onCompleted();
}));
return subject;
}
var buildServer = function (options) {
var subject = new Rx.Subject();
var build = function () {
var start = new Date();
return gulp.src("server/**/*.js") //get all js files under the src
//.pipe(sourceMaps.init()) //initialize source mapping
.pipe(babel()) //run babel
//.pipe(sourceMaps.write(".")) //write source maps
.pipe(gulp.dest("bundle/server"))
.pipe(notify(function () {
subject.onNext('Server build completed in ' + (Date.now() - start) + 'ms');
subject.onCompleted();
}));
};
if (options.development) {
gulp.watch(['./server/**/*']).on('change', build);
}
build();
return subject;
};
gulp.task('default', function () {
var dev = true;
var source1 = browserifyTask({
development: dev,
src: './client/app/main.js',
dest: './client/build/js'
});
var source2 = cssTask({
development: dev,
watch: './client/styles/**/*.less',
src: './client/styles/main.less',
dest: './client/build/css'
});
var source3 = buildServer({development: dev});
var source4 = copyFonts({
from: './node_modules/bootstrap/dist/fonts/**/*',
to:'./client/build/fonts/'});
Rx.Observable.combineLatest(source1, source2, source3, source4)
.subscribe(function (msg) {
console.log(msg[0]);
console.log(msg[1]);
console.log(msg[2]);
console.log(msg[3]);
startServer({
development: true,
path: "./bundle/server/server.js"
});
})
});
gulp.task('bundle', function () {
var dev = false;
var source1 = browserifyTask({
development: dev,
src: './client/app/main.js',
dest: './bundle/client/js'
});
var source2 = cssTask({
development: dev,
src: './client/styles/*.less',
dest: './bundle/client/css'
});
var source3 = buildServer({development: dev});
Rx.Observable.combineLatest(source1, source2, source3)
.do(function (msg) {
console.log(msg[0] + '\n' + msg[1] + '\n' + msg[2]);
gulp.src(['./package.json']).pipe(gulp.dest('./bundle'));
gulp.src(['./client/build/img/**/*']).pipe(gulp.dest('./bundle/client/img'));
gulp.src(['./client/build/fonts/*']).pipe(gulp.dest('./bundle/client/fonts'));
gulp.src(['./client/build/js/*.min.js']).pipe(gulp.dest('./bundle/client/js'));
})
.delay(3000)
.subscribe(function () {
gulp.src('**/*', {cwd: 'bundle'}).pipe(tar('bundle.tar')).pipe(gzip()).pipe(gulp.dest('.')).pipe(notify(function () {
console.log('Creating the app bundle has completed...')
}));
})
});