-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile2.js
295 lines (246 loc) · 8.5 KB
/
gulpfile2.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* 运行有三种方法:
* 1、gulp.task('default',[deps])
* 2、gulp.run([''])或者gulp.start([''])
* 3、引入runSequence,进行异步控制
* @type {Gulp|*|exports|module.exports}
*/
var gulp = require('gulp');
var jshint = require('gulp-jshint');//代码检查
var useref = require('gulp-useref');//!!!没懂
var concat = require('gulp-concat');//文件合并
var rename = require('gulp-rename');//重命名
var uglify = require('gulp-uglify');//js代码压缩
var imagemin = require('gulp-imagemin');//图片压缩
var img64 = require('gulp-img64');//图片使用dataUri
var gulpif = require('gulp-if');
var minifyCss = require('gulp-minify-css');//https://github.com/murphydanger/gulp-minify-css或者csso
var csso = require('gulp-csso');//同样是压缩css
var autoprefixer = require('gulp-autoprefixer');
var changed = require('gulp-changed');//只编译stream里的改动的文件
//html压缩用不多
var htmlmin = require('gulp-htmlmin');//https://github.com/jonschlinkert/gulp-htmlmin或者gulp-minify-html
var minifyHTML = require('gulp-minify-html');
var htmlreplace = require('gulp-html-replace');
//版本号控制和重引用很常用
var rev = require('gulp-rev');//re-version重新命名版本
var revReplace = require('gulp-rev-replace');//进行重新命名版本之后的assert的替换.或者gulp-rev-collector
var revCollector = require('gulp-rev-collector');
var filter = require('gulp-filter');//!!!与src的exculde类似
var cache = require('gulp-cache');//缓存
var clean = require('gulp-clean');//删除文件或者文件夹
var sourcemaps = require('gulp-sourcemaps');
var notify = require('gulp-notify');//通知
var watch = require('gulp-watch');
var runSequence = require('run-sequence');
var runGulp = require('run-sequence').use(gulp);
var replace = require('gulp-replace');//替换文件中的字符串
var flatten = require('gulp-flatten');//提取文件到目录
var del = require('del');//删除glob
var
useref = require('gulp-useref'),
lazypipe = require('lazypipe');
var plumber = require('gulp-plumber');
gulp.task('delete', function () {
del('del/**/*.js');
});
gulp.task('testchanged', function () {
gulp.src('testchanged/*.js')
.pipe(changed('aaaa'))
.pipe(gulp.dest('aaaa'));
})
gulp.task('assert', function () {
var jsFilter = filter('**/*.js', {restore: true});
var cssFilter = filter('**/*.js', {restore: true});
var imgFilter = filter('**/*.+(png|jpg)', {restore: true});
gulp.src('common/**/*.*')
.pipe(jsFilter)
.pipe(uglify())
.pipe(jsFilter.restore)
.pipe(cssFilter)
.pipe(csso())
.pipe(cssFilter.restore)
.pipe(imgFilter)
.pipe(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
}))
.pipe(imgFilter.restore)
.pipe(gulp.dest('common/dist'))
})
gulp.task('clean', function () {
return gulp.src('aaaa', {read: false})
.pipe(clean());
});
gulp.task('add', ['clean'], function () {
gulp.src('js/**/*.js')
.pipe(gulp.dest('aaaa'));
});
gulp.task('img', function () {
var imgSrc = "image/*.jpg", imgDst = "dist/image";
var imgRevPath = "rev/image";
return gulp.src(imgSrc)
.pipe(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
}))
.pipe(rev())
.pipe(gulp.dest(imgDst))
.pipe(rev.manifest())
.pipe(gulp.dest('rev/image'));
});
gulp.task('testuse', function () {
var jsFilter = filter('**/*.js', {restore: true});
var cssFilter = filter('**/*.css', {restore: true});
return gulp.src('testuse.html')
.pipe(useref())
.pipe(jsFilter)
.pipe(uglify())
.pipe(rev())
.pipe(jsFilter.restore)
.pipe(cssFilter)
.pipe(minifyCss())
.pipe(rev())
.pipe(cssFilter.restore)
.pipe(revReplace())
.pipe(gulp.dest('testuse'));
})
//filter是把排除的加进来
gulp.task('css', function () {
var cssSrc = "css/**/*.css", cssDst = "dist/css";
var cssRevPath = "rev/css";
var mainfilter = filter(['main.css', 'normalize.css'], {restore: true});
var hallfilter = filter(['hall_fiml.css', 'a4.css'], {restore: true});
return gulp.src(cssSrc)
.pipe(mainfilter)
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(csso())
.pipe(concat('all.css'))
.pipe(rev())
.pipe(gulp.dest('dist/css'))
.pipe(mainfilter.restore)
.pipe(hallfilter)
.pipe(csso())
.pipe(rev())
.pipe(gulp.dest('dist/css2'))
.pipe(hallfilter.restore)
.pipe(gulp.dest('dist/allCss'));
//.pipe(rev.manifest())
//.pipe(gulp.dest('rev/css'))
});
//只是路径的替换,revision的版本能不能rename,适用于revCollector
gulp.task('js', function () {
return gulp.src(['js/**/*.js'])
.pipe(plumber())
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest("dist/js"))
.pipe(rev.manifest())
.pipe(plumber.stop())
.pipe(gulp.dest('rev/js'))
});
//!!!适用于revReplace,更优
//gulp.task('js', function () {
// return gulp.src(['js/**/*.js'])
// .pipe(uglify())
// .pipe(rev())
// .pipe(rename(function (path) {
// path.basename += ".min";
// }))
// .pipe(gulp.dest("dist/js"))
// .pipe(rev.manifest())
// .pipe(gulp.dest("rev/js"))
//
//});
//使用bower的时候单独提出用到的文件到某目录
gulp.task('testflatten', function () {
gulp.src('js/**/*.js')
.pipe(flatten({includeParents: 1}))
.pipe(gulp.dest('build/js'));
});
//手动替换,类似于htmlreplace
gulp.task('testreplace', function () {
gulp.src(['test.html'])
.pipe(replace('js/app.js', 'js/a.js'))
.pipe(gulp.dest('build'));
});
//可以进行资源文件合并之后重命名
gulp.task('testHtmlReplace', function () {
gulp.src('index2.html')
.pipe(htmlreplace({
'maincss': 'styles.min.css',
//'js': ['js/bundle1.min.js','js/bundle2.min.js'],
//js: {
// src: [['data-main.js', 'require-src.js']],
// tpl: '<script data-main="%s" src="%s"></script>'
//}
}, {
keepUnassigned: true,
keepBlockTags: true,//是否保持注释
resolvePaths: false//是否移除没匹配上的
}))
.pipe(gulp.dest('build/'));
});
gulp.task("testUsefrf", function () {
//var jsFilter = filter("**/*.js");
//var cssFilter = filter("**/*.css");
//
//var userefAssets = useref.assets();
//
//return gulp.src("src/index.html")
// .pipe(userefAssets) // Concatenate with gulp-useref
// .pipe(jsFilter)
// .pipe(uglify()) // Minify any javascript sources
// .pipe(jsFilter.restore())
// .pipe(cssFilter)
// .pipe(csso()) // Minify any CSS sources
// .pipe(cssFilter.restore())
// .pipe(rev()) // Rename the concatenated files
// .pipe(userefAssets.restore())
// .pipe(useref())
// .pipe(revReplace()) // Substitute in new filenames
// .pipe(gulp.dest('public'));
});
gulp.task('default', ['dev']);
gulp.task('watch', function () {
var watcher = gulp.watch('testchanged/**/*.js', ['testchanged']);
watcher.on('change', function (event) {
console.log(JSON.stringify(event), 'File ' + event.path + ' was ' + event.type + ', running tasks...');
});
})
gulp.task('dev', function (done) {
//runSequence(
// //['testreplace', 'testflatten', 'testHtmlReplace', 'testUsefrf'],
// ['img', 'css', 'js'],
// ['revcollector'],
// //['watch'],
// //['revreplace'],
// done
//);
//gulp.run('img',[ 'css', 'js']);
});
//css和js同是reviserion
//gulp.task("revision", function () {
// return gulp.src(["dist/**/*.css", "dist/**/*.js"])
// .pipe(rev())
// .pipe(gulp.dest(opt.distFolder))
// .pipe(rev.manifest())
// .pipe(gulp.dest(opt.distFolder))
//})
//更好
gulp.task("revreplace", function () {
var manifest = gulp.src("rev/**/*.json");
return gulp.src("index.html")
.pipe(revReplace({manifest: manifest}))
.pipe(gulp.dest('testt'));
});
gulp.task('revcollector', function () {
return gulp.src(['rev/**/*.json', 'index.html'])
.pipe(revCollector())
.pipe(gulp.dest('dist/'));
});