-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
170 lines (163 loc) · 5.11 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
165
166
167
168
169
170
const gulp = require('gulp');
const less = require('gulp-less')
const cleanCss = require('gulp-clean-css');
const uglify = require("gulp-uglify");
const concat = require("gulp-concat");
const babel = require('gulp-babel');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const rev = require('./lib/gulp-rev/index'); //生成hash文件 自己更改了源码
const revReplace = require("gulp-rev-replace");
const gulpSequence = require('gulp-sequence')
const clean = require('gulp-clean');
gulp.task('less', function () {
return gulp.src([
'src/css/**.less',
'src/css/**/**.less',
'!src/css/common/**.less'
])
.pipe(less())
.pipe(cleanCss())
.pipe(gulp.dest('dist/css'));
});
gulp.task("uglify", function() {
return gulp.src([
'src/js/**.js',
'src/js/**/**.js',
'!src/js/common/**.js'
])
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(uglify())
.pipe(gulp.dest('dist/js/'));
})
gulp.task('concatJs', function() {
return gulp.src([
'src/js/common/rem.js',
'src/js/common/**.js'
])
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(concat('common.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
gulp.task("cleanhtml", function() {
return gulp.src("dist/views/**/**")
.pipe(clean())
});
gulp.task('imgmin', function() {
// gulp.src(['gulpDemo(Test)/img/*.jpg', 'gulpDemo(Test)/img/*.png'])
return gulp.src("src/content/images/*")
.pipe(imagemin())
.pipe(gulp.dest('dist/content/images'))
});
gulp.task('move', function() {
gulp.src([
"src/content/**",
"!src/content/images/*"
])
.pipe(imagemin())
.pipe(gulp.dest('dist/content'))
gulp.src([
"src/lib/**"
])
.pipe(imagemin())
.pipe(gulp.dest('dist/lib'))
});
gulp.task('rev',['less', 'uglify', 'concatJs','clean-md5'],function() {
return gulp.src([
'dist/css/**/**',
'dist/js/**/**',
])
.pipe(rev())
// .pipe(gulp.dest('dist/rev'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist/rev'))
});
gulp.task("clean-md5", function() {
return gulp.src([
'dist/rev/*.json'
])
.pipe(clean())
})
gulp.task("htmlmin-watch", function(){
const bundle = gulp.src("src/views/**/**")
.pipe(htmlmin(
{
collapseWhitespace: true, //去掉空格
removeComments:true, //删除html里的注释
minifyCSS:true, //压缩html里的style里的css样式
minifyJS:true, //压缩html里的script里的js代码
}
))
.pipe(gulp.dest('dist/views'));
return bundle;
});
gulp.task("htmlmin",gulpSequence('rev', function(){
const manifest = gulp.src([
'dist/rev/rev-manifest.json',
]);
const bundle = gulp.src("src/views/**/**")
.pipe(htmlmin(
{
collapseWhitespace: true, //去掉空格
removeComments:true, //删除html里的注释
minifyCSS:true, //压缩html里的style里的css样式
minifyJS:true, //压缩html里的script里的js代码
}
))
.pipe(revReplace({manifest: manifest}))
.pipe(gulp.dest('dist/views'));
return bundle;
}));
gulp.task('watch', function () {
var cssWatcher = gulp.watch([
'src/css/**.less',
'src/css/**/**.less',
'!src/css/common/**.less'
] , ['less']);
cssWatcher.on('change', function (event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
var jsConcatWatcher = gulp.watch([
'src/js/common/**.js'], ['concatJs']);
jsConcatWatcher.on('change', function (event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
var jsWatcher = gulp.watch([
'src/js/**.js',
'src/js/**/**.js',
'!src/js/common/**.js'], ['uglify']);
jsWatcher.on('change', function (event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
var htmlWatcher = gulp.watch([
'src/views/**.html',
'src/views/**/**.html'
] , ['htmlmin-watch']);
htmlWatcher.on('change', function (event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
//因为图片太多,导致gulp watch卡顿,所以,注释掉
// var imgWatcher = gulp.watch([
// 'src/content/images/*'
// ] , ['imgmin']);
// imgWatcher.on('change', function (event) {
// console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
// });
//因为文件太多,导致gulp watch卡顿,所以,注释掉
// var contentWatcher = gulp.watch([
// "src/content/**",
// "!src/content/images/*",
// "src/lib/**"
// ], ['move']);
//contentWatcher.on('change', function (event) {
// console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
//});
});
gulp.task("default",['htmlmin']);
gulp.task("lib",['move','imgmin']);
gulp.task('build',['htmlmin','move','imgmin']);