-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
140 lines (122 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
'use strict';
// dependencies -----------------------------------------------------------
var gulp = require('gulp'),
babelify = require('babelify'),
browserify = require('browserify'),
browserSync = require('browser-sync'),
buffer = require('vinyl-buffer'),
CacheBuster = require('gulp-cachebust'),
cachebust = new CacheBuster(),
changed = require('gulp-changed'),
del = require('del'),
envify = require('gulp-envify'),
gulpif = require('gulp-if'),
rewrite = require('connect-history-api-fallback'),
notify = require('gulp-notify'),
reload = browserSync.reload,
sass = require('gulp-sass'),
source = require('vinyl-source-stream'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
watchify = require('watchify');
// project config ---------------------------------------------------------
var p = {
html: './src/index.html',
jsx: './src/scripts/client.jsx',
scss: './src/sass/**/*.scss',
scssmain: './src/sass/main.scss',
assets: './src/assets/*',
bundle: 'app.min.js',
dist: 'dist',
distTemp: 'dist/temp',
distAssets: 'dist/assets',
env: 'prod'
};
// primary tasks ----------------------------------------------------------
gulp.task('build', ['styles', 'buildApp'], function() {
gulp.start(['copy']);
});
gulp.task('watch', [], function() {
p.env = 'dev';
gulp.start(['watchStyles', 'watchApp', 'styles', 'copy', 'browserSync']);
});
gulp.task('default',['watch']);
// deployment build -------------------------------------------------------
// serve and sync changes
gulp.task('browserSync', function() {
browserSync.init({
server: './dist',
port: 9876,
middleware: rewrite()
});
});
// copy
gulp.task('copy', function () {
if (p.env === 'prod') {
del(['dist/*.js', 'dist/*.map', 'dist/*.html', 'dist/*.css']).then(function () {
gulp.src(p.html).pipe(cachebust.references()).pipe(gulp.dest(p.dist));
gulp.src(p.assets).pipe(gulp.dest(p.distAssets));
gulp.src('dist/temp/*').pipe(gulp.dest(p.dist))
.on('end', function () {
del(['dist/temp']);
});
});
} else {
gulp.src(p.html).pipe(gulp.dest(p.dist));
gulp.src(p.assets).pipe(gulp.dest(p.distAssets));
}
});
// bundle js
gulp.task('buildApp', function(cb) {
process.env.NODE_ENV = 'production';
browserify(p.jsx)
.transform(babelify)
.bundle()
.pipe(source(p.bundle))
.pipe(buffer())
.pipe(envify(process.env))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(cachebust.resources())
.pipe(sourcemaps.write('./', {
sourceMappingURL: function (file) {
return '/' + file.relative + '.map';
}
}))
.pipe(gulp.dest(p.distTemp))
.on('end', cb);
});
// compile & minify scss
gulp.task('styles', function() {
return gulp.src(p.scssmain)
.pipe(changed(p.dist))
.pipe(sass({errLogToConsole: true}))
.on('error', notify.onError())
.pipe(gulpif(p.env === 'prod', cachebust.resources()))
.pipe(gulpif(p.env === 'prod', gulp.dest(p.distTemp), gulp.dest(p.dist)))
.pipe(reload({stream: true}));
});
// development build ------------------------------------------------------
// watch for js changes
gulp.task('watchApp', function() {
var bundler = watchify(browserify(p.jsx, watchify.args));
function rebundle() {
return bundler
.bundle()
.on('error', notify.onError())
.pipe(source(p.bundle))
.pipe(buffer())
.pipe(envify(process.env))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest(p.dist))
.pipe(reload({stream: true}));
}
bundler.transform(babelify).on('update', rebundle);
return rebundle();
});
// watch styles
gulp.task('watchStyles', function() {
gulp.watch(p.scssmain, ['styles']);
gulp.watch(p.scss, ['styles']);
});