This repository was archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
408 lines (330 loc) · 8.75 KB
/
gulpfile.js
File metadata and controls
408 lines (330 loc) · 8.75 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* CONFIGURE AUTOPREFIXER SUPPORT
*
*/
const autoprefixSupport = [
'last 2 versions',
'safari >= 8',
'ie >= 10',
'ff >= 20',
'ios 6',
'android 4'
];
/**
* NPM MODULES
*
*/
const argv = require('yargs').argv;
const del = require('del');
const _ = require('lodash');
const gulp = require('gulp');
const gutil = require('gulp-util');
const plumber = require('gulp-plumber');
const $p = require('gulp-load-plugins')();
const $if = require('gulp-if');
const flatmap = require('gulp-flatmap');
const changed = require('gulp-changed');
const rename = require('gulp-rename');
const sequence = require('gulp-sequence');
const rev = require('gulp-rev');
// Sass
const sass = require('gulp-sass');
const maps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const mediaQuery = require('gulp-group-css-media-queries');
// JS
const eslint = require('gulp-eslint');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
// Images
const imagemin = require('gulp-imagemin');
const optipng = require('imagemin-optipng');
const jpegtran = require('imagemin-jpegtran');
const gifsicle = require('imagemin-gifsicle');
// Browserify
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const browserify = require('browserify');
const watchify = require('watchify');
const babelify = require("babelify");
const fs = require('fs');
// Browsersync
const browsersync = require('browser-sync');
/**
* CONFIGURE LOCALHOST URL
* If no localhost url is required, or to just
* run on localhost, set to false
*
*/
let devUrl = argv.devUrl ? argv.devUrl : '127.0.0.1:8443';
/**
* PRODUCTION FLAG
* Run --production after any Gulp task to perform a production build
*
*/
const production = argv.production;
/**
* PATHS
*
*/
const enter_base = 'assets/';
const src_base = './src/';
const dist_base = './dist/';
const build_base = production ? dist_base : src_base;
const assetPath = {
img: {
enter: enter_base + 'images/**/*',
dest: dist_base + 'images/',
},
fonts: {
enter: enter_base + 'fonts/**/*',
dest: dist_base + 'fonts/'
},
sass: {
enter: enter_base + 'sass/main/*.scss',
dest: build_base + 'css/'
},
js: {
main: {
enter: enter_base + 'js/app.js'
},
single: {
base: enter_base + 'js/single/',
enter: enter_base + 'js/single/*.js'
},
modules: enter_base + 'js/modules/**.js',
dest: build_base + 'js/'
}
}
/**
* CLEAN TASK
* Remove `./src` & `/dist`
*
*/
gulp.task('clean', function() {
del([src_base, dist_base]);
});
/**
* SASS TASK
* Runs foreach on every file in 'sass/main'
*
*/
gulp.task('build_css', function() {
gulp.src( assetPath.sass.enter )
.pipe(flatmap( function(stream, file) {
return stream
.pipe( $if( !production, plumber() ) )
.pipe( changed( assetPath.sass.dest ) )
.pipe( $if( !production, maps.init() ) )
.pipe(sass().on('error', sass.logError))
.pipe( $if( production, mediaQuery() ) )
.pipe( autoprefixer({
browsers: autoprefixSupport,
add: true
}) )
.pipe( $if( production, cssnano() ) )
.pipe( $if( production, rename({ suffix: '.min' })) )
.pipe( $if( !production, maps.write('.') ) );
}) )
.pipe( gulp.dest( assetPath.sass.dest ) )
.pipe( browsersync.stream({match: '**/*.css'}) );
});
/**
* LINT TASKS
*
* Run single js through the linter
* Run main browserify file through the linter
*/
function commonLintTasks(inputStream) {
return inputStream
.pipe(eslint())
.pipe(eslint.formatEach())
.pipe( $if( production, eslint.failAfterError() ) )
};
gulp.task('lint_single', () => {
return commonLintTasks( gulp.src( assetPath.js.single.enter ) );
});
gulp.task('lint_main', () => {
return commonLintTasks( gulp.src([assetPath.js.main.enter, assetPath.js.modules]) );
});
/**
* BROWSERIFY CONFIGURATION
* Runs on primary `app.js` file and all single files
*
* @link https://gist.github.com/ramasilveyra/b4309edf809e55121385
*
*/
let isWatchify = false;
let bundles = [
{
entries: assetPath.js.main.enter,
output: 'app.js',
destination: assetPath.js.dest
}
];
// Make each single js file a bundle
fs.readdir(assetPath.js.single.base, (err, files) => {
files.forEach(file => {
if ( file.includes('.js') ) {
let bundle = {
entries: [assetPath.js.single.base + file],
output: file,
destination: assetPath.js.dest
}
bundles.push(bundle);
}
});
});
// Setup Bundles
const createBundle = options => {
// Browersify Options
const opts = _.assign({}, watchify.args, {
entries: options.entries,
extensions: options.extensions,
debug: true,
paths: ['./node_modules', assetPath.js.modules]
});
let b = browserify(opts);
b.transform(babelify.configure({
presets: ["env"]
}));
// Gulp tasks ran on each bundle
const rebundle = () =>
b.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source(options.output))
.pipe(buffer())
.pipe( $if( !production, plumber() ) )
.pipe( $if( !production, maps.init( {loadMaps: true} ) ) )
.pipe(maps.init({ loadMaps: true }))
.pipe( $if( production, uglify() ) )
.pipe( $if( !production, maps.write('.') ) )
.pipe( $if( production, rename({ suffix: '.min' })) )
.pipe(gulp.dest(assetPath.js.dest))
.pipe( browsersync.stream( {once: true} ) );
if (isWatchify) {
b = watchify(b);
b.on('update', rebundle);
b.on('log', gutil.log);
}
return rebundle();
};
let bundleBuildTask = () => {
bundles.forEach( bundle =>
createBundle({
entries: bundle.entries,
output: bundle.output,
extensions: bundle.extensions,
destination: bundle.destination
})
)
}
/**
* BROWSERIFY BUILD TASK
* Builds main app.js and all single scripts
*
*/
gulp.task('build_bundles', ['lint_main', 'lint_single'], () => {
bundleBuildTask();
});
/**
* WATCHIFY TASK
* Builds main app.js and all single scripts and then runs them
*
*/
gulp.task('watch_bundles', () => {
isWatchify = true;
bundleBuildTask();
});
/**
* REVISION CSS AND JS ASSETS
*
* Revision all asset names in `dist/css` and `dist/js`
* by appending content hash to filenames. New file names
* are stored in `dist/_rev-manifest.json`
*
* This task should only be used within a production run
* of the default gulp task: `gulp --production`
*
*/
gulp.task('build_rev', function () {
if ( production ) {
var cssPath = assetPath.sass.dest + '*.css';
var jsPath = assetPath.js.dest + '*.js';
return gulp.src([cssPath, jsPath], {base: dist_base})
.pipe(rev())
.pipe(gulp.dest(dist_base))
.pipe(rev.manifest('_rev-manifest.json'))
.pipe(gulp.dest(dist_base));
}
});
/**
* IMAGE TASK
* Process images in src folder, move to dist folder
*
*/
gulp.task('build_images', function () {
return gulp.src( assetPath.img.enter )
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false},
{cleanupNumericValues: {floatPrecision: 2}}
]
})
]))
.pipe(gulp.dest(assetPath.img.dest));
});
/**
* COPY STATIC FILES
*
* Copy jQuery for local fallback
* Copy Fonts to distribution folder
*
*/
gulp.task('copy_jquery', function() {
return gulp.src('./node_modules/jquery/dist/jquery.min.js')
.pipe( gulp.dest( assetPath.js.dest ) )
});
gulp.task('copy_fonts', function () {
return gulp.src( assetPath.fonts.enter ).pipe(gulp.dest(assetPath.fonts.dest));
});
/**
* WATCH RELATED TASKS
* Trigger BrowserSync to reload
*
*/
gulp.task('watch_reload', function(){ browsersync.reload(); });
/**
* SERVE TASK
* Initialize browsersync, watch for file changes
*
*/
gulp.task('serve', ['build_css', 'watch_bundles'], function(){
let opts = {
proxy: devUrl
};
browsersync(opts);
// Watch tasks
gulp.watch([assetPath.sass.enter], ['build_css']);
gulp.watch([assetPath.fonts.enter], ['copy_fonts']);
gulp.watch([assetPath.img.enter], ['build_images']);
gulp.watch('**/*.php', ['watch_reload']);
gulp.watch('**/*.html', ['watch_reload']);
});
/**
* DEFAULT TASK
* Start the whole show. Run to start a project up.
*
*/
gulp.task('default', ['clean'], sequence(
['build_bundles'],
['build_css', 'build_images'],
['copy_jquery', 'copy_fonts'],
['build_rev']
));