Skip to content

Commit 30fdf4d

Browse files
committed
dev
1 parent b0da7f8 commit 30fdf4d

File tree

4 files changed

+141
-27
lines changed

4 files changed

+141
-27
lines changed

_tasks/TaskBuildDist.js

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var fs = require('fs');
12
var del = require('del');
23
var path = require('path');
34
var ejs = require('gulp-ejs');
@@ -19,6 +20,7 @@ var posthtml = require('gulp-posthtml');
1920
var posthtmlPx2rem = require('posthtml-px2rem');
2021
var RevAll = require('gulp-rev-all'); // reversion
2122
var revDel = require('gulp-rev-delete-original');
23+
var changed = require('./common/changed')();
2224

2325
var paths = {
2426
src: {
@@ -33,6 +35,13 @@ var paths = {
3335
htmlAll: './src/html/**/*',
3436
php: './src/**/*.php'
3537
},
38+
tmp: {
39+
dir: './tmp',
40+
css: './tmp/css',
41+
img: './tmp/img',
42+
html: './tmp/html',
43+
sprite: './tmp/sprite'
44+
},
3645
dist: {
3746
dir: './dist',
3847
css: './dist/css',
@@ -64,38 +73,43 @@ module.exports = function (gulp, config) {
6473
]
6574
}
6675

67-
// 清除目标目录
76+
// 清除 dist 目录
6877
function delDist() {
6978
return del([paths.dist.dir]);
7079
}
7180

81+
// 清除 tmp 目录
82+
function delTmp() {
83+
return del([paths.tmp.dir]);
84+
}
85+
7286
//编译 less
7387
function compileLess() {
7488
return gulp.src(paths.src.less)
7589
.pipe(less())
7690
.pipe(lazyImageCSS({imagePath: lazyDir}))
7791
.pipe(tmtsprite({margin: 4}))
78-
.pipe(gulpif('*.png', gulp.dest(paths.dist.sprite), gulp.dest(paths.dist.css)));
92+
.pipe(gulpif('*.png', gulp.dest(paths.tmp.sprite), gulp.dest(paths.tmp.css)));
7993
}
8094

8195
//自动补全
8296
function compileAutoprefixer() {
83-
return gulp.src('./dist/css/style-*.css')
97+
return gulp.src('./tmp/css/style-*.css')
8498
.pipe(postcss(postcssOption))
85-
.pipe(gulp.dest('./dist/css/'));
99+
.pipe(gulp.dest('./tmp/css/'));
86100
}
87101

88102
//CSS 压缩
89-
function miniCSS(){
90-
return gulp.src('./dist/css/style-*.css')
103+
function miniCSS() {
104+
return gulp.src('./tmp/css/style-*.css')
91105
.pipe(minifyCSS({
92106
safe: true,
93107
reduceTransforms: false,
94108
advanced: false,
95109
compatibility: 'ie7',
96110
keepSpecialComments: 0
97111
}))
98-
.pipe(gulp.dest('./dist/css/'));
112+
.pipe(gulp.dest('./tmp/css/'));
99113
}
100114

101115
//图片压缩
@@ -104,7 +118,7 @@ module.exports = function (gulp, config) {
104118
.pipe(imagemin({
105119
use: [pngquant()]
106120
}))
107-
.pipe(gulp.dest(paths.dist.img));
121+
.pipe(gulp.dest(paths.tmp.img));
108122
}
109123

110124
//复制媒体文件
@@ -116,16 +130,16 @@ module.exports = function (gulp, config) {
116130
function uglifyJs() {
117131
return gulp.src(paths.src.js, {base: paths.src.dir})
118132
.pipe(uglify())
119-
.pipe(gulp.dest(paths.dist.dir));
133+
.pipe(gulp.dest(paths.tmp.dir));
120134
}
121135

122136
//雪碧图压缩
123137
function imageminSprite() {
124-
return gulp.src('./dist/sprite/**/*')
138+
return gulp.src('./tmp/sprite/**/*')
125139
.pipe(imagemin({
126140
use: [pngquant()]
127141
}))
128-
.pipe(gulp.dest(paths.dist.sprite));
142+
.pipe(gulp.dest(paths.tmp.sprite));
129143
}
130144

131145
//html 编译
@@ -144,7 +158,7 @@ module.exports = function (gulp, config) {
144158
.pipe(usemin({ //JS 合并压缩
145159
jsmin: uglify()
146160
}))
147-
.pipe(gulp.dest(paths.dist.html));
161+
.pipe(gulp.dest(paths.tmp.html));
148162
}
149163

150164
//webp 编译
@@ -166,18 +180,54 @@ module.exports = function (gulp, config) {
166180
});
167181

168182
if (config['reversion']) {
169-
return gulp.src(['./dist/**/*'])
183+
return gulp.src(['./tmp/**/*'])
170184
.pipe(revAll.revision())
171-
.pipe(gulp.dest(paths.dist.dir))
185+
.pipe(gulp.dest(paths.tmp.dir))
172186
.pipe(revDel({
173187
exclude: /(.html|.htm)$/
174188
}))
175189
.pipe(revAll.manifestFile())
176-
.pipe(gulp.dest(paths.dist.dir));
190+
.pipe(gulp.dest(paths.tmp.dir));
177191
} else {
178192
cb();
179193
}
180194
}
195+
196+
function findChanged(cb) {
197+
198+
var diff = changed('./tmp');
199+
var tmpSrc = [];
200+
201+
if(process.argv[2].indexOf(':all') > -1){
202+
return gulp.src('./tmp/**/*', {base: paths.tmp.dir})
203+
.pipe(gulp.dest(paths.dist.dir))
204+
.on('end', function(){
205+
delTmp();
206+
})
207+
}
208+
209+
if(diff){
210+
for(var i in diff){
211+
tmpSrc.push('./tmp/' + i);
212+
console.log(i + ' has changed!');
213+
}
214+
215+
if(!tmpSrc.length){
216+
console.log('Nothing changed!');
217+
delTmp();
218+
cb();
219+
}else{
220+
return gulp.src(tmpSrc, {base: paths.tmp.dir})
221+
.pipe(gulp.dest(paths.dist.dir))
222+
.on('end', function(){
223+
delTmp();
224+
})
225+
}
226+
}else{
227+
cb();
228+
}
229+
}
230+
181231

182232
//加载插件
183233
function loadPlugin(cb) {
@@ -200,7 +250,13 @@ module.exports = function (gulp, config) {
200250
compileHtml,
201251
reversion,
202252
supportWebp(),
253+
findChanged,
203254
loadPlugin
204255
));
256+
257+
//注册 build_dist 任务
258+
gulp.task('build_dist:all', gulp.series(
259+
'build_dist'
260+
));
205261
};
206262

_tasks/common/changed.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var md5 = require('crypto-md5');
2+
var path = require('path');
3+
var rd = require('rd');
4+
var fs = require('fs');
5+
var _ = require('lodash');
6+
7+
function changed(dir) {
8+
9+
var manifestPath = path.resolve('./src/manifest.json');
10+
var manifest = {};
11+
var originManifest = {};
12+
13+
//如果存在 manifest.json, 则加载保存
14+
if (fs.existsSync(manifestPath)) {
15+
originManifest = require(manifestPath);
16+
}
17+
18+
//遍历目录, 根据内容 md5 加密
19+
rd.eachFileFilterSync(dir, function (file) {
20+
var index = path.relative(dir, file);
21+
22+
//过滤掉 隐藏文件 和 manifest.json
23+
if (path.extname(file) && index !== 'manifest.json' && fs.existsSync(file)) {
24+
25+
var data = fs.readFileSync(file);
26+
27+
if (data) {
28+
manifest[index] = md5(data, 'hex');
29+
}
30+
}
31+
32+
});
33+
34+
//将新的 manifest.json 保存
35+
fs.writeFile(manifestPath, JSON.stringify(manifest), function (err) {
36+
if (err) throw err;
37+
});
38+
39+
//找出有变动的文件
40+
if(originManifest){
41+
var diff = {};
42+
43+
_.forEach(manifest, function (item, index) {
44+
if (originManifest[index] !== item) {
45+
diff[index] = item;
46+
}
47+
});
48+
}
49+
50+
return diff;
51+
52+
}
53+
54+
55+
module.exports = function(){
56+
return changed;
57+
};

_tasks/common/webp.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,43 +43,43 @@ module.exports = function (config, done) {
4343
return function () {
4444
return gulp.series(
4545
function compileSprite() {
46-
return gulp.src('./dist/sprite/**/*')
46+
return gulp.src('./tmp/sprite/**/*')
4747
.pipe(gulpWebp())
48-
.pipe(gulp.dest('./dist/sprite'));
48+
.pipe(gulp.dest('./tmp/sprite'));
4949
},
5050
function compileImg () {
51-
return gulp.src('./dist/img/**/*')
51+
return gulp.src('./tmp/img/**/*')
5252
.pipe(gulpWebp())
53-
.pipe(gulp.dest('./dist/img'));
53+
.pipe(gulp.dest('./tmp/img'));
5454
},
5555
//智能寻找 webp
5656
function find2Webp (cb) {
57-
render_webp('./dist/sprite');
58-
render_webp('./dist/img');
57+
render_webp('./tmp/sprite');
58+
render_webp('./tmp/img');
5959
if(imgArr.length){
6060
reg = eval('/(' + imgArr.join('|') + ')/ig');
6161
}
6262
cb();
6363
},
6464
function compileCss () {
65-
return gulp.src(['./dist/css/**/*.css', '!./dist/css/**/*.webp.css'])
65+
return gulp.src(['./tmp/css/**/*.css', '!./tmp/css/**/*.webp.css'])
6666
.pipe(rename({suffix: '.webp'}))
6767
.pipe(replace(reg, function (match) {
6868
if(match){
6969
return match.substring(0, match.lastIndexOf('.')) + '.webp';
7070
}
7171
}))
72-
.pipe(gulp.dest('./dist/css'));
72+
.pipe(gulp.dest('./tmp/css'));
7373
},
7474
function insertWebpJs () {
7575
var preload_script = '<script>window.imgMap = ' + JSON.stringify(imgMap) + '</script>';
7676

77-
return gulp.src('./dist/html/**/*.html')
77+
return gulp.src('./tmp/html/**/*.html')
7878
.pipe(replace('data-href', 'href'))
7979
.pipe(replace(/(link.*?)href/ig, '$1data-href'))
8080
.pipe(replace('</head>', webpScript))
8181
.pipe(replace('</head>', preload_script))
82-
.pipe(gulp.dest('./dist/html'));
82+
.pipe(gulp.dest('./tmp/html'));
8383
}
8484
);
8585
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"posthtml-px2rem": "^0.0.3",
3232
"rc": "^1.1.6",
3333
"rd": "^0.0.2",
34-
"tmt-ejs-helper": "^0.0.1"
34+
"tmt-ejs-helper": "^0.0.1",
35+
"crypto-md5": "^1.0.0"
3536
},
3637
"description": "A f2e project workflow based on Gulp",
3738
"repository": {
@@ -56,4 +57,4 @@
5657
},
5758
"homepage": "https://github.com/weixin/tmt-workflow",
5859
"readmeFilename": "README.md"
59-
}
60+
}

0 commit comments

Comments
 (0)