-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
113 lines (101 loc) · 3.38 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
// declarations, dependencies
// ----------------------------------------------------------------------------
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
var babelify = require('babelify');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
'react',
'react-dom'
];
// keep a count of the times a task refires
var scriptsCount = 0;
// Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('scripts', function () {
bundleApp(false);
});
gulp.task('deploy', function (){
bundleApp(true);
});
// gulp.task('watch', function () {
// gulp.watch(['./app/*.js'], ['scripts']);
// gulp.watch('./app/sass/*.scss', ['sass']);
// });
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch(['./app/js/*.js'], ['scripts']).on('change', browserSync.reload);
gulp.watch(['./app/js/components/*.js'], ['scripts']).on('change', browserSync.reload);
gulp.watch('./app/sass/*.scss', ['sass:watch']).on('change', browserSync.reload);
gulp.watch("*.html").on('change', browserSync.reload);
});
gulp.task('sass', function () {
return gulp.src('./app/sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
});
gulp.task('sass:watch', function () {
gulp.watch('./app/sass/*.scss', ['sass']);
});
// 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', ['scripts','sass','browser-sync']);
// Private Functions
// ----------------------------------------------------------------------------
function bundleApp(isProduction) {
scriptsCount++;
// Browserify will bundle all our js files together in to one and will let
// us use modules in the front end.
var appBundler = browserify({
entries: [
'./app/js/app.js',
'./app/js/components/main.js',
'./app/js/components/home.js',
'./app/js/components/login.js',
'./app/js/components/about.js',
'./app/js/components/contact.js',
'./app/js/components/shop.js',
'./app/js/components/products.js'
],
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('./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(function(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('./js/'));
}