-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
160 lines (147 loc) · 3.73 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
'use strict';
// Import
import gulp from 'gulp'
import del from 'del'
import Bs from 'browser-sync'
import Glp from 'gulp-load-plugins'
// Config
const browserSync = Bs.create()
const reload = browserSync.reload
const Pi = Glp({
rename: {
// 'gulp-html-replace': 'htmlReplace',
'gulp-clean-css': 'cleanCss',
}
})
const Paths = {
dest: 'dist',
jsTest: ['src/**/*.js', 'gulpfile.babel.js'],
views: {
src: 'src/views/**/*.html',
dest: 'dist/views/',
},
styles: {
css: 'src/styles/*.css',
less: 'src/styles/*.less',
sass: 'src/styles/*.scss',
dest: 'dist/styles/',
},
scripts: {
src: 'src/scripts/**/*.js',
dest: 'dist/scripts/',
},
images: {
src: 'src/images/**/*.{jpg,jpeg,png,svg}',
dest: 'dist/images/',
},
}
// Fn
const clean = () => del(Paths.dest).then(files => console.log(`Delete ${files}`))
export { clean }
// Views
export function views() {
return gulp.src(Paths.views.src)
// .pipe(Pi.htmlReplace({
// 'css': '../../styles/all.min.css',
// 'js': '../../scripts/all.min.js',
// }))
.pipe(gulp.dest(Paths.views.dest))
}
// Less
export function less() {
return gulp.src(Paths.styles.less)
.pipe(Pi.changed(Paths.dest))
.pipe(Pi.less())
.pipe(Pi.autoprefixer({
browsers: ['iOS >= 7', 'Android >= 4.1'],
cascade: false,
}))
.pipe(gulp.dest(Paths.styles.dest))
.pipe(Pi.cleanCss())
// .pipe(Pi.concat('all.min.css'))
.pipe(Pi.rename(path => path.basename += '.min'))
.pipe(gulp.dest(Paths.styles.dest))
.pipe(reload({ stream: true }))
}
// Sass
export function sass() {
return gulp.src(Paths.styles.sass)
.pipe(Pi.changed(Paths.dest))
.pipe(Pi.sass({
outputStyle: 'expanded',
// onerror?
}).on('error', Pi.sass.logError))
.pipe(Pi.autoprefixer({
browsers: ['last 2 versions'],
cascade: false,
}))
.pipe(gulp.dest(Paths.styles.dest))
.pipe(Pi.cleanCss())
// .pipe(Pi.concat('all.min.css'))
.pipe(Pi.rename(path => path.basename += '.min'))
.pipe(gulp.dest(Paths.styles.dest))
.pipe(reload({ stream: true }))
}
// Js
export function scripts() {
return gulp.src(Paths.scripts.src, { sourcemaps: true })
.pipe(Pi.plumber())
.pipe(Pi.sourcemaps.init())
.pipe(Pi.babel())
// .pipe(gulp.dest(Paths.scripts.dest))
// .pipe(Pi.uglify())
// .pipe(Pi.rename(path => path.basename += '.min'))
// .pipe(Pi.concat('all.min.js'))
.pipe(Pi.sourcemaps.write())
.pipe(gulp.dest(Paths.scripts.dest))
}
// Eslint
export function eslint() {
return gulp.src(Paths.jsTest)
.pipe(Pi.changed(Paths.dest))
.pipe(Pi.eslint({
envs: ['browser', 'ES6', 'node'],
// rules: {
// curly: [2, 'multi-line'],
// indent: [2,2],
// },
}))
.pipe(Pi.eslint.format())
}
// Img
export function images() {
return gulp.src(Paths.images.src, { since: gulp.lastRun('images') })
.pipe(Pi.imagemin({ optimizationLevel: 5 }))
.pipe(gulp.dest(Paths.images.dest));
}
// Server
export function server(next) {
browserSync.init({
server: {
baseDir: './'
}
// proxy: '代理域名/IP', // 二选一
})
next();
}
//
export function bsReload(next) {
reload();
next();
}
// Watch
export function watch() {
gulp.watch(Paths.views.src, gulp.series(views, bsReload))
gulp.watch(Paths.styles.css, sass)
gulp.watch(Paths.styles.less, less)
gulp.watch(Paths.styles.sass, sass)
gulp.watch(Paths.scripts.src, gulp.series(scripts, bsReload))
gulp.watch(Paths.images.src, gulp.series(images, bsReload))
}
// Tasks
const build = gulp.series(clean, gulp.parallel(views, sass, scripts, images))
export { build }
const dev = gulp.parallel(build, server, watch)
export { dev }
// Default
export default dev