-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
executable file
·171 lines (151 loc) · 4.29 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
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
"use strict";
import gulp from 'gulp';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import autoprefixer from 'gulp-autoprefixer';
import concat from 'gulp-concat';
import svgSprite from 'gulp-svg-sprite';
import plumber from 'gulp-plumber';
import notify from 'gulp-notify';
import minify from 'gulp-minify';
import babel from 'gulp-babel';
import cache from 'gulp-cached';
import tingpng from 'gulp-tinypng';
import browserSync from 'browser-sync';
// Tinypng Api Key - 500 images limit per month - Get key here: https://tinypng.com/developers
const TINYPNG_API = 'e9aSCEpvwKzzSFKRgEA9FxW11TUJJF2z';
// Distribution Locations
let dist = {
css: 'css',
img: 'img',
js: 'js',
maps: 'maps',
jslib: 'js/lib',
fonts: 'fonts'
};
// Source Locations
let source = 'src/';
let bowerSrc = 'bower_components/';
let src = {
sprite: source + 'img/svg/*.svg',
styles: {
location: source + 'scss/**/*.scss',
autoprefixer: ['last 2 versions', 'safari 6', 'ie 9', 'ios 7', 'android 4']
},
scripts: source + 'js/scripts.js',
plugins: [
bowerSrc + 'jquery/dist/jquery.js',
bowerSrc + 'slick-carousel/slick/slick.js',
],
images: source + 'img/**/*.{png,jpg,jpeg}',
copyTask: {
jslib: source + 'js/lib/**/*',
fonts: source + 'fonts/**/*'
},
serve: ['css/*.css', 'js/*.js', '*.html']
}
// Handle the error
let onError = (err) => {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
};
// SVG Sprites
gulp.task('sprite', () => {
return gulp.src(src.sprite)
.pipe(plumber({errorHandler: onError}))
.pipe(svgSprite({
mode: {
symbol: {
dest: '',
prefix: '',
sprite: 'spritemap'
}
}
}))
.pipe(gulp.dest(dist.img))
.pipe(notify({ message: 'SVG task complete' }));
});
// CSS
gulp.task('styles', () => {
return gulp.src(src.styles.location)
.pipe(plumber({errorHandler: onError}))
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: src.styles.autoprefixer
}))
.pipe(sourcemaps.write(dist.maps))
.pipe(gulp.dest(dist.css))
.pipe(notify({ message: 'Styles task complete' }));
});
// JS - custom scripts
gulp.task('scripts', () => {
return gulp.src(src.scripts)
.pipe(plumber({errorHandler: onError}))
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(concat('scripts.js'))
.pipe(minify())
.pipe(sourcemaps.write(dist.maps))
.pipe(gulp.dest(dist.js))
.pipe(notify({ message: 'Scripts task complete' }));
});
// JS - plugins
gulp.task('scripts-plugins', () => {
return gulp.src(src.plugins)
.pipe(plumber({errorHandler: onError}))
.pipe(sourcemaps.init())
.pipe(concat('plugins.js'))
.pipe(minify())
.pipe(sourcemaps.write(dist.maps))
.pipe(gulp.dest(dist.js))
.pipe(notify({ message: 'Plugins task complete' }));
});
// Images
gulp.task('images', () => {
return gulp.src(src.images)
.pipe(plumber({errorHandler: onError}))
.pipe(cache('tinypnging'))
.pipe(tingpng(TINYPNG_API))
.pipe(gulp.dest(dist.img))
.pipe(notify({ message: 'Image Task Completed: <%= file.relative %>' }));
});
// Copy files to dist
gulp.task('copyTask', () => {
// JS library
gulp.src(src.copyTask.jslib)
.pipe(gulp.dest(dist.jslib));
// Fonts
gulp.src(src.copyTask.fonts)
.pipe(gulp.dest(dist.fonts));
});
// Browsersync task - Static server
gulp.task('serve', () => {
browserSync.init(src.serve, {
server: {
baseDir: "./"
}
});
});
// Default Tasks
gulp.task('default', ['copyTask', 'styles', 'scripts', 'sprite', 'scripts-plugins', 'images']);
// Watch tasks - gulp watch
gulp.task('watch', ['serve'], () => {
// SVG files for spritemap
gulp.watch(src.sprite, ['sprite']);
// Watch .scss files
gulp.watch(src.styles.location, ['styles']);
// Watch scripts.js files
gulp.watch(src.scripts, ['scripts']);
// Watch plugin .js files
gulp.watch(src.plugins, ['scripts-plugins']);
// Watch image files
gulp.watch(src.images, ['images']);
// Watch for .js library files
gulp.watch('{src.copyTask.jslib, src.copyTask.fonts}', ['copyTask']);
});