-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
164 lines (149 loc) · 4.47 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
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
const gulp = require('gulp');
const less = require('gulp-less');
const postcss = require('gulp-postcss');
const postcssPresetEnv = require('postcss-preset-env');
const precss = require('precss');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const terser = require('gulp-terser');
const rename = require('gulp-rename');
const del = require('del');
const browserSync = require('browser-sync').create();
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const replace = require('gulp-replace');
const imagemin = require('gulp-imagemin');
const plumber = require('gulp-plumber');
const paths = {
html: {
src: './src/**/*.html',
dest: './build'
},
styles: {
src: './src/css/**/*.less',
dest: './build/assets/css'
},
scripts: {
src: './src/js/**/*.js',
dest: './build/assets/js'
},
vendors: {
src: './src/js/vendors/**/*.js',
dest: './build/assets/js'
},
images: {
src: './src/images/**/*',
dest: './build/assets/images'
},
favicon: {
src: './src/favicon.ico',
dest: './build'
}
};
const clean = () => del(['./build']);
// Cache busting
const curTime = new Date().getTime();
const cacheBust = () =>
gulp
.src(paths.html.src)
.pipe(plumber())
.pipe(replace(/cb=\d+/g, 'cb=' + curTime))
.pipe(gulp.dest(paths.html.dest));
// Copies all html
const html =() =>
gulp
.src(paths.html.src)
.pipe(plumber())
.pipe(gulp.dest(paths.html.dest));
// Convert css, auto-prefix and rename into styles.min.css
const styles = () =>
gulp
.src(paths.styles.src)
.pipe(plumber())
.pipe(sourcemaps.init())
// .pipe(postcss([ require('postcssPresetEnv'), require('autoprefixer'), require('cssnano'), require('precss')]))
.pipe(less())
.pipe(postcss([ postcssPresetEnv(), autoprefixer(), cssnano(), precss()]))
.pipe(
rename({
basename: 'styles',
suffix: '.min'
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.styles.dest))
.pipe(browserSync.stream());
// Minify all javascript files and concat them into a single app.min.js
const scripts = () =>
gulp
.src(paths.scripts.src)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ['@babel/preset-env']
})
)
.pipe(terser())
.pipe(concat('app.min.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.scripts.dest));
// Minify all javascript vendors/libs and concat them into a single vendors.min.js
const vendors = () =>
gulp
.src(paths.vendors.src)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ['@babel/preset-env']
})
)
.pipe(terser())
.pipe(concat('vendors.min.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.vendors.dest));
// Copy and minify images
const images = () =>
gulp
.src(paths.images.src)
.pipe(plumber())
.pipe(imagemin())
.pipe(gulp.dest(paths.images.dest));
// Copy the favicon
const favicon = () =>
gulp
.src(paths.favicon.src)
.pipe(plumber())
.pipe(gulp.dest(paths.favicon.dest));
// Watches all .scss, .js and .html changes and executes the corresponding task
function watchFiles() {
browserSync.init({
server: {
baseDir: './build'
},
notify: false
});
gulp.watch(paths.styles.src, styles);
gulp.watch(paths.vendors.src, vendors).on('change', browserSync.reload);
gulp.watch(paths.favicon.src, favicon).on('change', browserSync.reload);
gulp.watch(paths.scripts.src, scripts).on('change', browserSync.reload);
gulp.watch(paths.images.src, images).on('change', browserSync.reload);
gulp.watch('./src/*.html', html).on('change', browserSync.reload);
}
const build = gulp.series(
clean,
gulp.parallel(styles, vendors, scripts, images, favicon),
cacheBust
);
const watch = gulp.series(build, watchFiles);
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.vendors = vendors;
exports.images = images;
exports.favicon = favicon;
exports.watch = watch;
exports.build = build;
exports.default = build;