-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
executable file
·128 lines (119 loc) · 2.72 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import gulp from 'gulp';
import plumber from 'gulp-plumber';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import flexbugsFixes from 'postcss-flexbugs-fixes';
import sass from 'gulp-sass';
import rename from 'gulp-rename';
import notify from 'gulp-notify';
import browserify from 'browserify';
import babelify from 'babelify';
import uglify from 'gulp-uglify';
import source from 'vinyl-source-stream';
import sourcemaps from 'gulp-sourcemaps';
import buffer from 'vinyl-buffer';
import browserSync from 'browser-sync';
import del from 'del';
const server = browserSync.create();
/* config vars */
const config = {
plugins: [
autoprefixer({
browsers: 'last 2 versions',
}),
flexbugsFixes,
cssnano({
discardComments: {
removeAll: true,
},
zindex: false,
}),
],
};
/**
* handle errors compilation, task errors and don't stop the gulp task
*/
const onError = function(err) {
console.log('Se ha producido un error: ', err.message);
this.emit('end');
};
/**
* Compile sass files and add through postcss plugins minify, autoprefix and resolve some flex issues
*/
function styles() {
return gulp
.src('./src/styles/main.scss')
.pipe(
plumber({
errorHandler: onError,
})
)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(postcss(config.plugins))
.pipe(rename('main.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('public/'))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Styles task ended' }));
}
/**
* Clean partials dir
*/
gulp.task('clean:partials', function() {
return del(['public/front/css/partials/**/*.*']);
});
/**
* Bundling of javascript files
*/
function scripts() {
return browserify('./src/js/main.js')
.transform(babelify, {
presets: ['env'],
})
.bundle()
.on(
'error',
notify.onError({
message: 'Error: <%= error.message %>',
title: 'Failed running browserify',
})
)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(
sourcemaps.init({
loadMaps: true,
})
)
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('public/'))
.pipe(browserSync.stream())
.pipe(
notify({
message: 'Browserify task ended',
})
);
}
/**
* Browser sync live server
*/
function reload(done) {
server.reload();
done();
}
function serve(done) {
server.init({
server: {
baseDir: './',
},
browser: 'google-chrome',
});
done();
}
gulp.watch('index.html', reload);
gulp.watch('./src/js/**/*.js', gulp.series(scripts, reload));
gulp.watch('./src/styles/**/*.scss', gulp.series(styles, reload));
gulp.task('default', serve);