|
|
1
|
+/**
|
|
|
2
|
+ * GULP-FILE
|
|
|
3
|
+ * author: xuqi(qi.xu@yoho.cn)
|
|
|
4
|
+ * date: 2015/9/30
|
|
|
5
|
+ */
|
|
|
6
|
+
|
|
|
7
|
+var gulp = require('gulp'),
|
|
|
8
|
+ compass = require('gulp-compass'),
|
|
|
9
|
+ cp = require('child_process');
|
|
|
10
|
+
|
|
|
11
|
+var fs = require('fs'),
|
|
|
12
|
+ ftp = require('gulp-ftp'),
|
|
|
13
|
+ gutil = require('gulp-util'),
|
|
|
14
|
+ uglify = require('gulp-uglify'),
|
|
|
15
|
+ Package = require('father').SpmPackage,
|
|
|
16
|
+ transport = require('gulp-spm'),
|
|
|
17
|
+ concat = require('gulp-concat');
|
|
|
18
|
+
|
|
|
19
|
+var config = JSON.parse(fs.readFileSync('./package.json').toString());
|
|
|
20
|
+
|
|
|
21
|
+var rootDist = 'dist/yohobuy/',
|
|
|
22
|
+ ftpConfig = {
|
|
|
23
|
+ host: '218.94.75.58',
|
|
|
24
|
+ user: 'php',
|
|
|
25
|
+ pass: 'yoho9646'
|
|
|
26
|
+ };
|
|
|
27
|
+
|
|
|
28
|
+var distDir = {
|
|
|
29
|
+ js: rootDist + config.version,
|
|
|
30
|
+ css: rootDist + config.version,
|
|
|
31
|
+ img: rootDist + 'assets/img',
|
|
|
32
|
+ font: rootDist + 'assets/font'
|
|
|
33
|
+};
|
|
|
34
|
+
|
|
|
35
|
+gulp.task('default', ['compass', 'compass-watch', 'spm-doc']);
|
|
|
36
|
+
|
|
|
37
|
+// compass
|
|
|
38
|
+gulp.task('compass', function() {
|
|
|
39
|
+ gulp.src('sass/**/*.scss')
|
|
|
40
|
+ .pipe(
|
|
|
41
|
+ compass({
|
|
|
42
|
+ config_file: 'config.rb',
|
|
|
43
|
+ css: 'css',
|
|
|
44
|
+ sass: 'sass'
|
|
|
45
|
+ })
|
|
|
46
|
+ ).on('error', function(error) {
|
|
|
47
|
+ console.log(error);
|
|
|
48
|
+ this.emit('end');
|
|
|
49
|
+ });
|
|
|
50
|
+});
|
|
|
51
|
+
|
|
|
52
|
+// compass watch
|
|
|
53
|
+gulp.task('compass-watch', function() {
|
|
|
54
|
+ gulp.watch('sass/**/*.scss', ['compass']);
|
|
|
55
|
+});
|
|
|
56
|
+
|
|
|
57
|
+// start spm server
|
|
|
58
|
+gulp.task('spm-doc', function() {
|
|
|
59
|
+ var sd = cp.exec('spm doc');
|
|
|
60
|
+
|
|
|
61
|
+ // sd.stdout.on('data', function(data) {
|
|
|
62
|
+ // console.log(data);
|
|
|
63
|
+ // });
|
|
|
64
|
+
|
|
|
65
|
+ sd.stderr.on('data', function(data) {
|
|
|
66
|
+ console.log(data);
|
|
|
67
|
+ });
|
|
|
68
|
+
|
|
|
69
|
+ sd.on('exit', function(code) {
|
|
|
70
|
+ console.log('process spm doc exit with code ' + code);
|
|
|
71
|
+ });
|
|
|
72
|
+});
|
|
|
73
|
+
|
|
|
74
|
+//生成发布目录,可用于上传测试机
|
|
|
75
|
+gulp.task('ge', ['assets', 'compass-production', 'build', 'script']);
|
|
|
76
|
+
|
|
|
77
|
+//发布
|
|
|
78
|
+gulp.task('dist', ['assets', 'compass-production', 'build', 'script'], function() {
|
|
|
79
|
+ var ftpstream = ftp(ftpConfig);
|
|
|
80
|
+
|
|
|
81
|
+ return gulp.src('dist/**/')
|
|
|
82
|
+ .pipe(ftpstream)
|
|
|
83
|
+ .pipe(gutil.noop());
|
|
|
84
|
+});
|
|
|
85
|
+
|
|
|
86
|
+//font+img->dist/assets
|
|
|
87
|
+gulp.task('assets', function() {
|
|
|
88
|
+ gulp.src('img/**')
|
|
|
89
|
+ .pipe(gulp.dest(distDir.img));
|
|
|
90
|
+ gulp.src('font/*')
|
|
|
91
|
+ .pipe(gulp.dest(distDir.font));
|
|
|
92
|
+});
|
|
|
93
|
+
|
|
|
94
|
+//compass
|
|
|
95
|
+gulp.task('compass-production', ['assets'], function() {
|
|
|
96
|
+ gulp.src('sass/index.scss')
|
|
|
97
|
+ .pipe(
|
|
|
98
|
+ compass({
|
|
|
99
|
+ css: distDir.css,
|
|
|
100
|
+ sass: 'sass',
|
|
|
101
|
+ image: distDir.img,
|
|
|
102
|
+ font: distDir.font,
|
|
|
103
|
+ http_path: '/',
|
|
|
104
|
+ style: 'compressed'
|
|
|
105
|
+ })
|
|
|
106
|
+ )
|
|
|
107
|
+ .on('error', function(error) {
|
|
|
108
|
+ console.log(error);
|
|
|
109
|
+ this.emit('end');
|
|
|
110
|
+ });
|
|
|
111
|
+});
|
|
|
112
|
+
|
|
|
113
|
+//spm build
|
|
|
114
|
+gulp.task('build', ['libs', 'business']);
|
|
|
115
|
+
|
|
|
116
|
+//libs
|
|
|
117
|
+gulp.task('libs', ['pre-libs', 'concat-min-libs', 'clear-libs']);
|
|
|
118
|
+
|
|
|
119
|
+gulp.task('pre-libs', function() {
|
|
|
120
|
+ var obj = {
|
|
|
121
|
+ name: '',
|
|
|
122
|
+ version: config.version,
|
|
|
123
|
+ spm: config.spm
|
|
|
124
|
+ };
|
|
|
125
|
+ var packages = [],
|
|
|
126
|
+ libsjs = '';
|
|
|
127
|
+
|
|
|
128
|
+ var libCon, key, i;
|
|
|
129
|
+
|
|
|
130
|
+ var pkg;
|
|
|
131
|
+
|
|
|
132
|
+ obj.spm.main = 'libs.js';
|
|
|
133
|
+ obj.spm.buildArgs = '--idleading {{}} --include all';
|
|
|
134
|
+
|
|
|
135
|
+ libCon = JSON.stringify(obj);
|
|
|
136
|
+
|
|
|
137
|
+ fs.renameSync('package.json', 'package.bak.json');
|
|
|
138
|
+ fs.writeFileSync('package.json', libCon);
|
|
|
139
|
+
|
|
|
140
|
+ for (key in obj.spm.dependencies) {
|
|
|
141
|
+ if (obj.spm.inside && obj.spm.inside[key]) {
|
|
|
142
|
+ packages.push(obj.spm.inside[key]);
|
|
|
143
|
+ } else {
|
|
|
144
|
+ packages.push(key);
|
|
|
145
|
+ }
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ for (i = 0; i < packages.length; i++) {
|
|
|
149
|
+ libsjs += 'require("' + packages[i] + '");';
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ fs.writeFileSync('libs.js', libsjs); //重写入口文件
|
|
|
153
|
+
|
|
|
154
|
+ pkg = new Package(__dirname);
|
|
|
155
|
+
|
|
|
156
|
+ return gulp.src(pkg.main)
|
|
|
157
|
+ .pipe(transport({
|
|
|
158
|
+ pkg: pkg
|
|
|
159
|
+ }))
|
|
|
160
|
+ .pipe(gulp.dest(distDir.js));
|
|
|
161
|
+});
|
|
|
162
|
+
|
|
|
163
|
+gulp.task('concat-min-libs', ['pre-libs'], function() {
|
|
|
164
|
+ var path = distDir.js + '/libs.js';
|
|
|
165
|
+ var jsStr = fs.readFileSync(path).toString();
|
|
|
166
|
+
|
|
|
167
|
+ fs.writeFileSync(path, jsStr.substr(jsStr.indexOf('});') + 4));
|
|
|
168
|
+
|
|
|
169
|
+ return gulp.src(['js/sea.js', distDir.js + '/libs.js'])
|
|
|
170
|
+ .pipe(concat('lib.js'))
|
|
|
171
|
+ .pipe(uglify())
|
|
|
172
|
+ .pipe(gulp.dest(distDir.js));
|
|
|
173
|
+});
|
|
|
174
|
+
|
|
|
175
|
+gulp.task('clear-libs', ['concat-min-libs'], function() {
|
|
|
176
|
+ fs.renameSync('package.bak.json', 'package.json');
|
|
|
177
|
+ fs.unlinkSync('./libs.js');
|
|
|
178
|
+ fs.unlinkSync(distDir.js + '/libs.js');
|
|
|
179
|
+});
|
|
|
180
|
+
|
|
|
181
|
+gulp.task('business', ['libs'], function() {
|
|
|
182
|
+ var pkg = new Package(__dirname);
|
|
|
183
|
+
|
|
|
184
|
+ return gulp.src(pkg.main)
|
|
|
185
|
+ .pipe(transport({
|
|
|
186
|
+ pkg: pkg
|
|
|
187
|
+ }))
|
|
|
188
|
+ .pipe(concat('index-debug.js'))
|
|
|
189
|
+ .pipe(gulp.dest(distDir.js))
|
|
|
190
|
+ .pipe(uglify())
|
|
|
191
|
+ .pipe(concat('index.js'))
|
|
|
192
|
+ .pipe(gulp.dest(distDir.js));
|
|
|
193
|
+});
|
|
|
194
|
+
|
|
|
195
|
+//页面需要直接标签引用的JS
|
|
|
196
|
+gulp.task('script', function() {
|
|
|
197
|
+ gulp.src('plugin/page-use/*')
|
|
|
198
|
+ .pipe(gulp.dest(rootDist));
|
|
|
199
|
+}); |