gulpfile.js
3.08 KB
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
/**
* gulp file 执行compass解析,spm build以及文件合并压缩等
* author: xuqi(qi.xu@yoho.cn)
* date; 2015/3/27
*/
var gulp = require('gulp'),
fs = require('fs'),
exec = require('child_process').exec,
concat = require('gulp-concat'),
ftp = require('gulp-ftp'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
transport = require('gulp-spm'),
Package = require('father').SpmPackage,
md5 = require('gulp-md5');
var config = JSON.parse(fs.readFileSync('./package.json').toString()),
distDir = {
libJs: 'dist/' + config.name + '/',
js: 'dist/' + config.name + '/' + config.version,
css: 'dist/' + config.name + '/' + config.version,
image: 'dist/' + config.name + '/assets/images',
fonts: 'dist/' + config.name + '/assets/fonts'
},
cdnDomain = 'http://cdn.yoho.cn/',
ftpConfig = {
host: '218.94.75.58',
user: 'php',
pass: 'yoho9646'
};
gulp.task('dist', function() {
var ftpstream = ftp(ftpConfig);
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
gulp.task('build', ['libs-build']);
//库文件打包
gulp.task('libs-build', ['pre-libs', 'min-libs', 'clear-libs']);
gulp.task('pre-libs', function() {
libPkgPre();
var pkg = new Package(__dirname);
return gulp.src(pkg.main)
.pipe(transport({
pkg: pkg
}))
.pipe(gulp.dest(distDir.libJs));
});
gulp.task('min-libs', ['pre-libs'], function() {
return gulp.src(['sea/sea-debug.js', distDir.libJs + 'libs.js'])
.pipe(concat('libs.js'))
.pipe(uglify())
.pipe(md5())
.pipe(gulp.dest(distDir.libJs));
});
gulp.task('clear-libs', ['min-libs'], function() {
fs.renameSync('package_bak.json', 'package.json'); //恢复原包配置文件
fs.unlinkSync(distDir.libJs + 'libs.js'); //删除md5前生成的lib
fs.unlinkSync('./libs.js'); //删除入口文件
});
//库文件的入口文件和过程文件的生成
function libPkgPre() {
var obj = {
name: config.name,
version: config.version,
spm: config.spm
},
libCon, libsjs = '',
key,
libDir = distDir.js + '';
obj.spm.main = 'libs.js';
obj.spm.buildArgs = '--idleading {{}} --include all';
libCon = JSON.stringify(obj);
fs.renameSync('package.json', 'package_bak.json');
fs.writeFileSync('package.json', libCon); //生成库文件pkg配置
var packages = [];
for (key in obj.spm.dependencies) {
console.log(obj.spm.inside);
if (obj.spm.inside && obj.spm.inside[key]) {
packages.push(obj.spm.inside[key]);
} else {
packages.push(key);
}
}
for (var i = 0; i < packages.length; i++) {
//iscroll执行打包子模块命令
if (packages[i] === 'iscroll') {
libsjs += 'require("iscroll/iscroll-probe");';
break;
}
libsjs += "require('" + packages[i] + "');";
}
fs.writeFileSync('libs.js', libsjs); //写入口文件
return libDir;
}