-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
198 lines (151 loc) · 5.77 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
var gulp = require('gulp'),
argv = require('yargs').argv,
path = require('path'),
setting = require('./settings.js'),
notify = require('gulp-notify'),//提示信息
clean = require('gulp-clean'),//清理文件
runSequence = require('run-sequence');
var moduleGenerator = require('./buildSrc/src/ModuleGenerator.js');
var generator = new moduleGenerator(setting.modules);
var gulpif = require('gulp-if');
var gulpLoadPlugins = require('gulp-load-plugins');
var plugins = gulpLoadPlugins();
var Server = require('karma').server;
gulp.task('help', function () {
console.log(' gulp build 文件打包');
console.log(' gulp test 运行测试');
console.log(' gulp help 参数说明');
console.log(' gulp -p 生产环境(默认)');
console.log(' gulp -d 开发环境');
//console.log(' gulp -m <module> 模块打包(默认:all)')
});
gulp.task('default', ['test'], function () {
});
gulp.task('build', function (cb) {
var option = {
evr: argv.p || !argv.d
};
var modules = generator.getAllModules();
var executor = new TaskExecutor(modules,option);
var list = executor.execute();
runSequence(list, cb);
});
gulp.task('test', function (done) {
var option = {
evr: argv.p || !argv.d
};
var modules = generator.getAllModules();
if(null === modules || modules.length <=0 ){
return;
}
var files = [];
var buildType = option.evr?'build/production':'build/develop';
for(var i=0; i<modules.length; i++){
var dest = [];
var sources = modules[i].src.concat(modules[i].test);
var tmpPath = path.join(modules[i].dest, buildType, '*.js');
dest.push(tmpPath);
files = files.concat(sources, dest);
}
Server.start({
configFile: __dirname + '/karma.conf.js',
singleRun: true,
files: files
}, done);
});
//****************************************
function GulpTasks(){
var _getDesModuleSrc = function(module, isPod) {
var sources = [];
var moduleDeps = module.dependencies;
var buildType = isPod ? module.production : module.develop;
if (null != moduleDeps && moduleDeps.length > 0) {
for (var i = 0; i < moduleDeps.length; i++) {
var tmpPath = path.join(moduleDeps[i].dest, buildType, moduleDeps[i].name + '*.js');
sources.push(tmpPath);
}
}
return sources;
};
var _gclean = function (module,isPod) {
var buildType = isPod ? module.production : module.develop;
var cleanPath = path.join(module.name, buildType);
console.log(module.name + ' clean task begin');
var stream = gulp.src([cleanPath], {read: false})
.pipe(clean())
.pipe(notify({message: module.name + ' clean task ok'}));
console.log(module.name + ' clean task ok')
return stream;
};
var _gbuild = function () {
var module = arguments[0], isPod = arguments[1];
var sources = _getDesModuleSrc(module, isPod);
var buildType = isPod ? module.production : module.develop;
var dest = path.join(module.name, buildType);
var time = Date.now();
console.log(module.name + ' build task begin');
var stream = gulp.src(sources.concat(module.src))
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('default'))
//只对最后发布的包(jiShiTracker)进行压缩
.pipe(gulpif(module.name === 'jishiTracker', plugins.uglify()))
.pipe(plugins.concat(module.name + time + '.js'))
.pipe(gulp.dest(dest))
.pipe(notify({message: module.name + ' build task ok'}));
console.log(module.name + ' build task ok')
return stream;
};
return {
gclean: _gclean,
gbuild:_gbuild,
}
}
function TaskExecutor(parts, options) {
var _execute = function(){
var gulpTasks = new GulpTasks();
var list = [];
for (var i = 0; i < parts.length; i++) {
(function (key) {
var module = key;
var des = module.dependencies;
var cleanTaskName = module.name + '_clean';
var buildTaskName = module.name + '_build';
var testTaskName = module.name + '_test';
var cleanDesTasks = [];
var buildDesTasks = [];
var testDesTasks = [];
if (null != des && des.length > 0) {
for (var i = 0; i < des.length; i++) {
var tmpClean = des[0].name + '_clean';
cleanDesTasks.push(tmpClean);
var tmpBuild = des[0].name + '_build';
buildDesTasks.push(tmpBuild);
var tmpTest = des[0].name + '_test';
testDesTasks.push(tmpTest);
}
}
/* clean */
gulp.task(cleanTaskName, cleanDesTasks, function () {
return gulpTasks.gclean(module, options.evr);
});
/* build */
gulp.task(buildTaskName, buildDesTasks, function () {
return gulpTasks.gbuild(module, options.evr);
});
/* 模块任务 */
var moduleTask = module.name + 'Task';
gulp.task(moduleTask, function (cb) {
runSequence(
[cleanTaskName, buildTaskName],
cb
);
});
})(parts[i]);
list.push(parts[i].name + 'Task');
}
return list;
};
return{
execute: _execute,
}
}