forked from jbutko/AngularJS-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
270 lines (245 loc) · 6.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @author Jozef Butko
* @url www.jozefbutko.com/resume
* @date March 2015
* @license MIT
*
* AngularJS Boilerplate: Build, watch and other useful tasks
*
* The build process consists of following steps:
* 1. clean /_build folder
* 2. compile SASS files, minify and uncss compiled css
* 3. copy and minimize images
* 4. minify and copy all HTML files into $templateCache
* 5. build index.html
* 6. minify and copy all JS files
* 7. copy fonts
* 8. show build folder size
*
*/
var gulp = require('gulp'),
browserSync = require('browser-sync'),
reload = browserSync.reload,
$ = require('gulp-load-plugins')(),
del = require('del'),
runSequence = require('run-sequence');
// optimize images
gulp.task('images', function() {
return gulp.src('./images/**/*')
.pipe($.changed('./_build/images'))
.pipe($.imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
}))
.pipe(gulp.dest('./_build/images'));
});
// browser-sync task, only cares about compiled CSS
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./"
}
});
});
// minify JS
gulp.task('minify-js', function() {
gulp.src('js/*.js')
.pipe($.uglify())
.pipe(gulp.dest('./_build/'));
});
// minify CSS
gulp.task('minify-css', function() {
gulp.src(['./styles/**/*.css', '!./styles/**/*.min.css'])
.pipe($.rename({suffix: '.min'}))
.pipe($.minifyCss({keepBreaks:true}))
.pipe(gulp.dest('./styles/'))
.pipe(gulp.dest('./_build/css/'));
});
// minify HTML
gulp.task('minify-html', function() {
var opts = {
comments: true,
spare: true,
conditionals: true
};
gulp.src('./*.html')
.pipe($.minifyHtml(opts))
.pipe(gulp.dest('./_build/'));
});
// copy fonts from a module outside of our project (like Bower)
gulp.task('fonts', function() {
gulp.src('./fonts/**/*.{ttf,woff,eof,eot,svg}')
.pipe($.changed('./_build/fonts'))
.pipe(gulp.dest('./_build/fonts'));
});
// start webserver
gulp.task('server', function(done) {
return browserSync({
server: {
baseDir: './'
}
}, done);
});
// start webserver from _build folder to check how it will look in production
gulp.task('server-build', function(done) {
return browserSync({
server: {
baseDir: './_build/'
}
}, done);
});
// delete build folder
gulp.task('clean:build', function (cb) {
del([
'./_build/'
// if we don't want to clean any file we can use negate pattern
//'!dist/mobile/deploy.json'
], cb);
});
// concat files
gulp.task('concat', function() {
gulp.src('./js/*.js')
.pipe($.concat('scripts.js'))
.pipe(gulp.dest('./_build/'));
});
// SASS task, will run when any SCSS files change & BrowserSync
// will auto-update browsers
gulp.task('sass', function() {
return gulp.src('styles/style.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
style: 'expanded'
}))
.on('error', $.notify.onError({
title: 'SASS Failed',
message: 'Error(s) occurred during compile!'
}))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('styles'))
.pipe(reload({
stream: true
}))
.pipe($.notify({
message: 'Styles task complete'
}));
});
// SASS Build task
gulp.task('sass:build', function() {
var s = $.size();
return gulp.src('styles/style.scss')
.pipe($.sass({
style: 'compact'
}))
.pipe($.autoprefixer('last 3 version'))
.pipe($.uncss({
html: ['./index.html', './views/**/*.html', './components/**/*.html'],
ignore: [
'.index',
'.slick',
/\.owl+/,
/\.owl-next/,
/\.owl-prev/
]
}))
.pipe($.minifyCss({
keepBreaks: true,
aggressiveMerging: false,
advanced: false
}))
.pipe($.rename({suffix: '.min'}))
.pipe(gulp.dest('_build/css'))
.pipe(s)
.pipe($.notify({
onLast: true,
message: function() {
return 'Total CSS size ' + s.prettySize;
}
}));
});
// BUGFIX: warning: possible EventEmitter memory leak detected. 11 listeners added.
require('events').EventEmitter.prototype._maxListeners = 100;
// index.html build
// script/css concatenation
gulp.task('usemin', function() {
return gulp.src('./index.html')
// add templates path
.pipe($.htmlReplace({
'templates': '<script type="text/javascript" src="js/templates.js"></script>'
}))
.pipe($.usemin({
css: [$.minifyCss(), 'concat'],
libs: [$.uglify()],
nonangularlibs: [$.uglify()],
angularlibs: [$.uglify()],
appcomponents: [$.uglify()],
mainapp: [$.uglify()]
}))
.pipe(gulp.dest('./_build/'));
});
// make templateCache from all HTML files
gulp.task('templates', function() {
return gulp.src([
'./**/*.html',
'!bower_components/**/*.*',
'!node_modules/**/*.*',
'!_build/**/*.*'
])
.pipe($.minifyHtml())
.pipe($.angularTemplatecache({
module: 'boilerplate'
}))
.pipe(gulp.dest('_build/js'));
});
// reload all Browsers
gulp.task('bs-reload', function() {
browserSync.reload();
});
// calculate build folder size
gulp.task('build:size', function() {
var s = $.size();
return gulp.src('./_build/**/*.*')
.pipe(s)
.pipe($.notify({
onLast: true,
message: function() {
return 'Total build size ' + s.prettySize;
}
}));
});
// default task to be run with `gulp` command
// this default task will run BrowserSync & then use Gulp to watch files.
// when a file is changed, an event is emitted to BrowserSync with the filepath.
gulp.task('default', ['browser-sync', 'sass', 'minify-css'], function() {
gulp.watch('styles/*.css', function(file) {
if (file.type === "changed") {
reload(file.path);
}
});
gulp.watch(['*.html', 'views/*.html'], ['bs-reload']);
gulp.watch(['app/*.js', 'components/**/*.js', 'js/*.js'], ['bs-reload']);
gulp.watch('styles/**/*.scss', ['sass', 'minify-css']);
});
/**
* build task:
* 1. clean /_build folder
* 2. compile SASS files, minify and uncss compiled css
* 3. copy and minimize images
* 4. minify and copy all HTML files into $templateCache
* 5. build index.html
* 6. minify and copy all JS files
* 7. copy fonts
* 8. show build folder size
*
*/
gulp.task('build', function(callback) {
runSequence(
'clean:build',
'sass:build',
'images',
'templates',
'usemin',
'fonts',
'build:size',
callback);
});