forked from hiloteam/Hilo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
285 lines (241 loc) · 8.47 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
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
var gulp = require('gulp');
var del = require('del');
var merge = require('merge-stream');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var gulpif = require('gulp-if');
var replace = require('gulp-replace');
var header = require('gulp-header');
var footer = require('gulp-footer');
var zip = require('gulp-zip');
var shell = require('gulp-shell');
var uitest = require('gulp-uitest');
var transformModule = require('gulp-transform-module');
var jshint = require('gulp-jshint');
var pkg = require('./package.json');
var child_process = require('child_process');
var isWatch = false;
var licenseCommentReg = /\/\*\*[\d\D]+?alibaba.com[\d\D]+?Licensed under the MIT License[\s]+?\*\//g;
var apiCommentReg = /(\/\*\*[\s]+?\*[\s]+?@language=en[\d\D]+?\*\/)[\s]+(\/\*\*[\s]+?\*[\s]+?@language=zh[\d\D]+?\*\/)/g;
//format
var getFileInfo = function(variant){
variant = variant ? (' for ' + variant) : '';
var info = '/**\n';
info += ' * ' + pkg.name + ' ' + pkg.version + variant + '\n';
info += ' * Copyright 2016 alibaba.com\n';
info += ' * Licensed under the MIT License\n';
info += ' */\n';
return info;
};
var createBuildFormatTask = function(type){
var cleanTask = type + '-clean';
var formatTask = type + '-format';
var uglifyTask = type + '-uglify';
var zipTask = type + '-zip';
var destPath = 'build/' + type;
var notConcat = ['commonjs'].indexOf(type) > -1;
//clean dir
gulp.task(cleanTask, function(cb){
del(destPath, cb);
});
//format
gulp.task(formatTask, [cleanTask], function(){
var stream = gulp.src(pkg.sources.files, {
cwd:pkg.sources.dir,
base:pkg.sources.dir
})
.pipe(gulpif(function(file){
return file.path.indexOf('Hilo.js') > -1;
}, replace('{{$version}}', pkg.version)))
.pipe(replace(licenseCommentReg, ''))
.pipe(replace(apiCommentReg, '$1'))
.pipe(transformModule(type))
.pipe(header(getFileInfo(type)))
if(notConcat){
return stream.pipe(gulp.dest(destPath))
}
else{
return stream.pipe(gulp.dest(destPath + '/hilo'))
.pipe(concat('hilo-' + type + '.js'))
.pipe(gulp.dest(destPath));
}
});
//uglify
gulp.task(uglifyTask, [formatTask], function(){
return gulp.src(destPath + '/**/*.js')
.pipe(gulpif(!isWatch, uglify()))
.pipe(header(getFileInfo(type)))
.pipe(rename({extname:".min.js"}))
.pipe(gulp.dest(destPath))
});
//zip
gulp.task(zipTask, [uglifyTask], function(){
return gulp.src(destPath + '/**/*.js')
.pipe(gulpif(!isWatch, zip('hilo-' + type + '.zip')))
.pipe(gulp.dest(destPath));
});
gulp.task(type, [isWatch?uglifyTask:zipTask]);
}
var moduleTypes = ['standalone', 'amd', 'commonjs', 'kissy', 'cmd'];
transformModule.add('standalone', function(metadata){
var head = '(function(window){\nif(!window.Hilo) window.Hilo = {};\n';
var tail = '\n})(window);';
var module = metadata.moduleName;
var requireModules = metadata.requireClasses;
requireModules.forEach(function(requireModule){
if(requireModule !== 'Hilo'){
head += 'var ' + requireModule + ' = window.Hilo.' + requireModule + ';\n';
}
else{
head += 'var Hilo = window.Hilo;'
}
});
if(module === 'Hilo'){
tail = '\nfor(var i in Hilo){window.Hilo[i] = Hilo[i];}' + tail;
}
else{
tail = '\nwindow.Hilo.' + module + ' = ' + module + ';' + tail;
}
return {
head:head,
tail:tail
};
});
moduleTypes.forEach(function(moduleType){
createBuildFormatTask(moduleType);
});
gulp.task('format', moduleTypes);
//flash
gulp.task('flash-clean', function(cb){
del('build/flash', cb);
});
gulp.task('flash', ['flash-clean'], function(){
var js = gulp.src(['src/flash/FlashRenderer.js', 'src/flash/FlashAdaptor.js'])
.pipe(concat('hilo-flash.js'))
.pipe(header('(function(){\n'))
.pipe(header(getFileInfo('flashAdaptor')))
.pipe(footer('})();\n'))
.pipe(gulp.dest('build/flash'))
.pipe(gulpif(!isWatch, uglify()))
.pipe(rename({extname:'.min.js'}))
.pipe(header(getFileInfo('flashAdaptor')))
.pipe(gulp.dest('build/flash'));
var swf = gulp.src(['src/flash/as/bin/hilo.swf'])
.pipe(gulp.dest('build/flash/'));
return merge(js, swf);
});
//extensions
gulp.task('extensions', function(){
var streams = merge();
for(var extensionName in pkg.extensions){
var extensionPathData = pkg.extensions[extensionName];
var src = extensionPathData.files.map(function(src, i, arr){
return 'src/extensions/' + extensionPathData.dir + src;
});
var dest = 'build/' + extensionName;
var stream = gulp.src(src)
.pipe(concat(extensionName + '.js'))
.pipe(header(getFileInfo(extensionName)))
.pipe(gulp.dest(dest))
.pipe(gulpif(!isWatch, uglify()))
.pipe(rename({extname:'.min.js'}))
.pipe(header(getFileInfo(extensionName)))
.pipe(gulp.dest(dest));
streams.add(stream);
}
return streams;
});
//docs
var createDocTask = function(languages){
languages.forEach(function(language, i){
var cleanTask = 'doc-clean-' + language;
var commentTask = 'doc-comment-' + language;
var buildTask = 'doc-' + language;
var codeSrc = 'docs/api-' + language + '/code/';
gulp.task(cleanTask, function(cb){
del('docs/api-' + language, cb);
});
gulp.task(commentTask, [cleanTask], function(){
return gulp.src(pkg.sources.files, {
cwd:pkg.sources.dir,
base:pkg.sources.dir
})
.pipe(replace(apiCommentReg, '$' + (i+1)))
.pipe(replace(/\*[\s]*@language=[a-z]+[\s]*/g, ''))
.pipe(gulp.dest(codeSrc));
});
gulp.task(buildTask, [commentTask], (function(){
var files = pkg.sources.files.map(function(src){
return codeSrc + src;
});
var cmd = 'java ' + [
'-jar',
'tools/jsdoc-toolkit-2.4.0/jsrun.jar',
'tools/jsdoc-toolkit-2.4.0/app/run.js',
codeSrc,
'-d=docs/api-' + language + '/',
'-t=docs/api_template/',
'-r=5',
'-x=js',
// '-q',
'-E=FilesLoad.js',
'-D="ver:' + pkg.version + '"',
'-lang=' + language
].join(' ');
return shell.task(cmd);
})());
});
};
createDocTask(['en', 'zh']);
gulp.task('doc', ['doc-en', 'doc-zh']);
//watch
gulp.task('setIsWatch', function(cb){
isWatch = true;
cb();
});
gulp.task('watch', ['setIsWatch', 'standalone', 'flash', 'extensions'], function(){
gulp.watch('src/**/*.js', ['standalone', 'flash']);
gulp.watch('src/extensions/**/*.js', ['extensions']);
});
gulp.task('npm:clean', function(done){
del('build/npm', done);
});
gulp.task('npm:build', ['npm:clean', 'standalone', 'commonjs'], function(){
var standaloneStream = gulp.src('build/standalone/hilo-standalone.js')
.pipe(footer(`
if(typeof module !== 'undefined' && module.exports){
module.exports = window.Hilo;
}
`))
.pipe(gulp.dest('build/npm'));
var packageStream = gulp.src(['package.json', '.npmignore', 'LICENSE', 'README.md', 'd.ts/hilo.d.ts', 'build/commonjs/**/*'])
.pipe(replace('"name": "Hilo"', '"name": "hilojs"'))
.pipe(gulp.dest('build/npm'));
return merge(standaloneStream, packageStream);
});
gulp.task('npm', ['npm:build'], function(done){
child_process.exec('npm publish build/npm', (error, stdout, stderr) => {
console.log(error, stdout, stderr);
done();
});
});
gulp.task('jshint', ['setIsWatch', 'standalone'], function(){
return gulp.src(['build/standalone/hilo/**/*.js', '!build/standalone/hilo/**/*.min.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'))
});
//test
gulp.task('test', ['jshint'], function () {
return gulp
.src('test/html/index.html')
.pipe(uitest({
width: 600,
height: 480,
hidpi: false,
useContentSize: true,
show: false
}));
});
gulp.task('default', ['format', 'flash']);