gulpfile.js
8.29 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
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
/**
* gulp file 执行compass解析,spm build以及文件合并压缩等
* author: xuqi(qi.xu@yoho.cn)
* date; 2015/3/27
*/
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,
server = require('gulp-develop-server'),
mkdirp = require('mkdirp'),
uglify = require('gulp-uglify'),
cssmin = require('gulp-cssmin'),
Package = require('father').SpmPackage,
transport = require('gulp-spm'),
md5 = require("gulp-md5"),
request = require('request'),
rmdir = require('rmdir');
var cssUrls = require('gulp-css-urls');
var config = JSON.parse(fs.readFileSync('./package.json').toString());
var assets_dir = 'dist/' + config.name + '/assets';
var public_dir = '../public';
var server_dir = '../server';
var dist_dir = {
js: 'dist/' + config.name + '/' + config.version,
css: 'dist/' + config.name + '/' + config.version,
image: assets_dir + '/images',
font: assets_dir + '/fonts'
};
var cdn_domain = 'http://cdn.yoho.cn/';
var ftpConfig = {
host: '218.94.75.58',
user: 'php',
pass: 'yoho9646'
};
// 本地运行时
// 启动
gulp.task('start', ['server', 'server:restart', 'compass-watch', 'compass']);
gulp.task('default', ['compass', 'compass-production', 'build']);
// start express server
gulp.task('server', function() {
server.listen({
path: server_dir + '/app.js'
});
});
// restart server if app.js changed
gulp.task('server:restart', function() {
gulp.watch([
server_dir + '/app.js', server_dir + '/views/**/*.html', server_dir + '/views/controller/*.js',
server_dir + '/views/*.html', public_dir + '/css/*.css', public_dir + '/js/data.js'
], server.restart);
});
//compass 解析压缩合并
gulp.task('compass-watch', function() {
gulp.watch('sass/**/*.scss', ['compass']);
});
//样式表的预编译
gulp.task('compass', function() {
gulp.src('sass/**/*.scss')
.pipe(
compass({
config_file: 'config.rb',
css: public_dir + '/css',
sass: 'sass'
})
)
});
//发布到CDN
gulp.task('dist', function() {
var ftpstream = ftp(ftpConfig);
//发布CDN的时候,由于目录结构发生变化
// gulp.src(dist_dir.css+"/all.css")
// .pipe(cssUrls(function(url) {
// return url.replace('../fonts','../assets/fonts');
// }, {
// sourcemaps: true,
// }))
// .pipe(cssmin())
// .pipe(gulp.dest(dist_dir.css));
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
//STEP1:拷贝fonts+images到发布目录
gulp.task('assets', function() {
gulp.src(public_dir + '/img/**')
.pipe(gulp.dest(dist_dir.image));
gulp.src(public_dir + '/fonts/*')
.pipe(gulp.dest(dist_dir.font));
});
//STEP2:compass整合所有css到index后发布到发布目录
gulp.task('compass-production', ['assets', 'libs-build'], function() {
gulp.src('sass/index.scss')
.pipe(
compass({
css: dist_dir.css,
sass: 'sass',
image: dist_dir.image,
font: dist_dir.font,
http_path: '/',
style: 'compressed'
})
)
.on('error', function(error) {
console.log(error);
this.emit('end');
});
});
//spm build
gulp.task('build', ['libs-build', 'index-build', 'config-libs', 'compass-production'], function() {
gulp.src(dist_dir.js + '/**')
.pipe(gulp.dest(public_dir + '/dist'));
var a=[ public_dir + '/css/bootstrap.min.css',
public_dir + '/css/bootstrap-override.css',
public_dir + '/css/font-awesome.min.css',
public_dir + '/css/newselect2.css',
public_dir + '/css/jquery.gritter.css',
public_dir + '/css/global.css',
public_dir + '/css/css1.css',
public_dir +'/css/index.css'];
gulp.src(a)
.pipe(concat('all.css'))
.pipe(cssUrls(function(url) {
return url.replace('../fonts','../assets/fonts');
}, {
sourcemaps: true,
}))
.pipe(cssmin())
.pipe(gulp.dest(dist_dir.css));
});
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(dist_dir.js))
.pipe(uglify())
.pipe(concat('index.js'))
.pipe(gulp.dest(dist_dir.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(dist_dir.js));
});
//清除过程文件
gulp.task('clear-libs', ['concat-libs', 'min-libs'], function() {
fs.renameSync('package_bak.json', 'package.json'); //恢复原包配置文件
fs.unlinkSync('./libs.js'); //删除入口文件
fs.unlinkSync(dist_dir.js + '/libs.js');
});
//合并库文件
gulp.task('concat-libs', ['pre-libs'], function() {
delLibsMainModule();
return gulp.src([public_dir + '/sea.js', dist_dir.js + '/libs.js'])
.pipe(concat('libs-all.js'))
.pipe(gulp.dest(dist_dir.js));
});
//压缩库文件
gulp.task('min-libs', ['pre-libs'], function() {
return gulp.src([public_dir + '/sea.js', dist_dir.js + '/libs.js'])
.pipe(concat('deps.js'))
.pipe(uglify())
.pipe(md5())
.pipe(gulp.dest('./dist/libs'));
});
//配置静态文件目录
gulp.task('config-libs', ['libs-build'], function() {
var files = fs.readdirSync('./dist/libs'),
preStr = 'exports.staticDir = ',
i, data, depsFile;
for (i = 0; i < files.length; i++) {
if (files[i].indexOf('deps_') > -1) {
data = {
test: {
libs: '/dist/libs-all.js',
js: '/dist/index-debug.js',
css:'/css/all.css'
},
preview: {
libs: cdn_domain + 'libs/' + files[i],
js: cdn_domain + config.name + '/' + config.version + '/index.js',
css: cdn_domain + config.name + '/' + config.version + '/all.css'
},
production: {
libs: cdn_domain + 'libs/' + files[i],
js: cdn_domain + config.name + '/' + config.version + '/index.js',
css: cdn_domain + config.name + '/' + config.version + '/all.css'
}
};
depsFile = files[i];
//写入静态文件配置
fs.writeFileSync('../server/staticConfig.js', preStr + JSON.stringify(data));
}
}
request(cdn_domain + 'libs/' + depsFile, function(error, response, body) {
if (response.statusCode == 200) {
rmdir('./dist/libs', function() {});
}
})
});
//库文件执行
gulp.task('libs-build', ['pre-libs', 'concat-libs', 'min-libs', 'clear-libs']);
//库文件的入口文件和过程文件的生成
function libPkgPre() {
var obj = {
name: '',
version: config.version,
spm: config.spm
},
libCon, libsjs = '',
key,
libDir = dist_dir.js + '';
obj.spm.main = 'libs.js';
obj.spm.buildArgs = '--idleading {{name}} --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;
}
//删除库模块的入口模块
function delLibsMainModule() {
var path = dist_dir.js + '/libs.js';
var jsStr = fs.readFileSync(path).toString();
var position = jsStr.indexOf('});');
jsStr = jsStr.substr(position + 4);
fs.writeFileSync(path, jsStr);
}