-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
238 lines (212 loc) · 6.9 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var autoprefixer = require('gulp-autoprefixer');
var sass = require('gulp-sass');
var ejs = require('gulp-ejs');
var imagemin = require('gulp-imagemin');
var cleanCSS = require('gulp-clean-css');
var rename = require('gulp-rename');
var server = require('gulp-server-livereload');
var livereload = require('gulp-livereload');
var ngAnnotate = require('gulp-ng-annotate');
var sourcemaps = require('gulp-sourcemaps');
var svgMin = require('gulp-svgmin');
var inlineSvg = require('gulp-inline-svg');
var gutil = require('gulp-util');
var uglify = require('gulp-uglify');
var templateCache = require('gulp-angular-templatecache');
var del = require('del');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var babelify = require('babelify');
var browserify = require('browserify');
var envify = require('envify');
var watchify = require('watchify');
var _ = require('lodash');
var s3Upload = require('./customGulpPlugins/s3Upload');
var cloudFrontInvalidation = require('./customGulpPlugins/cloudFrontInvalidation');
var env;
var ENV = {
DEV : 'dev',
PROD : 'prod'
};
var version = require('./package.json').version;
var baseUrl;
var resourcesUrl;
var serverPort = '8000';
var files = {
views : [ 'src/views/**/*.ejs', '!src/views/index.ejs' ],
indexView : 'src/views/index.ejs',
sass : 'src/sass/**/*.scss',
app : 'src/js/app.js',
js : 'src/js/**/*.js',
favicon : 'src/images/favicon.ico',
icons : 'src/icons/**/*.svg',
textures : 'src/images/scaled/textures/**/*.jpg'
};
TEMPLATE_MODULE = 'FantasyBach.templateCache';
TEMPLATE_HEADER = 'import angular from "angular"; export default angular.module("<%= module %>", []).run(["$templateCache", function($templateCache) {';
TEMPLATE_FOOTER = '}]).name;';
gulp.task('clean', function() {
return del(['build']);
});
gulp.task('indexView', function() {
verifyEnvSet();
return gulp.src(files.indexView)
.pipe(ejs({
version : version,
resourcesUrl : resourcesUrl
}, {
ext : '.html'
}))
.pipe(gulp.dest('build'))
.pipe(livereload());
});
gulp.task('views', function() {
verifyEnvSet();
return gulp.src(files.views)
.pipe(ejs({
version : version,
resourcesUrl : resourcesUrl
}, {
ext : '.html'
}))
.pipe(templateCache({
module : TEMPLATE_MODULE,
templateHeader : TEMPLATE_HEADER,
templateFooter : TEMPLATE_FOOTER
}))
.pipe(gulp.dest('src/js/build'))
.pipe(livereload());
});
gulp.task('favicon', function() {
return gulp.src(files.favicon)
.pipe(gulp.dest('./build/images'));
});
gulp.task('icons', function() {
verifyEnvSet();
return gulp.src(files.icons)
.pipe(svgMin())
.pipe(inlineSvg({
filename: '_icons.scss'
}))
.pipe(gulp.dest('src/sass/build'))
.pipe(livereload());
});
gulp.task('sass', ['icons'], function() {
verifyEnvSet();
return gulp.src(files.sass)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(cleanCSS({
timeout : 15
}))
.pipe(env === ENV.DEV ? sourcemaps.write() : gutil.noop())
.pipe(rename({
extname : '.min.css'
}))
.pipe(gulp.dest('build/css'))
.pipe(livereload());
});
gulp.task('watch', function() {
livereload.listen();
gulp.watch(files.indexView, ['indexView']);
gulp.watch(files.views, ['views']);
gulp.watch(files.sass, ['sass']);
gulp.watch(files.icons, ['icons']);
// Uses watchify
// gulp.watch(files.js, ['bundle']);
});
gulp.task('bundle', [ 'views' ], function() {
verifyEnvSet();
var isDev = env === ENV.DEV;
var bundler = browserify({
entries: [files.app],
debug: isDev,
cache: {}, // required for watchify
packageCache: {}, // required for watchify
fullPaths: isDev, // required to be true only for watchify
sourceType: 'module' // required for ES6
});
if (isDev) {
bundler = watchify(bundler);
}
bundler
.transform(babelify, { presets : ['es2015'] })
.transform(envify);
bundler.on('update', bundle); // on any dep update, runs the bundler
bundler.on('log', gutil.log); // output build logs to terminal
return bundle();
function bundle() {
return bundler.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(ngAnnotate())
.pipe(isDev ? gutil.noop() : uglify({ mangle : true }))
.pipe(isDev ? sourcemaps.write() : gutil.noop())
.pipe(rename({
extname : '.min.js'
}))
.pipe(gulp.dest('build/js'))
.pipe(livereload());
}
});
gulp.task('server', function() {
return gulp.src('build')
.pipe(server({
livereload : false,
port : serverPort
}));
});
var verifyEnvSet = function() {
if (env) { return; }
throw new Error('Environment not set. Please run "env.dev" or "env.prod" before this task.');
};
gulp.task('env.dev', function() {
env = ENV.DEV;
process.env.NODE_ENV = 'development';
baseUrl = 'http://localhost:' + serverPort;
resourcesUrl = baseUrl;
});
gulp.task('env.prod', function() {
env = ENV.PROD;
process.env.NODE_ENV = 'production';
process.env.AWS_PROFILE = 'fantasy-bach';
baseUrl = 'https://resources.fantasybach.com';
resourcesUrl = baseUrl + '/' + version;
});
gulp.task('s3Upload.index', [ 'indexView' ], function() {
verifyEnvSet();
return gulp.src('build/index.html')
.pipe(s3Upload({
region : 'us-west-2',
bucket : 'fantasybach.com'
}));
});
gulp.task('s3Upload.resources', [ 'favicon', 'views', 'sass', 'bundle' ], function() {
verifyEnvSet();
return gulp.src([ 'build/**', '!build/index.html'])
.pipe(s3Upload({
region : 'us-west-2',
baseUrl : '/' + version,
bucket : 'resources.fantasybach.com'
}));
});
gulp.task('s3Upload', [ 's3Upload.index', 's3Upload.resources']);
gulp.task('cloudFrontInvalidation', [ 's3Upload.index' ], function() {
verifyEnvSet();
return gulp.src('build/index.html')
.pipe(cloudFrontInvalidation({
region : 'us-west-2',
distributionId : 'E2UF7SN1N7PFM3'
}));
});
gulp.task('develop', ['env.dev', 'watch', 'favicon', 'indexView', 'views', 'sass', 'bundle', 'server']);
gulp.task('deploy', ['env.prod', 'favicon', 'indexView', 'views', 'sass', 'bundle', 's3Upload', 'cloudFrontInvalidation']);