-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
127 lines (115 loc) · 3.27 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
const
gulp = require( 'gulp' ),
babel = require( 'gulp-babel' ),
browserSync = require( 'browser-sync' ),
emittyPug = require( 'emitty' ).setup( 'dev', 'pug' ),
emittyScss = require( 'emitty' ).setup( 'dev', 'scss' ),
plumber = require( 'gulp-plumber' ),
sass = require( 'gulp-sass' ),
pug = require( 'gulp-pug' ),
rename = require( 'gulp-rename' ),
gulpIf = require( 'gulp-if' );
function errorHandler ( error ) {
let
errTitle = `${error.plugin.toUpperCase()} Error`,
errMessage = '';
switch ( error.plugin ) {
case 'gulp-sass':
errMessage = `${errTitle}\n${error.messageOriginal}\nAt: ${error.line}:${error.column}\nFile: ${error.relativePath}`;
break;
case 'gulp-pug':
errMessage =
( error.message ? error.message : error.name ) +
( ( error.line && error.column ) ? ('\rAt: '+ error.line +':'+ error.column) : '' ) +
( error.filename ? ('\rFile: '+ error.filename) : ('\rFile: '+ error.path) );
break;
default:
errMessage = JSON.stringify( error, null, 2 );
break;
}
console.log( `${errTitle}\n${errMessage}` );
}
function compileSass() {
return new Promise( ( resolve, reject ) => {
emittyScss.scan( global.emittyScssFile ).then( () => {
return gulp.src( 'dev/**/!(_)*.scss' )
.pipe( plumber({ errorHandler: errorHandler }) )
.pipe( gulpIf( global.watch, emittyScss.filter( global.emittyScssFile ) ) )
.pipe( sass({ outputStyle: 'expanded', indentType: 'tab', indentWidth: 1, linefeed: 'cr' }) )
.on( 'error', resolve )
.pipe( gulp.dest( 'dev' ) )
.on( 'end', function () {
browserSync.reload( '*.css' );
resolve();
});
});
});
}
function compilePug () {
return new Promise( ( resolve, reject ) => {
emittyPug.scan( global.emittyPugFile ).then( () => {
gulp.src( 'dev/**/!(_)*.pug' )
.pipe( plumber({ errorHandler: errorHandler }) )
.pipe( gulpIf( global.watch, emittyPug.filter( global.emittyPugFile ) ) )
.pipe( pug({ pretty: true, verbose: true, self: true, }) )
.pipe( gulp.dest( 'dev' ) )
.on( 'end', function () {
browserSync.reload();
resolve();
})
});
});
}
function transpileJs () {
return gulp.src( [ 'dev/Counter.js', 'dev/ProgressCircle.js', 'dev/Countdown.js' ] )
.pipe( plumber({ errorHandler: errorHandler }) )
.pipe( babel({
minified: true,
comments: false,
presets: [[
'@babel/preset-env',
{
"targets": {
"ie": "10",
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
}
}
]]
}))
.pipe( rename({ suffix: '.min' }) )
.pipe( gulp.dest( 'dist' ) );
}
function defaultTask() {
global.watch = true;
browserSync.init({
server: {
baseDir: `./dev`,
directory: false
},
port: 8000,
open: false,
notify: true,
reloadDelay: 0,
ghostMode: {
clicks: false,
forms: false,
scroll: false
}
});
gulp.watch( 'dev/**/*.scss', compileSass ).on( 'all', function ( event, filepath ) {
global.emittyScssFile = filepath;
});
gulp.watch( 'dev/**/*.pug', compilePug ).on( 'all', function ( event, filepath ) {
global.emittyPugFile = filepath;
});
gulp.watch( 'dev/**/!(*.min).js' ).on( 'change', function () {
browserSync.reload();
});
}
exports.default = defaultTask;
exports.sass = compileSass;
exports.pug = compilePug;
exports.transpileJs = transpileJs;