-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathgulpfile.js
285 lines (250 loc) · 7.72 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const gulp = require('gulp');
const gulpCleanCSS = require('gulp-clean-css');
const gulpConcat = require('gulp-concat');
const gulpCsslint = require('gulp-csslint');
const gulpIf = require('gulp-if');
const gulpImagemin = require('gulp-imagemin');
const gulpJshint = require('gulp-jshint');
const gulpJsonEditor = require('gulp-json-editor');
const gulpJsonlint = require('gulp-jsonlint');
const gulpMergeMediaQueries = require('gulp-merge-media-queries');
const gulpPostcss = require('gulp-postcss');
const gulpRev = require('gulp-rev');
const gulpSvgstore = require('gulp-svgstore');
const gulpTaskListing = require('gulp-task-listing');
const gulpUglify = require('gulp-uglify');
const gulpUtil = require('gulp-util');
const gulpWebp = require('gulp-webp');
const imageminPngquant = require('imagemin-pngquant');
const jshintStylish = require('jshint-stylish');
const mergeStream = require('merge-stream');
const postcssCssnext = require('postcss-cssnext');
const postcssImport = require('postcss-import');
const files = {
data : [ 'data/**/*.json' ],
img : [ 'img/**/*' ],
lint : [ 'app.js', 'gulpfile.js', 'js/**/*.js', 'lib/**/*.js' ],
scripts : {
tooling: [
'js/shims/**/*.js',
'js/helper/**/*.js',
'js/featureDetects/**/*.js',
'js/components/**/*.js',
'js/tooling.js'
],
enhance: [
'js/enhance.js'
]
},
sitemap : [ './sitemap.xml' ],
styles : [ 'css/main.css' ],
svg : [ 'svg/icons/*.svg' ],
rev : [ './rev.json' ],
watch : {
styles : [ 'css/**/*.css' ]
},
xml : [ './*.xml' ]
};
/*******************************************************************************
* HELP TASK
*
* This task will list out other available tasks when you run `gulp help`
*/
gulp.task('help', gulpTaskListing);
/*******************************************************************************
* LINT TASK
*
* This task will lint all JS files for common errors
*/
gulp.task( 'lint', () => {
return gulp.src( files.lint )
.pipe( gulpJshint() )
.pipe( gulpJshint.reporter( jshintStylish ) );
});
/*******************************************************************************
* STYLE TASK
*
* this task is responsible for the style files
* - we will concat all css files
* - we will run them through autoprefixer
* - we will minify the css files
* - and save it to public
* - hash the files
* - and save the hash in the rev json file
*/
gulp.task( 'styles', () => {
return gulp.src( files.styles )
.pipe( gulpPostcss([
postcssImport(),
postcssCssnext({
browsers: [ 'last 1 version' ],
features: {
rem: false
}
})
]) )
.pipe( gulpCsslint( '.csslintrc' ) )
.pipe( gulpCsslint.formatter() )
.pipe( gulpMergeMediaQueries( {
log: true
} ) )
.pipe( gulpCleanCSS() )
.pipe( gulpRev() )
.pipe( gulp.dest( 'public/' ) )
.pipe( gulpUtil.buffer( ( err, dataFiles ) => {
return gulp.src( files.rev )
.pipe( gulpJsonEditor( {
'styles': dataFiles.map( dataFile => {
return dataFile.revHash;
} ).join( '' )
} ) )
.pipe( gulp.dest( './' ) );
} ) );
});
/*******************************************************************************
* CSSLINT TASK
*
* this task is responsible for the style files
* - we will lint the generated css
*/
gulp.task( 'csslint', () => {
return gulp.src( files.styles )
.pipe( gulpCsslint( '.csslintrc' ) )
.pipe( gulpCsslint.formatter() )
.pipe( gulpCsslint.formatter('fail') );
});
/*******************************************************************************
* SCRIPT TASKS
*
* this task is responsible for the JavaScript files
* - uglify the js
* - and save it to public
*/
gulp.task( 'scripts', () => {
const streams = Object.keys( files.scripts ).map( element => {
const condition = element === 'tooling';
return gulp.src( files.scripts[element] )
.pipe( gulpConcat( `${element}.js` ) )
.pipe( gulpUglify() )
.pipe( gulpIf(condition, gulpRev() ) )
.pipe( gulp.dest( 'public/' ) )
.pipe( gulpIf( condition, gulpUtil.buffer( ( err, dataFiles ) => {
return gulp.src( files.rev )
.pipe( gulpJsonEditor( {
scripts : dataFiles.map( dataFile => {
return dataFile.revHash;
} ).join( '' )
} ) )
.pipe( gulp.dest( './' ) );
} ) ) );
} );
return mergeStream.apply( null, streams );
});
/*******************************************************************************
* SVG TASK
*
* this task is responsible for crunching all svg's together
* - crunch svg files
* - minify the files
*/
gulp.task( 'svg', () => {
return gulp.src( files.svg )
.pipe( gulpSvgstore() )
.pipe( gulpRev() )
.pipe( gulp.dest( 'public/' ) )
.pipe( gulpUtil.buffer( ( err, dataFiles ) => {
return gulp.src( files.rev )
.pipe( gulpJsonEditor( {
'svg': dataFiles.map( dataFile => {
return dataFile.revHash;
} ).join( '' )
} ) )
.pipe( gulp.dest( './' ) );
} ) );
});
/*******************************************************************************
* IMAGE TASK
*
* this task is responsible for compressing images properly
*/
gulp.task( 'images', () => {
return gulp.src( files.img )
.pipe( gulpImagemin( {
progressive: true,
use: [ imageminPngquant() ]
} ) )
.pipe( gulp.dest( 'public/' ) )
.pipe( gulpWebp() )
.pipe( gulp.dest( 'public/' ) );
});
/*******************************************************************************
* XML TASK
*
* copy xml files over to public
*/
gulp.task( 'xml', () => {
return gulp.src( files.xml )
.pipe( gulp.dest( 'public/' ) );
} );
/*******************************************************************************
* JSONLINT TASK
*
* this task is responsible for compressing images properly
*/
gulp.task( 'jsonlint', () => {
return gulp.src( files.data )
.pipe( gulpJsonlint() )
.pipe( gulpJsonlint.reporter( file => {
gulpUtil.log( `Error on file ${file.path}` );
gulpUtil.log( file.jsonlint.message );
throw new gulpUtil.PluginError(
'gulp-jsonlint',
`JSONLint failed for ${file.relative}`
);
} ) );
} );
/*******************************************************************************
* this task will kick off the watcher for JS, CSS, HTML files
* for easy and instant development
*/
gulp.task( 'watch', () => {
gulp.watch( files.lint, gulp.series( 'lint' ) );
gulp.watch( files.watch.styles, gulp.series( 'styles' ) );
gulp.watch( files.scripts.tooling, gulp.series( 'scripts' ) );
gulp.watch( files.svg, gulp.series( 'svg' ) );
});
/*******************************************************************************
* TEST
*
* task to run in CI environments
*
* $ gulp test
*/
gulp.task( 'test', gulp.parallel('csslint', 'jsonlint') );
/*******************************************************************************
* BUILD
*
* run all build related tasks with:
*
* $ gulp build
*
*/
gulp.task( 'build', gulp.parallel('styles', 'scripts', 'svg', 'images', 'xml') );
/*******************************************************************************
* DEV
*
* run all build related tasks, kick of server at 8080
* and enable file watcher with:
*
* $ gulp dev
*
*/
gulp.task( 'dev', gulp.series('build', 'watch') );
/**
* Default gulp task will run all landingpage task.
* This is just a shorcut for $ gulp landingpage
*
* $ gulp
*
*/
gulp.task( 'default', gulp.series('help' ) );