-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
136 lines (106 loc) · 3.08 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
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const autoprefixer = require('autoprefixer');
const gulpSequence = require('gulp-sequence');
const browserSync = require('browser-sync').create();
const parseArgs = require('minimist');
const envOptions = {
string: 'env',
default: {
env: 'dev',
},
}
const options = parseArgs(process.argv.slice(2), envOptions);
gulp.task('clean', () => {
return gulp.src('./dist/', {
read: false
})
.pipe($.clean());
});
gulp.task('pug', () => {
return gulp.src('./src/**/*.pug')
.pipe($.plumber())
.pipe($.pug({
// pretty: true
}))
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.stream());
});
gulp.task('sass', () => {
const plugins = [
autoprefixer({
// https://github.com/browserslist/browserslist#best-practices
// browsers: ['> 1%', 'last 2 versions', 'not ie <= 8']
browsers: ['last 1 version', 'not dead', '> 0.2%']
})
];
return gulp.src('./src/scss/**/*.scss')
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.sass().on('error', $.sass.logError))
// CSS 編譯完成
.pipe($.postcss(plugins))
.pipe($.if(options.env === 'prod', $.cleanCss()))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./dist/css/'))
.pipe(browserSync.stream());
});
gulp.task('babel', () => {
return gulp.src('./src/js/**/*.js')
.pipe($.plumber())
.pipe($.sourcemaps.init())
.pipe($.babel({
presets: ['@babel/env']
}))
.pipe($.if(options.env === 'prod', $.uglify({
compress: {
drop_console: true,
},
})))
.pipe($.concat('index.js'))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./dist/js/'))
.pipe(browserSync.stream());
});
// https://www.npmjs.com/package/gulp-imagemin
// https://www.npmjs.com/package/gulp-svgmin
gulp.task('img-compress', () => {
return gulp.src('./src/assets/**/*.svg')
.pipe($.plumber())
// .pipe($.if(options.env === 'prod', $.imagemin()))
.pipe($.if(options.env === 'prod', $.svgmin()))
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.stream());
});
gulp.task('img', () => {
return gulp.src('./src/assets/**/*.{png,jpg,ico}')
.pipe($.plumber())
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.stream());
});
gulp.task('audio', () => {
return gulp.src('./src/assets/**/*.mp3')
.pipe($.plumber())
.pipe(gulp.dest('./dist/'))
.pipe(browserSync.stream());
});
gulp.task('browser-sync', () => {
browserSync.init({
server: {
baseDir: './dist',
},
// reloadDebounce: 800,
});
});
// 監聽檔案變動
gulp.task('watch', () => {
gulp.watch('./src/**/*.pug', ['pug']);
gulp.watch('./src/scss/**/*.scss', ['sass']);
gulp.watch('./src/js/**/*.js', ['babel']);
gulp.watch('./src/assets/**/*.svg', ['img-compress']);
gulp.watch('./src/assets/**/*.mp3', ['audio']);
});
// 開發流程
gulp.task('default', ['pug', 'sass', 'babel', 'img', 'img-compress', 'audio', 'browser-sync', 'watch']);
// 發布流程
gulp.task('build', gulpSequence('clean', 'pug', 'sass', 'babel', 'img', 'img-compress', 'audio'));