-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
170 lines (142 loc) · 4.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
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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var smaps = require('gulp-sourcemaps');
var filter = require('gulp-filter');
var gulpIf = require('gulp-if');
var url = require('url');
gulp.task('default', ['build', 'bundle', 'compile', 'migrate']);
// This step will compile the ES6/JSX in app/
// into ES5 versions (with source maps). This
// does not perform any bundling.
gulp.task('build', function() {
// Requiring inline saves startup time for other builds
var babel = require('gulp-babel');
var newer = require('gulp-newer');
return gulp.src('app/**/*.js')
.pipe(newer('dist/src'))
.pipe(smaps.init())
.pipe(babel())
.on('error', err => {
gutil.log(err.toString());
})
.pipe(smaps.write('.', {sourceRoot: '/source/app/'}))
.pipe(gulp.dest('dist/src'));
});
// This will bundle the ES5 modules in dist/src
// into a single bundle (and source map) for
// dist/build
gulp.task('bundle', ['build'], bundle);
gulp.task('watch-bundle', function() {
var watchify = require('watchify');
var browserify = require('browserify');
var babelify = require('babelify');
watchify(getBundler(), {
ignoreWatch: [] // we want to watch node_modules
// because we do a lot of linking
}).on('update', () => {
gulp.start('bundle');
});
return gulp.start('bundle');
});
var bundler;
function bundle() {
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
return getBundler().bundle()
.on('error', err => {
gutil.log(new gutil.PluginError('browserify', err).toString());
})
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulpIf(process.env.DEBUG_MODE,
smaps.init({loadMaps: true}),
smaps.write('.')))
.pipe(gulp.dest('dist/build'));
}
function getBundler() {
var browserify = require('browserify');
if (!bundler) {
bundler = browserify('dist/src/main.js', {
cache: {},
packageCache: {},
debug: process.env.DEBUG_MODE
});
if (process.env.DEBUG_MODE) {
bundler.transform(require('sourceify'), {global: true});
}
}
return bundler;
}
// This compiles all LESS files from app/styles
// into a single CSS bundle (and source map) in
// dist/build
gulp.task('compile', function() {
var less = require('gulp-less');
var NpmImportPlugin = require('less-plugin-npm-import');
var npmImports = new NpmImportPlugin({prefix: '~'});
return gulp.src('./app/styles/main.less')
.pipe(smaps.init())
.pipe(less({plugins: [npmImports]}))
.pipe(smaps.write('.', {sourceRoot: '/source/app/styles'}))
.pipe(gulp.dest('dist/build'));
});
// This moves all the files from public/ to
// dist/build
gulp.task('migrate', function() {
var newer = require('gulp-newer');
return gulp.src(['public/**/*', 'node_modules/diet-tags/img/**/*'])
.pipe(newer('dist/build'))
.pipe(gulp.dest('dist/build'));
});
gulp.task('final', ['build', 'bundle', 'compile', 'migrate'], function() {
var RevAll = require('gulp-rev-all');
var uglify = require('gulp-uglify');
var cssnano = require('gulp-cssnano');
var onlyJs = filter('*.js', {restore: true});
var onlyCss = filter('*.css', {restore: true});
var revAll = new RevAll({
dontSearchFile: ['.js']
});
return gulp.src('dist/build/**/!(*.map)')
.pipe(onlyJs).pipe(uglify()).pipe(onlyJs.restore)
.pipe(onlyCss).pipe(cssnano()).pipe(onlyCss.restore)
.pipe(revAll.revision())
.pipe(gulp.dest('dist/final'))
.pipe(revAll.manifestFile())
.pipe(gulp.dest('dist'));
});
gulp.task('compress', ['final'], function() {
var gzip = require('gulp-gzip');
return gulp.src('dist/final/**/!(*.gz)')
.pipe(gzip())
.pipe(gulp.dest('dist/final'));
});
// This watchs all the files
gulp.task('watch', ['build', 'watch-bundle', 'compile', 'migrate'], function() {
gulp.watch('app/**/*.js', ['build']);
gulp.watch('app/styles/**/*.less', ['compile']);
gulp.watch('public/**/*', ['migrate']);
});
gulp.task('upload', function() {
var awspublish = require('gulp-awspublish');
var rename = require('gulp-rename');
var publisher = awspublish.create({
params: {
Bucket: process.env.GOODYBAG_S3_BUCKET
}
});
const headers = {
// Two Year cache policy (1000 * 60 * 60 * 24 * 730)
'Cache-Control': "max-age=630720000, public",
Expires: new Date(Date.now() + 63072000000).toISOString()
};
return gulp.src('dist/final/**/!(*.gz)')
.pipe(rename(function(path) {
path.dirname += '/assets';
}))
.pipe(awspublish.gzip())
.pipe(publisher.publish(headers))
.pipe(publisher.cache())
.pipe(awspublish.reporter());
});