-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
200 lines (184 loc) · 6.69 KB
/
gulpfile.babel.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
'use strict';
import gulp from 'gulp';
import gutil from 'gulp-util';
import concat from 'gulp-concat';
import del from 'del';
import changed from 'gulp-changed';
import gulpIf from 'gulp-if';
import rename from 'gulp-rename';
import babel from 'gulp-babel';
import imagemin from 'gulp-imagemin';
import pngquant from 'imagemin-pngquant';
// runs a sequence of gulp tasks in the specified order;
// hack until gulp 4.0 which supports defining task dependencies
import runSequence from 'run-sequence'
import jade from 'gulp-jade';
import minifyCSS from 'gulp-cssnano';
import uncss from 'gulp-uncss';
import autoprefixer from 'gulp-autoprefixer';
import minifyHTML from 'gulp-htmlmin';
// to not get an error, also need to npm install jshint;
// scans a program and reports about commonly made mistakes and potential bugs;
import jshint from 'gulp-jshint';
import uglify from 'gulp-uglify';
// strip console, alert, and debugger statements from JS code;
import stripDebug from 'gulp-strip-debug';
import ngHtml2Js from "gulp-ng-html2js";
// parse build blocks in HTML files to replace references;
// gets all links and all scripts and compiles it into one;
// pagespeed insights with reporting;
import psi from 'psi';
import Pageres from 'pageres';
let paths = {
// need to manually set order of files;
scripts: [
'client/app/app.js',
'client/app/app.routes.js',
'client/app/components/home/home.js',
'client/app/components/navbar/navbar.js',
'client/app/components/my-wishlist/my-wishlist.js',
'client/app/components/faq/faq.js',
'client/app/components/friend-wishlist/friend-wishlist.js',
'client/app/services/user.js',
'client/app/services/starred.js'
],
images: 'client/images/*',
css: 'client/styles/*.css',
components: 'client/app/components/**/*.html',
jade: 'client/**/*.jade'
};
// to make it run synchronously:done
gulp.task('default', (cb) => {
runSequence('clean',
['css', 'scripts'],
'jade',
'components',
'images',
'watch',
cb);
});
gulp.task('clean', () => {
return gutil.log('gulp is running!!')
return del(['tmp'])
return del(['client/dist'])
return del(['responsive-testing'])
});
// done
// try to apply the uncss to the vendor/css
gulp.task('css', () => {
return gulp.src([paths.css])
.pipe(changed('client/dist/css', {extension: '.css'}))
.pipe(autoprefixer({
browsers: ['last 3 versions'],
cascade: false
}))
.pipe(gulp.dest('tmp/css'))
.pipe(rename({suffix: '.min'}))
.pipe(minifyCSS().on('error', gutil.log))
.pipe(gulp.dest('client/dist/css'))
})
// done
gulp.task('scripts', ['jshint'], () => {
return gulp.src(paths.scripts)
// .pipe(changed('client/dist/js'))
.pipe(babel({presets: ['es2015']}))
.pipe(concat('bundle.js'))
.pipe(gulp.dest('tmp/js'))
.pipe(rename({suffix: '.min'}))
.pipe(stripDebug())
.pipe(uglify({mangle: false}).on('error', gutil.log))
.pipe(gulp.dest('client/dist/js'))
});
// done
gulp.task('jshint', () => {
return gulp.src('paths.scripts')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
// compile to HTML:done
// would be awesome to incorporate gulp-useref;
gulp.task('jade', () => {
return gulp.src(paths.jade)
.pipe(changed('client/dist/index'))
// .pipe(useref()) doesn't work: gets rid of comments
// during converting Jade to HTML
.pipe(jade({
pretty: true
}))
.pipe(gulpIf('index.html', gulp.dest('tmp/index')))
.pipe(minifyHTML({
collapseWhitespace:true,
minifyJS:true,
minifyURLs: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
}))
.pipe(rename({suffix: '.min'}))
.pipe(gulpIf('index.min.html', gulp.dest('client/dist/index')))
})
// templatecaching for quick retrieval: done;
gulp.task('components', () => {
return gulp.src(paths.components)
.pipe(changed('client/dist/js/components'))
.pipe(ngHtml2Js({
moduleName: function(file){
let pathParts = file.path.split('/');
let folder = pathParts[pathParts.length - 2];
return folder.replace(/-[a-z]/g, function(match){
return match.substr(1).toUpperCase();
});
}
}))
.pipe(concat('components.js'))
.pipe(gulp.dest('tmp/js'))
.pipe(rename({suffix: '.min'}))
.pipe(stripDebug())
.pipe(uglify({mangle: false}).on('error', gutil.log))
.pipe(gulp.dest('client/dist/js'))
})
// done
gulp.task('images', () => {
return gulp.src(paths.images)
.pipe(changed('client/dist/images'))
.pipe(imagemin({
progressive: true,
interlaced: true,
multipass: true,
optimizationLevel: 5,
svgoPlugins: [
{removeViewBox: false},
{cleanupIDs: false}
],
use: [pngquant()]
}))
.pipe(gulp.dest('client/dist/images'))
})
// gulp.task('heroku:production', ['default'])
gulp.task('watch', () => {
gulp.watch(paths.scripts, ['scripts', 'jshint']);
gulp.watch(paths.css, ['css']);
gulp.watch(paths.images, ['images']);
gulp.watch(paths.jade, ['jade']);
gulp.watch(paths.components, ['components']);
});
// getting the pagespeed insights report
psi('http://giftsgenies.herokuapp.com/#/').then(data => {
console.log(data.ruleGroups.SPEED.score);
console.log(data.pageStats);
})
// output a format report to the terminal
psi.output('http://giftsgenies.herokuapp.com/#/').then(() => {
console.log('donee')
});
// supply options to PSI and get back speed and usability scores;
psi('http://giftsgenies.herokuapp.com/#/', {nokey: 'true', strategy: 'mobile'}).then(data => {
console.log('Speed score: ' + data.ruleGroups.SPEED.score);
console.log('Usability score: ' + data.ruleGroups.USABILITY.score);
});
const pageres = new Pageres({delay: 2})
.src('http://localhost:3000/', ['480x320', '1024x768', 'iphone 6'], {crop: true})
.src('http://giftsgenies.herokuapp.com/#/', ['480x320', '1024x768', 'iphone 6'])
.dest('./responsive-testing')
.run()
.then(() => console.log('done'));