-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
102 lines (92 loc) · 2.93 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
// declarations, dependencies
// ----------------------------------------------------------------------------
const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const gutil = require('gulp-util');
const babelify = require('babelify');
const browserSync = require('browser-sync');
const reload = browserSync.reload;
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
const dependencies = [
'react',
'react-dom',
];
// keep a count of the times a task refires
let scriptsCount = 0;
// Private Functions
// ----------------------------------------------------------------------------
function bundleApp(isProduction, shouldReload) {
scriptsCount += 1;
// Browserify will bundle all our js files together in to one and will let
// us use modules in the front end.
const appBundler = browserify({
entries: './app/app.jsx',
debug: true,
});
// If it's not for production, a separate vendors.js file will be created
// the first time gulp is run so that we don't have to rebundle things like
// react everytime there's a change in the js file
if (!isProduction && scriptsCount === 1) {
// create vendors.js for dev environment.
browserify({
require: dependencies,
debug: true,
})
.bundle()
.on('error', gutil.log)
.pipe(source('vendors.js'))
.pipe(gulp.dest('./web/js/'));
}
if (!isProduction) {
// make the dependencies external so they dont get bundled by the
// app bundler. Dependencies are already bundled in vendor.js for
// development environments.
dependencies.forEach((dep) => {
appBundler.external(dep);
});
}
appBundler
// transform ES6 and JSX to ES5 with babelify
.transform(babelify, { presets: ['es2015', 'react'] })
.bundle()
.on('error', gutil.log)
.pipe(source('bundle.js'))
.pipe(gulp.dest('./web/js/'))
.on('end', () => {
if (shouldReload) {
reload();
}
});
}
// Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('startBrowserSync', () => {
browserSync({
notify: false,
logPrefix: 'BS',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: './',
});
});
gulp.task('scripts', () => {
bundleApp(false, true);
});
gulp.task('deploy', () => {
bundleApp(true, false);
});
gulp.task('watch', () => {
gulp.watch([
'./app/**/*.css',
'./app/**/*.js',
'./app/**/*.jsx',
], ['scripts']);
});
// When running 'gulp' on the terminal this task will fire.
// It will start watching for changes in every .js file.
// If there's a change, the task 'scripts' defined above will fire.
gulp.task('default', ['startBrowserSync', 'scripts', 'watch']);