-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
111 lines (109 loc) · 2.94 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
var gulp = require('gulp'),
fileInclude = require('gulp-file-include'),
sass = require('gulp-sass'),
plumber = require('gulp-plumber'),
autoprefixer = require('gulp-autoprefixer'),
minify = require('gulp-minify'),
cleanCSS = require('gulp-clean-css'),
sourcemaps = require('gulp-sourcemaps'),
babel = require('gulp-babel'),
ts = require('gulp-typescript'),
revAll = require('gulp-rev-all'),
clean = require('gulp-clean')
// 复制无需打包的库文件到dist
const copyUrl = [ 'src/assets/js/lib/**/*' ]
gulp.task('copy', () =>
gulp.src(copyUrl).pipe(gulp.dest('dist/assets/js/lib'))
)
// html文件合成
gulp.task('generateHtml', function () {
return gulp.src('src/pages/**/*.html')
.pipe(fileInclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(minify({
comments: true
}))
.pipe(gulp.dest('dist'))
})
//scss编译
gulp.task('generateScss', function () {
return gulp.src([ 'src/assets/css/style.scss' ])
.pipe(sass())
.pipe(plumber())
.pipe(autoprefixer({
overrideBrowserslist: [ 'last 2 versions' ],
cascade: false
}))
.pipe(sourcemaps.init())
.pipe(cleanCSS())
.pipe(gulp.dest('dist/assets/css')),
// 插件css
gulp.src([ 'src/assets/js/plugins/**/*.scss' ])
.pipe(sass())
.pipe(plumber())
.pipe(autoprefixer({
overrideBrowserslist: [ 'last 2 versions' ],
cascade: false
}))
.pipe(sourcemaps.init())
.pipe(cleanCSS())
.pipe(gulp.dest('dist/assets/js/plugins'))
})
// 替换md5后缀
gulp.task('rev', () => {
return gulp.src([ 'dist/**' ])
.pipe(revAll.revision({
dontRenameFile: [ 'html', '/lib' ]
// debug: true
}))
.pipe(gulp.dest('./dist/'))
})
const babelSrc = [ 'src/**/*.es6' ]
// babel
gulp.task('generateBabel', () =>
gulp.src(babelSrc)
.pipe(sourcemaps.init())
.pipe(babel({
presets: [ '@babel/env' ]
}))
.pipe(gulp.dest('dist'))
)
// ts编译
const tsUrl = [ 'src/assets/js/**/*.ts' ]
gulp.task('generateTs', () => gulp.src(tsUrl)
.pipe(ts({declaration: true}))
.pipe(gulp.dest('dist/assets/js'))
)
// 清空生成目录
gulp.task('clean', () =>
gulp.src('dist', {read: false, allowEmpty: true})
.pipe(clean({force: true}))
)
// 默认任务
gulp.task('default',
gulp.series(
'clean',
gulp.parallel(
'generateScss',
'generateTs',
'generateBabel',
'generateHtml'
),
'rev',
'copy'
)
)
//监听
gulp.task('watch', function () {
gulp.watch([ 'src/**/*' ], gulp.parallel('default'))
// 监听src内所有html
// gulp.watch('src/**/*.html', 'html')
// // 监听src内所有css
// gulp.watch('src/**/*.scss', 'scss')
// // 监听src内所有ts
// gulp.watch('src/**/*.ts', 'ts')
// // 监听src内所有es6
// gulp.watch('src/**/*.es6', 'babel')
})