forked from buiteam/bui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
203 lines (179 loc) · 5.29 KB
/
gulpfile.js
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
'use strict';
var gulp = require('gulp');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var less = require('gulp-less');
var rename = require('gulp-rename');
var through = require('through2');
var path = require('path');
var exec = require('child_process').exec;
var replace = require('gulp-replace');
var uglify = require('gulp-uglify');
var shell = require('gulp-shell')
var fs = require('fs');
var distDir = "./dist";
var dependencies = require('./package.json').spm.dependencies;
// 获取包文件的路径
function getPackagePath(name) {
var path = distDir+'/' + name + '/';
var subDirs = fs.readdirSync(path);
var version = subDirs[subDirs.length -1];
return path + version + '/';
}
/**
* 重命名包文件的入口文件,因为入口文件有可能为index.js
* 统一为{package}.js
*/
function renameFile() {
var stream = through.obj(function(file, enc, callback) {
var filepath = file.path.split(path.sep),
module = filepath.slice(filepath.lastIndexOf('dist') + 1), // => [bui-{package}, version, [path,] {main.js}]
packageName = module[0].split('-')[1],
filename;
if (module.length === 3) {
filename = packageName + '.js';
}
else {
filename = module.slice(2).join(path.sep).replace(/-debug.js$/, '.js');
}
file.path = path.join(file.base, filename);
return callback(null, file);
});
return stream;
}
//清理目录
gulp.task('clean', function() {
return gulp.src([
'./build'
// './spm_modules',
// distDir
], {read: false})
.pipe(clean());
});
//获取依赖的package
gulp.task('prepare', ['clean'], function(cb){
exec('./node_modules/spm/bin/spm install && ./node_modules/spm/bin/spm build --with-deps && cd ./spm_modules/bui-config/*\.*\.* && ../../../node_modules/spm/bin/spm build -O ../../../dist --with-deps --include standalone', function (err, stdout, stderr) {
cb(stderr);
});
/*return gulp.src([
'./spm_modules'
], {read: false})
.pipe(shell([
'cd <%=file.path %> && spm install && spm build -O ../../../dist'
]))
*/
});
//生成依赖包的js文件
gulp.task('package', function(){
var files = [];
for(var name in dependencies){
if(name != 'seajs'){
files.push(getPackagePath(name, dependencies[name]) + '**/*-debug.js');
}
}
return gulp.src(files)
// 重命名包文件的js名
.pipe(renameFile())
// 去掉版本号和包名
.pipe(replace(/bui-(\w+)\/\d.\d.\d\/\w+/g, 'bui/$1'))
// require.async的时候要把bui-xxx换成bui/xxx
.pipe(replace(/require\.async\((\'|\")bui-/g, 'require.async($1bui/'))
// 去掉-debug的后缀
.pipe(replace(/-debug/g, ''))
.pipe(gulp.dest('./build'));
});
gulp.task('seed.js', ['package'], function() {
var tmp_distDir = './spm_modules';
return gulp.src([
tmp_distDir + '/seajs/' + dependencies.seajs + '/dist/sea-debug.js',
'./build/config.js',
'./build/i18n.js',
'./build/common.js'
])
.pipe(concat('seed.js'))
.pipe(gulp.dest('./build'));
});
gulp.task('bui.js', ['package'], function() {
var tmp_distDir = './spm_modules';
return gulp.src([
tmp_distDir + '/seajs/' + dependencies.seajs + '/dist/sea-debug.js',
'./build/config.js',
'./build/i18n.js',
'./build/common.js',
'./build/data.js',
'./build/list.js',
'./build/menu.js',
'./build/tab.js',
'./build/mask.js',
'./build/overlay.js',
'./build/picker.js',
'./build/toolbar.js',
'./build/calendar.js',
'./build/select.js',
'./build/form.js',
'./build/editor.js',
'./build/tooltip.js',
'./build/grid.js',
'all.js'
])
.pipe(concat('bui.js'))
.pipe(gulp.dest('./build'));
});
// 适配kissy的js
gulp.task('adapter.js', ['package'], function() {
var tmp_distDir = './spm_modules';
return gulp.src([
tmp_distDir + '/bui-adapter/' + dependencies['bui-adapter'] + '/dist/adapter-debug.js',
])
.pipe(rename(function (path) {
var basename = path.basename;
path.basename = basename.replace(/-debug$/, '');
}))
.pipe(gulp.dest('./build'));
});
gulp.task('script', ['seed.js', 'bui.js', 'adapter.js'/**/], function() {
return gulp.src([
'./build/**/*.js'
])
.pipe(uglify({
output: {
ascii_only: true
}
}))
.pipe(rename({suffix: '-min'}))
.pipe(gulp.dest('./build'))
});
gulp.task('css', ['package'], function() {
// gulp.src([
// ])
// .pipe('')
var files = [];
for(var name in dependencies){
files.push(getPackagePath(name, dependencies[name]) + '**/*.css');
}
return gulp.src(files)
.pipe(rename(function(path){
var basename = path.basename;
if(!(/-debug$/.test(basename))){
path.basename = basename + '-min';
}
}))
.pipe(rename(function(path){
path.basename = path.basename.replace(/-debug$/, '');
}))
.pipe(gulp.dest('./build'));
});
// 图片以及一些其他静态资源
gulp.task('assets', function() {
var files = [
'!**/*.js','!**/*.css'
];
for(var name in dependencies){
files.push(getPackagePath(name, dependencies[name]) + '**/*.*');
}
return gulp.src(files)
.pipe(gulp.dest('./build'))
});
gulp.task('default', ['clean'], function() {
return gulp.start('package', 'script', 'css', 'assets');
});