-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
152 lines (128 loc) · 4.02 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
var gulp = require('gulp');
var source = require('vinyl-source-stream'); // Used to stream bundle for further handling
var browserify = require('browserify');
var watchify = require('watchify');
var reactify = require('reactify');
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 livereload = require('gulp-livereload');
var connect = require('gulp-connect');
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
'react',
'react/addons'
];
var browserifyTask = function (options) {
// Our app bundler
var appBundler = browserify({
entries: [options.src], // Only need initial file, browserify finds the rest
transform: [reactify], // We want to convert JSX to normal javascript
debug: options.development, // Gives us sourcemapping
cache: {}, packageCache: {}, fullPaths: options.development // Requirement of watchify
});
// We set our dependencies as externals on our app bundler when developing
(options.development ? dependencies : []).forEach(function (dep) {
appBundler.external(dep);
});
// 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(gulpif(options.development, livereload()))
.pipe(notify(function () {
console.log('APP bundle built in ' + (Date.now() - start) + 'ms');
}));
};
// Fire up Watchify when developing
if (options.development) {
appBundler = watchify(appBundler);
appBundler.on('update', rebundle);
}
rebundle();
// We create a separate bundle for our dependencies as they
// should not rebundle on file changes. This only happens when
// we develop. When deploying the dependencies will be included
// in the application bundle
if (options.development) {
if (!options.development) {
dependencies.splice(dependencies.indexOf('react-addons'), 1);
}
var vendorsBundler = browserify({
debug: true,
require: dependencies
});
// Run the vendor bundle
var start = new Date();
console.log('Building VENDORS bundle');
vendorsBundler.bundle()
.on('error', gutil.log)
.pipe(source('vendors.js'))
.pipe(gulpif(!options.development, streamify(uglify())))
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
console.log('VENDORS bundle built in ' + (Date.now() - start) + 'ms');
}));
}
}
var cssTask = function (options) {
if (options.development) {
var run = function () {
console.log(arguments);
var start = new Date();
console.log('Building CSS bundle');
gulp.src(options.src)
.pipe(concat('main.css'))
.pipe(gulp.dest(options.dest))
.pipe(notify(function () {
console.log('CSS bundle built in ' + (Date.now() - start) + 'ms');
}));
};
run();
gulp.watch(options.src, run);
} else {
gulp.src(options.src)
.pipe(concat('main.css'))
.pipe(cssmin())
.pipe(gulp.dest(options.dest));
}
}
// Starts our development workflow
gulp.task('default', function () {
browserifyTask({
development: true,
src: './app/scripts/main.js',
dest: './build'
});
cssTask({
development: true,
src: './app/styles/**/*.css',
dest: './build'
});
connect.server({
root: 'build/',
port: 8880
});
});
gulp.task('deploy', function () {
browserifyTask({
development: false,
src: './app/scripts/main.js',
dest: './dist'
});
cssTask({
development: false,
src: './app/styles/**/*.css',
dest: './dist'
});
});