Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
230 lines (201 loc) · 6.09 KB
/
Copy pathgulpfile.js
File metadata and controls
230 lines (201 loc) · 6.09 KB
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
// load gulp components
const gulp = require( 'gulp' );
const $ = require( 'gulp-load-plugins' )( {
lazy: true,
} );
const browserSync = require( 'browser-sync' ).create();
// load generic JS components
const imageminJpegRecompress = require( 'imagemin-jpeg-recompress' );
const imageminPngcrush = require( 'imagemin-pngcrush' );
const browserify = require( 'browserify' );
const source = require( 'vinyl-source-stream' );
const buffer = require( 'vinyl-buffer' );
const babel = require( 'babelify' );
const cleanCSS = require( 'gulp-clean-css' );
// load local config
const gConfig = require( './gulp-config' );
// setup paths
const { sources, destinations } = gConfig.paths;
/**
* Utility Tasks & Helper Functions
*
* 1. Plumber Error Handler
*/
// error handler for any errors occurring while using gulp-plumber
const plumberErrorHandler = {
// use gulp-notify to send system notifications w/ error messages.
// means that terminal doesn't have to be open to see issues
errorHandler: $.notify.onError( {
title: 'Gulp',
message: 'Error: <%= error.message %>',
} ),
};
// default task
gulp.task( 'default', ( done ) => {
console.log( 'Gulp is up and running' );
done();
} );
/**
* Pug & HTML Functions
*
* 1. Compile Pug
*/
gulp.task( 'compile-pug', () => (
gulp.src( [sources.pug, '!src/pug/layout.pug'] )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.pug( {} ) )
.pipe( gulp.dest( './' ) )
) );
/**
* CSS & Stylus Tasks
*
* 1. Vet Stylus
* 2. Compile Stylus
* 3. Autoprefix CSS
* 4. Minify CSS
* 5. CSS Master Task (Run All)
*/
// lint the stylus files
gulp.task( 'vet-stylus', () => (
gulp.src( sources.stylus )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.stylint() )
.pipe( $.stylint.reporter() )
.pipe( $.stylint.reporter( 'fail' ) ) // the gulp-process should fail if there are linting errors
) );
// compile the stylus files into style.css, set up sourcemaps
// files must pass linting before they will compile
gulp.task( 'compile-stylus', gulp.series( 'vet-stylus', () => (
gulp.src( sources.stylusM )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.sourcemaps.init() ) // using external sourcemap
.pipe( $.stylus( {} ) )
.pipe( $.sourcemaps.write( '.' ) ) // write to sourcemap
.pipe( gulp.dest( './assets/css' ) )
) ) );
// combine media queries & autoprefix the CSS file
gulp.task( 'autoprefix-css', gulp.series( 'compile-stylus', () => (
gulp.src( destinations.cssM )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.sourcemaps.init() )
.pipe( $.autoprefixer() )
.pipe( $.sourcemaps.write( '.' ) )
.pipe( gulp.dest( destinations.css ) )
) ) );
// minify the CSS file
// this will vet + compile from stylus before minifying
gulp.task( 'minify-css', gulp.series( 'autoprefix-css', () => (
gulp.src( destinations.cssM )
.pipe( cleanCSS( { debug: true } ) )
.pipe( gulp.dest( destinations.css ) )
) ) );
// master CSS task to do all CSS tasks
gulp.task( 'master-css', gulp.parallel( 'minify-css' ) );
/**
* Javascript Tasks
*
* 1. Lint JS
* 2. Compile JS
* 3. Transpile JS
* 4. Compress JS
* 5. Master JS Task (Run All)
*/
// lint javascript
gulp.task( 'lint-javascript', () => (
gulp.src( sources.js )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.eslint() )
.pipe( $.eslint.format() )
.pipe( $.eslint.failAfterError() )
) );
const compile = () => {
const bundler = browserify( sources.jsM, { debug: true } ).transform( babel );
const rebundle = () => {
return bundler.bundle()
.on( 'error', function( err ) { console.error( err ); this.emit( 'end' ) } )
.pipe( source( 'main.js' ) )
.pipe( buffer() )
.pipe( $.sourcemaps.init( { loadMaps: true } ) )
.pipe( $.sourcemaps.write( './' ) )
.pipe( gulp.dest( destinations.js ) );
};
return rebundle();
};
gulp.task( 'transpile-javascript', gulp.series( 'lint-javascript', () => compile() ) );
// minify/uglify javascript
gulp.task( 'compress-javascript', gulp.series( 'transpile-javascript', () => (
gulp.src( destinations.jsM )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.uglify() )
.pipe( gulp.dest( destinations.js ) )
) ) );
// master JS task to do all JS tasks
gulp.task( 'master-js', gulp.parallel( 'compress-javascript' ) );
/**
* Performance & Optimization Tasks
*
* 1. Image Optimization
*/
// image optimization
gulp.task( 'compress-images', () => (
gulp.src( sources.img )
.pipe( $.plumber( plumberErrorHandler ) )
.pipe( $.cache(
$.imagemin(
[
$.imagemin.gifsicle(),
imageminJpegRecompress(),
imageminPngcrush(),
$.imagemin.svgo(),
], {
verbose: true,
},
),
) )
.pipe( gulp.dest( destinations.img ) )
) );
/**
* Browsersync & Watch Tasks
*
* 1. JS Watch
* 2. Stylus Watch
* 3. Image Watch
* 4. Pug Watch
* 5. Master Watch (Run All)
*/
// need watch tasks to make sure browser is reloaded AFTER the file prep is done
gulp.task( 'js-watch', gulp.series( 'transpile-javascript', ( done ) => {
browserSync.reload();
done();
} ) );
gulp.task( 'stylus-watch', gulp.series( 'autoprefix-css', ( done ) => {
browserSync.reload();
done();
} ) );
gulp.task( 'img-watch', gulp.series( 'compress-images', ( done ) => {
browserSync.reload();
done();
} ) );
gulp.task( 'pug-watch', gulp.series( 'compile-pug', ( done ) => {
browserSync.reload();
done();
} ) );
gulp.task( 'watch', gulp.series( 'transpile-javascript', 'autoprefix-css', 'compress-images', 'compile-pug', () => {
browserSync.init( {
server: {
baseDir: './',
},
} );
gulp.watch( sources.stylus, gulp.series( 'stylus-watch' ) );
gulp.watch( sources.js, gulp.series( 'js-watch' ) );
gulp.watch( sources.img, gulp.series( 'img-watch' ) );
gulp.watch( sources.pug, gulp.series( 'pug-watch' ) );
} ) );
/**
* Dev & Build Scripts
*/
// dev script to monitor files, update as needed, and refresh the browser
// just an alias of 'watch' for familiarity of naming conventions
gulp.task( 'dev', gulp.series( 'watch' ) );
// build script to prep all files for deployment
gulp.task( 'build', gulp.series( 'compile-pug', 'compress-images', 'master-js', 'master-css' ) );