-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
122 lines (111 loc) · 2.62 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
const { task, src, dest, watch, series, parallel } = require('gulp');
const babel = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
const imagemin = require('gulp-imagemin');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const size = require('gulp-size');
const sourcemaps = require('gulp-sourcemaps');
const svgSprite = require('gulp-svg-sprite');
/**
* Package JavaScript with Babel and Browserify
*/
task('js', () => {
return browserify(['./src/assets/scripts/main.js'], {
debug: true,
})
.transform(babel)
.bundle()
.on('error', function (err) {
console.error(err);
this.emit('end');
})
.pipe(source('main.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./dist/assets/scripts'));
});
/**
* Create CSS and Sourcemaps with PostCSS
*/
task('css', function () {
return src(['./src/assets/styles/*.css', './src/assets/styles/critical/*.css'])
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(postcss())
.pipe(sourcemaps.write('.'))
.pipe(dest('./dist/assets/styles'));
});
/**
* Create CSS with PostCSS for production
*/
task('css:production', function () {
return src(['./src/assets/styles/*.css'])
.pipe(plumber())
.pipe(postcss())
.pipe(dest('./dist/assets/styles'));
});
/**
* Copy critical CSS files to project folder
*/
task('css:critical', function () {
return src(['./src/assets/styles/critical/*.css'])
.pipe(plumber())
.pipe(postcss())
.pipe(dest('./src/includes/critical'));
});
/**
* Create SVG sprite map from SVG files
*/
task('svg', () => {
return src('./src/icons/*.svg')
.pipe(plumber())
.pipe(
svgSprite({
mode: {
symbol: {
dest: '.',
sprite: 'icons.svg',
},
svg: {
xmlDeclaration: false,
doctypeDeclaration: false,
},
},
})
)
.pipe(dest('src/includes'));
});
/**
* Optimize and minimize images
*/
task('optimize:images', () => {
return src('src/assets/images/**/*.{jpg,jpeg,png,gif,svg}')
.pipe(
imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true,
})
)
.pipe(dest('src/assets/images/'))
.pipe(size());
});
/**
* Run build for production
*/
task('build', parallel('js', 'css:production', 'css:critical'));
/**
* Watch for changes in files
*/
task('watch', () => {
watch('./src/assets/scripts/**/*.js', series('js'));
watch('./src/assets/styles/**/*.css', series('css'));
});
/**
* Compile assets and start watching for changes
*/
task('default', parallel('js', 'css', 'watch'));