This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
executable file
·106 lines (96 loc) · 3.2 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
import gulp from 'gulp';
import gulploadplugins from 'gulp-load-plugins';
import runSequence from 'run-sequence';
import yargs from 'yargs';
import browserSync from 'browser-sync';
import browserify from 'browserify';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import path from 'path';
import del from 'del';
import handlebars from 'gulp-compile-handlebars';
const $ = gulploadplugins({
lazy: true
});
const argv = yargs.argv;
function handleError(error) {
$.util.log(error.message);
$.util.log(error.codeFrame);
this.emit('end');
}
// SASS Styles
gulp.task('styles', () => {
return gulp.src([
'src/vendor/**/*.css',
'src/sass/*.scss'
])
.pipe($.changed('.tmp/styles', {extension: '.css'}))
.pipe($.if(!argv.production, $.sourcemaps.init()))
.pipe($.sassVariables({
$production: (argv.production === true)
}))
.pipe($.sass({
precision: 10
}).on('error', $.sass.logError))
.pipe($.autoprefixer({ browsers: ['> 1%', 'last 2 versions'] }))
.pipe(gulp.dest('.tmp'))
// Concatenate and minify styles if production mode (via gulp styles --production)
.pipe($.if('*.css' && argv.production, $.minifyCss()))
.pipe($.if(!argv.production, $.sourcemaps.write()))
.pipe(gulp.dest('public/css'))
.pipe(browserSync.stream())
.pipe($.size({title: 'styles'}));
});
// Scripts - app.js is the main entry point, you have to import all required files and modules
gulp.task('scripts', () => {
return browserify({
entries: 'src/js/app.js',
debug: true
})
.transform('babelify', {presets: ['es2015']})
.bundle()
.on('error', handleError)
.pipe(source('app.js'))
.pipe(buffer())
.pipe($.if(!argv.production, $.sourcemaps.init({loadMaps: true})))
.pipe($.if(argv.production, $.uglify()))
.on('error', handleError)
.pipe($.if(!argv.production, $.sourcemaps.write()))
.pipe(gulp.dest('./public/js'));
});
gulp.task('static', () => {
return gulp.src('src/**/*.{html,php,jpg,jpeg,png,gif,svg,ico,eot,ttf,woff,woff2,csv,tsv,json}').pipe(gulp.dest('public'));
});
gulp.task('templates', () => {
return gulp.src([
'src/**/*.hbs',
'!src/partials/*.hbs'
])
.pipe(handlebars({}, {
batch: 'src/partials'
}))
.pipe($.rename((path) => {
path.extname = '.html'
}))
.pipe(gulp.dest('public'));
});
// Browser-Sync
gulp.task('serve', ['styles', 'scripts', 'templates', 'static'], () => {
browserSync({
notify: false,
server: ['.tmp', 'public']
});
gulp.watch(['src/sass/**/*.{scss,css}'], ['styles']);
gulp.watch(['src/js/**/*.{js,es6}'], ['scripts', browserSync.reload]);
gulp.watch(['src/**/*.hbs'], ['templates', browserSync.reload]);
gulp.watch(['src/**/*.{html,php,jpg,jpeg,png,gif,svg,ico,eot,ttf,woff,woff2,csv,tsv,json}'], ['static', browserSync.reload]).on('change', (event) => {
if(event.type === 'deleted') {
let filePathFromSrc = path.relative(path.resolve('src'), event.path);
let destFilePath = path.resolve('public', filePathFromSrc);
console.log(`deleting ${destFilePath}...`);
del.sync(destFilePath);
}
});
});
gulp.task('build', ['styles', 'scripts', 'templates', 'static']);