-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
268 lines (256 loc) · 5.91 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
/**
* The build process consists of following steps:
* 1. clean /_build folder
* 2. minify css files
* 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');
var eslint = require('gulp-eslint');
var browserSync = require('browser-sync').create();
var del = require('del');
var size = require('gulp-size');
var changed = require('gulp-changed');
var imagemin = require('gulp-imagemin');
var notify = require('gulp-notify');
var htmlmin = require('gulp-htmlmin');
var angularTemplateCache = require('gulp-angular-templatecache');
var htmlReplace = require('gulp-html-replace');
var usemin = require('gulp-usemin');
var cleanCss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var jsConfig = {
extends: 'eslint:recommended',
rules: {
'quotes': [1, 'single'],
'curly': [1, 'multi-line'],
'no-extra-parens': 1,
'key-spacing': [1, {
'beforeColon': false,
'afterColon': true,
'mode': 'strict'
}],
'no-multi-spaces': 1,
'dot-location': [1, 'property'],
'max-len': [1, { 'code': 80, 'tabWidth': 2 }],
'no-trailing-spaces': 1,
'indent': [1, 'tab'],
'no-empty-function': 2,
'eol-last': [2, 'always'],
'semi': [2, 'always'],
'no-multiple-empty-lines': 2
}
};
// lint JS
function lint() {
return gulp.src(['app/**/*.js',
'components/**/*.js', 'js/**/*.js',
'!js/nonangular/*'])
.pipe(eslint(jsConfig))
.pipe(eslint.format());
}
function lintBuild() {
return gulp.src(['app/**/*.js',
'components/**/*.js', 'js/**/*.js',
'!js/nonangular/*'])
.pipe(eslint(jsConfig))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
}
// clean
function cleanBuild(done) {
del([
'./_build/'
]);
done();
}
// fonts
function fonts() {
var fonts = size();
return gulp.src('./fonts/*.{ttf,woff,woff2,eof,eot,svg}')
.pipe(changed('./_build/fonts'))
.pipe(gulp.dest('./_build/fonts'))
.pipe(fonts)
.pipe(notify({
onLast: true,
message: function() {
return "Total Font size " + fonts.prettySize;
}
}));
}
// images
function images() {
var images = size();
return gulp.src('./images/*.{gif,jpg,jpeg,png}')
.pipe(changed('./_build/images'))
.pipe(imagemin({
verbose: true
}))
.pipe(gulp.dest('./_build/images'))
.pipe(images)
.pipe(notify({
onLast: true,
message: function() {
return "Minified Image size " + images.prettySize;
}
}));
}
// template cache
function cacheTemplate() {
var template = size();
return gulp.src([
'./**/*.html',
'!bower_components/**/*.*',
'!node_modules/**/*.*',
'!_build/**/*.*'
])
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(angularTemplateCache({
module: 'boilerplate'
}))
.pipe(gulp.dest('_build/js'))
.pipe(template)
.pipe(notify({
onLast: true,
message: function() {
return "Build Template size " + template.prettySize;
}
}));
}
// minify CSS and JS
function minCssJs() {
return gulp.src('./index.html')
// add templates path
.pipe(htmlReplace({
'templates': '<script src="js/templates.js"></script>'
}))
.pipe(usemin({
css: [cleanCss({compatibility: 'ie8'}), 'concat'],
libs: [uglify()],
nonangularlibs: [uglify()],
angularlibs: [uglify()],
appcomponents: [uglify()],
mainapp: [uglify()]
}))
.pipe(gulp.dest('./_build/'));
}
// File sizes
function sourceTemplateSize() {
var sourceTemplate = size();
return gulp.src(['*.html',
'components/**/*.html', 'views/**/*.html'])
.pipe(sourceTemplate)
.pipe(notify({
onLast: true,
message: function() {
return 'Source Template size ' + sourceTemplate.prettySize;
}
}));
}
function sourceJsSize() {
var sourceJs = size();
return gulp.src(['app/**/*.js',
'components/**/*.js', 'js/**/*.js'])
.pipe(sourceJs)
.pipe(notify({
onLast: true,
message: function() {
return 'Source JS size ' + sourceJs.prettySize;
}
}));
}
function sourceCssSize() {
var sourceCss = size();
return gulp.src('./styles/**/*.css')
.pipe(sourceCss)
.pipe(notify({
onLast: true,
message: function() {
return 'Source CSS size ' + sourceCss.prettySize;
}
}));
}
function buildCssSize() {
var buildCss = size();
return gulp.src('./_build/css/style.min.css')
.pipe(buildCss)
.pipe(notify({
onLast: true,
message: function() {
return 'Build CSS size ' + buildCss.prettySize;
}
}));
}
function buildJsSize() {
var buildJs = size();
return gulp.src('./_build/js/*.js')
.pipe(buildJs)
.pipe(notify({
onLast: true,
message: function() {
return 'Build JS size ' + buildJs.prettySize;
}
}));
}
function buildSize() {
var build = size();
return gulp.src('./_build/**/*.*')
.pipe(build)
.pipe(notify({
onLast: true,
message: function() {
return 'Entire build size ' + build.prettySize;
}
}));
}
// sync to browser
// no return needed
function syncSource(done) {
browserSync.init({
server: {
baseDir: "./"
},
port: 3000
});
done();
}
function syncBuild(done) {
browserSync.init({
server: {
baseDir: "./_build/"
},
port: 3000
});
done();
}
function syncReload(done) {
browserSync.reload();
done();
}
// watch
// no return
// no callback
function watch() {
gulp.watch(['*.html', 'components/**/*.html', 'views/**/*.html'],
gulp.parallel(syncReload, sourceTemplateSize));
gulp.watch(['app/**/*.js', 'components/**/*.js', 'js/**/*.js'],
gulp.parallel(syncReload, sourceJsSize, lint));
gulp.watch('./styles/**/*.css',
gulp.parallel(syncReload, sourceCssSize));
}
// define output tasks
exports.default = gulp.parallel(watch, syncSource);
exports.serveSource = syncSource;
exports.build = gulp.series(lintBuild, cleanBuild,
cacheTemplate, minCssJs, images, fonts,
gulp.parallel(buildCssSize, buildJsSize, buildSize));
exports.serveBuild = gulp.series(lintBuild, cleanBuild,
cacheTemplate, minCssJs, images, fonts,
gulp.parallel(buildCssSize, buildJsSize, buildSize, syncBuild));