gulpfile.js
5.09 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
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
/**
* GULP ENTRY
* author: xuqi(qi.xu@yoho.cn)
* date: 2015/6/29
*/
var gulp = require('gulp'),
fs = require('fs'),
ftp = require('gulp-ftp'),
gutil = require('gulp-util'),
concat = require('gulp-concat'),
compass = require('gulp-compass'),
exec = require('child_process').exec,
mkdirp = require('mkdirp'),
uglify = require('gulp-uglify'),
Package = require('father').SpmPackage,
transport = require('gulp-spm'),
request = require('request'),
// md5 = require("gulp-md5"),
rmdir = require('rmdir')
server = require('gulp-develop-server');
gulp.task('default', ['server', 'server:restart', 'compass-watch', 'compass']);
// start express server
gulp.task('server', function() {
server.listen({
path: 'app.js'
});
});
// restart server if app.js changed
gulp.task('server:restart', function() {
gulp.watch(['app.js', 'views/controller/*.js', 'public/js/data.js'], server.restart);
});
// compass watch
gulp.task('compass-watch', function() {
gulp.watch('public/sass/**/*.scss', ['compass']);
});
//compass
gulp.task('compass', function() {
gulp.src('public/sass/**/*.scss')
.pipe(
compass({
config_file: 'config.rb',
css: 'public/css',
sass: 'public/sass'
})
)
});
//PRODUCTION
//cdn
var config = JSON.parse(fs.readFileSync('./package.json').toString()),
distRoot = 'dist/yohobuy-mobile/',
distDir = {
js: distRoot + config.name + '/' + config.version,
css: distRoot + config.name + '/' + config.version,
image: distRoot + config.name + '/assets/images',
fonts: distRoot + config.name + '/assets/fonts'
},
cdnDomain = 'http://cdn.yoho.cn/',
ftpConfig = {
host: '58.213.133.26',
user: 'php',
pass: 'yoho9646'
};
gulp.task('dist', function() {
var ftpstream = ftp(ftpConfig);
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
//拷贝fonts+images到发布目录
gulp.task('assets', function() {
gulp.src('public/img/**')
.pipe(gulp.dest(distDir.image));
gulp.src('public/fonts/*')
.pipe(gulp.dest(distDir.fonts));
});
//compass整合所有css到index后发布到发布目录
gulp.task('compass-production', function() {
gulp.src('public/sass/*.scss')
.pipe(
compass({
css: distDir.css,
sass: 'public/sass',
image: distDir.image,
font: distDir.fonts,
http_path: '/',
style: 'compressed'
})
)
.on('error', function(error) {
console.log(error);
this.emit('end');
});
});
//JS build
gulp.task('build', ['libs-build', 'index-build']);
gulp.task('libs-build', ['pre-libs', 'concat-libs', 'min-libs', 'clear-libs']);
gulp.task('index-build', ['libs-build'], function() {
var pkg = new Package(__dirname);
return gulp.src(pkg.main)
.pipe(transport({
pkg: pkg
}))
.pipe(concat('index-debug.js'))
.pipe(gulp.dest(distDir.js))
.pipe(uglify())
.pipe(concat('index.js'))
.pipe(gulp.dest(distDir.js));
});
//SPM 打包库文件
gulp.task('pre-libs', function() {
libPkgPre();
var pkg = new Package(__dirname);
return gulp.src(pkg.main)
.pipe(transport({
pkg: pkg
}))
.pipe(gulp.dest(distDir.js));
});
//合并库文件
gulp.task('concat-libs', ['pre-libs'], function() {
return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
.pipe(concat('libs-debug.js'))
.pipe(gulp.dest(distDir.js));
});
//压缩库文件
gulp.task('min-libs', ['pre-libs'], function() {
return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
.pipe(concat('libs.js'))
.pipe(uglify())
// .pipe(md5())
.pipe(gulp.dest(distDir.js));
});
//清除过程文件
gulp.task('clear-libs', ['concat-libs', 'min-libs'], function() {
fs.renameSync('package_bak.json', 'package.json'); //恢复原包配置文件
fs.unlinkSync('./libs.js'); //删除入口文件
// fs.unlinkSync(distDir.js + '/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) {
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++) {
libsjs += "require('" + packages[i] + "');";
}
fs.writeFileSync('libs.js', libsjs); //写入口文件
return libDir;
}