This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gulpfile.js
executable file
·278 lines (243 loc) · 6.22 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
/**
* Settings
*
* Setup your project paths and requirements here
*/
var settings = {
// react
componentpath: ['components/**/*.jsx', 'components/**/*.js'],
js: './components/vip-dashboard.jsx',
jspath: 'assets/js/',
// path to main scss file
scss: 'components/style.scss',
// path to output css file
css: 'assets/css/style.css',
// path to watch for changed scss files
scsswatch: 'components/**/*.scss',
// path to output css folder
csspath: 'assets/css/',
// path to images
imagespath: 'src/img/',
imagesdistpath: 'assets/img/',
// path to base
basepath: './',
// path to html
htmlpath: ['./*.html', './*.php'],
// enable the static file server and browsersync
// check for unused styles in static html? - seems buggy, requires html
staticserver: false,
checkunusedcss: false,
// enable the proxied local server for browsersync
// static above server must be disabled
proxyserver: true,
proxylocation: 'vip.w.dev'
};
/**
* Load node modules
*/
var gulp = require( 'gulp' ),
// Plugins
assign = require( 'lodash.assign' ),
autoprefixer = require( 'gulp-autoprefixer' ),
browserify = require( 'browserify' ),
browsersync = require( 'browser-sync' ),
buffer = require( 'vinyl-buffer' ),
checkcss = require( 'gulp-check-unused-css' ),
concat = require( 'gulp-concat' ),
csscomb = require( 'gulp-csscomb' ),
eslint = require( 'gulp-eslint' ),
filter = require( 'gulp-filter' ),
imagemin = require( 'gulp-imagemin' ),
install = require( 'gulp-install' ),
minifycss = require( 'gulp-minify-css' ),
parker = require( 'gulp-parker' ),
plumber = require( 'gulp-plumber' ),
react = require( 'gulp-react' ),
sass = require( 'gulp-sass' ),
source = require( 'vinyl-source-stream' ),
reactify = require( 'reactify' ),
sourcemaps = require( 'gulp-sourcemaps' ),
sync = require( 'gulp-config-sync' ),
uglify = require( 'gulp-uglify' ),
util = require( 'gulp-util' ),
watch = require( 'gulp-watch' ),
watchify = require( 'watchify' );
/**
* Generic error handler used by plumber
*
* Display an OS notification and sound with error message
*/
var onError = function( err ) {
if ( err.lineNumber ) {
util.log( util.colors.red( 'Error: (Line: ' + err.lineNumber + ') ' + err.message ) );
} else {
util.log( util.colors.red( 'Error: ' + err.message ) );
}
this.emit( 'end' );
};
/**
* Default Task
*
* Watch for changes and run tasks
*/
gulp.task( 'default', function() {
// Install
gulp.start( 'install' );
// Compile Styles on start
gulp.start( 'styles' );
// Process Images on start
gulp.start( 'images' );
// Process react on start
gulp.start( 'react' );
// Browsersync and local server
// Options: http://www.browsersync.io/docs/options/
if ( settings.staticserver ) {
browsersync( {
server: settings.basepath
} );
// Check to see if the CSS is being used
if ( settings.checkunusedcss ) {
gulp.watch( settings.css, ['checkcss'] );
}
}
if ( settings.proxyserver ) {
browsersync( {
proxy: settings.proxylocation
} );
}
// Watch for SCSS changes
gulp.watch( settings.scsswatch, ['styles'] );
// Watch for image changes
gulp.watch( settings.imagespath, ['images'] );
// Watch for HTML changes
gulp.watch( settings.htmlpath, ['markup'] );
// Watch for react components
gulp.watch( settings.componentpath, ['react'] );
} );
/**
* Install Task
* Ensure our packages are upto date
*/
gulp.task( 'install', function() {
gulp.src( ['./package.json'] )
.pipe( install() );
} );
/**
* Stylesheet Task
*
* SCSS -> CSS
* Autoprefix
* CSSComb
* Sourcemaps
* Minify
* Report
*/
gulp.task( 'styles', function() {
return gulp.src( settings.scss )
.pipe( plumber( {errorHandler: onError} ) )
.pipe( sass( {
style: 'expanded',
errLogToConsole: false
} ) )
.pipe( sourcemaps.init() )
.pipe( autoprefixer( 'last 2 versions', 'ie 8', 'ie 9' ) )
.pipe( csscomb() )
.pipe( sourcemaps.write( './' ) )
.pipe( minifycss() )
.pipe( gulp.dest( settings.csspath ) )
.pipe( filter( '**/*.css' ) )
.pipe( browsersync.reload( {stream: true} ) )
.pipe( parker() );
} );
/**
* React Tast
*
* Compile JSX etc
*/
var reactopts = {
entries: [settings.js],
debug: true,
extensions: ['.jsx']
};
var opts = assign( {}, watchify.args, reactopts );
var b = watchify( browserify( opts ) );
b.transform( reactify );
//b.on('log', util.log); // output build logs to terminal
gulp.task( 'react', ['lint', 'set-node-env'], function() {
return b.bundle()
.on( 'error', onError )
.pipe( source( 'vip-dashboard.js' ) )
.pipe( buffer() )
.pipe( sourcemaps.init( {loadMaps: true} ) )
.pipe( uglify() )
.pipe( sourcemaps.write( './' ) )
.pipe( gulp.dest( settings.jspath ) )
.pipe( browsersync.reload( {stream: true} ) );
} );
/**
* Set env variable for production
*/
gulp.task( 'set-node-env', function() {
return process.env.NODE_ENV = 'production';
} );
/**
* Compress the JS
*/
gulp.task( 'compress', function() {
return gulp.src( settings.jspath + 'vip-dashboard.js' )
.pipe( uglify() )
.pipe( gulp.dest( settings.jspath ) );
} );
/**
* Lint Task
*
* Run before react task above to check for errors
*/
gulp.task( 'lint', function() {
return gulp.src( settings.componentpath )
.pipe( eslint( {
baseConfig: {
ecmaFeatures: {
jsx: true
}
}
} ) )
.pipe( eslint.format( ) )
.pipe( eslint.failAfterError( ) )
.on( 'error', onError );
} );
/**
* Images Task
*
* Run independantly when you want to optimise image assets
*/
gulp.task( 'images', function() {
return gulp.src( settings.imagespath + '**/*.{gif,jpg,png}' )
.pipe( plumber( {errorHandler: onError} ) )
.pipe( imagemin( {
progressive: true,
interlaced: true,
//svgoPlugins: [ {removeViewBox:false}, {removeUselessStrokeAndFill:false} ]
} ) )
.pipe( gulp.dest( settings.imagesdistpath ) )
.pipe( browsersync.reload( {stream: true} ) );
} );
/**
* CheckCSS Task
*
* Are all our styles being used correctly?
*/
gulp.task( 'checkcss', function() {
return gulp.src( [ settings.css, settings.staticlocation + '*.html' ] )
.pipe( plumber( {errorHandler: onError} ) )
.pipe( checkcss() );
} );
/**
* Reload HTML files
*
* If modified, refreshes HTML files
*/
gulp.task( 'markup', function() {
return gulp.src( settings.htmlpath )
.pipe( browsersync.reload( {stream: true} ) );
} );