gulpfile.js
3.69 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
/**
* 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');
var config = JSON.parse(fs.readFileSync('./package.json').toString());
var assets_dir = 'dist/' + config.name + '/assets';
var dist_dir = {
js: 'dist/' + config.name + '/' + config.version,
css: 'dist/' + config.name + '/' + config.version,
image: assets_dir + '/images',
font: assets_dir + '/fonts'
};
var ftpConfig = {
host: '58.213.133.26',
user: 'php',
pass: 'yoho9646'
};
// 启动
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/**/*.html', 'views/controller/*.js',
'layouts/*.html', 'public/css/*.css', 'public/js/data.js'], server.restart);
});
//compass 解析压缩合并
gulp.task('compass-watch', function() {
gulp.watch('public/sass/**/*.scss', ['compass']);
});
gulp.task('compass', function() {
gulp.src('public/sass/**/*.scss')
.pipe(
compass({
config_file: 'config.rb',
css: 'public/css',
sass: 'public/sass'
})
)
});
//spm build
gulp.task('build', function() {
exec('spm build --include all', function() {
exec('gulp build-debug');
exec('gulp build-production');
});
});
//generate all-debug.js for development ENV
gulp.task('build-debug', function() {
gulp.src(['public/lib/sea.js', 'dist/yohobuy-mobile/1.0.0/index-debug.js'])
.pipe(concat('all-debug.js'))
.pipe(gulp.dest('public/dist'))
});
//generate all.js for production ENV
gulp.task('build-production', function() {
gulp.src(['public/lib/sea.js', 'dist/yohobuy-mobile/1.0.0/index.js'])
.pipe(concat('all.js'))
.pipe(gulp.dest('public/dist'))
});
//线上环境使用
//发布到CND
gulp.task('dist', ['assets', 'compass-production', 'spm-build-production'], function() {
var ftpstream = ftp(ftpConfig);
return gulp.src('dist/**/')
.pipe(ftpstream)
.pipe(gutil.noop());
});
//STEP1:拷贝fonts+images到发布目录
gulp.task('assets',function() {
gulp.src('public/img/**')
.pipe(gulp.dest(dist_dir.image));
gulp.src('public/fonts/*')
.pipe(gulp.dest(dist_dir.font));
});
//STEP2:compass整合所有css到index后发布到发布目录
gulp.task('compass-production', function() {
gulp.src('public/sass/index.scss')
.pipe(
compass({
css: dist_dir.css,
sass: 'public/sass',
image: dist_dir.image,
font: dist_dir.font,
http_path: '/',
style: 'compressed'
})
)
.on('error', function(error) {
console.log(error);
this.emit('end');
});
});
//STEP3:spm
gulp.task('spm-build-production', function() {
exec('spm build --include all', function() {
gulp.src(['public/lib/sea-pro.js', 'dist/yohobuy-mobile/1.0.0/index-debug.js'])
.pipe(concat('index-debug.js'))
.pipe(gulp.dest(dist_dir.js));
gulp.src(['public/lib/sea-pro.js', 'dist/yohobuy-mobile/1.0.0/index.js'])
.pipe(concat('index.js'))
.pipe(gulp.dest(dist_dir.js))
});
});