-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
116 lines (100 loc) · 3.22 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
'use strict';
import browserSync from 'browser-sync';
import del from 'del';
import path from 'path';
import gulp from 'gulp';
import named from 'vinyl-named';
import yargs from 'yargs';
var sass = require('gulp-dart-sass');
const sourceMaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const uglify = require('gulp-uglify');
const cleanCss = require('gulp-clean-css');
const gulpif = require('gulp-if');
const imagemin = require('gulp-imagemin');
const PRODUCTION = !!yargs.argv.p;
const _STATIC = path.resolve('static');
const _NODE_MODULES = path.resolve('node_modules');
// paths
const PATHS = {
src: {
base: path.join(_STATIC, 'src'),
images: path.join(_STATIC, 'src', 'img'),
scripts: path.join(_STATIC, 'src', 'js'),
styles: path.join(_STATIC, 'src', 'scss'),
stylesCss: path.join(_STATIC, 'src', 'css'),
},
dest: {
base: path.join(_STATIC, 'dist'),
images: path.join(_STATIC, 'dist', 'img'),
scripts: path.join(_STATIC, 'dist', 'js'),
styles: path.join(_STATIC, 'dist', 'css'),
},
};
function browserSyncServer(c) {
browserSync.init({ proxy: 'localhost:8000' });
c();
}
function cleanDist() {
return del(path.join(PATHS.dest.base, '**'));
}
function sassCompile() {
return gulp
.src(path.join(PATHS.src.styles, '**', '*'))
.pipe(gulpif(!PRODUCTION, sourceMaps.init()))
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulpif(PRODUCTION, cleanCss({ compatibility: 'ie9' })))
.pipe(gulpif(!PRODUCTION, sourceMaps.write()))
.pipe(gulp.dest(PATHS.dest.styles))
.pipe(browserSync.stream());
}
function cssCompile(){
return gulp
.src(path.join(PATHS.src.stylesCss, '**', '*'))
.pipe(gulpif(!PRODUCTION, sourceMaps.init()))
.pipe(autoprefixer())
.pipe(gulpif(PRODUCTION, cleanCss({ compatibility: 'ie9' })))
.pipe(gulpif(!PRODUCTION, sourceMaps.write()))
.pipe(gulp.dest(PATHS.dest.styles))
.pipe(browserSync.stream());
}
function images() {
return gulp
.src(path.join(PATHS.src.images, '**', '*'))
.pipe(imagemin())
.pipe(gulp.dest(PATHS.dest.images));
}
function scripts() {
return (
gulp
.src(path.join(PATHS.src.scripts, '**', '*.js'))
.pipe(named())
.pipe(gulpif(!PRODUCTION, sourceMaps.init()))
.pipe(gulpif(PRODUCTION, uglify()))
.pipe(gulpif(!PRODUCTION, sourceMaps.write()))
.pipe(gulp.dest(PATHS.dest.scripts))
);
}
function others(c) {
// console.log('others');
c();
}
function watchChanges() {
gulp
.watch(path.join(path.resolve('templates'), '**', '*'))
.on('all', browserSync.reload);
gulp
.watch(path.join(PATHS.src.images, '**', '*'))
.on('all', gulp.series(images, browserSync.reload));
gulp
.watch(path.join(PATHS.src.scripts, '**', '*'))
.on('all', gulp.series(scripts, browserSync.reload));
gulp.watch(path.join(PATHS.src.styles, '**', '*')).on('all', gulp.series(sassCompile,browserSync.reload));
gulp.watch(path.join(PATHS.src.stylesCss, '**', '*')).on('all', gulp.series(cssCompile,browserSync.reload));
}
gulp.task(
'build',
gulp.series(cleanDist, gulp.parallel(scripts, sassCompile, others,cssCompile ,images))
);
gulp.task('watch', gulp.series('build', browserSyncServer, watchChanges));