-
Notifications
You must be signed in to change notification settings - Fork 38
/
gulpfile.js
138 lines (118 loc) · 3.48 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
// Load Gulp...of course
const { src, dest, task, watch, series, parallel } = require('gulp');
// CSS related plugins
var sass = require( 'gulp-sass' );
var autoprefixer = require( 'gulp-autoprefixer' );
// JS related plugins
var uglify = require( 'gulp-uglify' );
var babelify = require( 'babelify' );
var browserify = require( 'browserify' );
var source = require( 'vinyl-source-stream' );
var buffer = require( 'vinyl-buffer' );
var stripDebug = require( 'gulp-strip-debug' );
// Utility plugins
var rename = require( 'gulp-rename' );
var sourcemaps = require( 'gulp-sourcemaps' );
var notify = require( 'gulp-notify' );
var plumber = require( 'gulp-plumber' );
var options = require( 'gulp-options' );
var gulpif = require( 'gulp-if' );
// Browers related plugins
var browserSync = require( 'browser-sync' ).create();
// Project related variables
var styleSRC = './src/scss/style.scss';
var styleURL = './dist/css/';
var mapURL = './';
var jsSRC = './src/js/';
var jsFront = 'main.js';
var jsFiles = [ jsFront ];
var jsURL = './dist/js/';
var imgSRC = './src/images/**/*';
var imgURL = './dist/images/';
var fontsSRC = './src/fonts/**/*';
var fontsURL = './dist/fonts/';
var htmlSRC = './src/**/*.html';
var htmlURL = './dist/';
var styleWatch = './src/scss/**/*.scss';
var jsWatch = './src/js/**/*.js';
var imgWatch = './src/images/**/*.*';
var fontsWatch = './src/fonts/**/*.*';
var htmlWatch = './src/**/*.html';
// Tasks
function browser_sync() {
browserSync.init({
server: {
baseDir: './dist/'
}
});
}
function reload(done) {
browserSync.reload();
done();
}
function css(done) {
src( [ styleSRC ] )
.pipe( sourcemaps.init() )
.pipe( sass({
errLogToConsole: true,
outputStyle: 'compressed'
}) )
.on( 'error', console.error.bind( console ) )
.pipe( autoprefixer({ browsers: [ 'last 2 versions', '> 5%', 'Firefox ESR' ] }) )
.pipe( rename( { suffix: '.min' } ) )
.pipe( sourcemaps.write( mapURL ) )
.pipe( dest( styleURL ) )
.pipe( browserSync.stream() );
done();
};
function js(done) {
jsFiles.map( function( entry ) {
return browserify({
entries: [jsSRC + entry]
})
.transform( babelify, { presets: [ '@babel/preset-env' ] } )
.bundle()
.pipe( source( entry ) )
.pipe( rename( {
extname: '.min.js'
} ) )
.pipe( buffer() )
.pipe( gulpif( options.has( 'production' ), stripDebug() ) )
.pipe( sourcemaps.init({ loadMaps: true }) )
.pipe( uglify() )
.pipe( sourcemaps.write( '.' ) )
.pipe( dest( jsURL ) )
.pipe( browserSync.stream() );
});
done();
};
function triggerPlumber( src_file, dest_file ) {
return src( src_file )
.pipe( plumber() )
.pipe( dest( dest_file ) );
}
function images() {
return triggerPlumber( imgSRC, imgURL );
};
function fonts() {
return triggerPlumber( fontsSRC, fontsURL );
};
function html() {
return triggerPlumber( htmlSRC, htmlURL );
};
function watch_files() {
watch(styleWatch, series(css, reload));
watch(jsWatch, series(js, reload));
watch(imgWatch, series(images, reload));
watch(fontsWatch, series(fonts, reload));
watch(htmlWatch, series(html, reload));
src(jsURL + 'main.min.js')
.pipe( notify({ message: 'Gulp is Watching, Happy Coding!' }) );
}
task("css", css);
task("js", js);
task("images", images);
task("fonts", fonts);
task("html", html);
task("default", parallel(css, js, images, fonts, html));
task("watch", parallel(browser_sync, watch_files));