forked from leecade/echarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
99 lines (86 loc) · 3.22 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
var Buffer = require('buffer').Buffer
var path = require('path')
var gulp = require('gulp')
var shell = require('gulp-shell')
var beautify = require('gulp-beautify')
var replace = require('gulp-replace')
var clean = require('gulp-clean')
var merge = require('merge-stream')
var through = require('through2')
var parse = require('nodefy').parse
var gutil = require('gulp-util')
var nodefy = function () {
var transform = function (file, enc, cb) {
if (file.isNull()) return cb(null, file)
if (file.isStream()) return cb(new gutil.PluginError('gulp-nodefy', 'Streaming not supported'))
var data
var str = file.contents.toString('utf8')
// fixes: require('./config') => require('./config.js')
str = str
.replace(/(require\(\[?['"]((?!\.js)[^'"])*)/g, '$1.js')
// fixes:
// require(['./geoJson/china_geo.js'], function (md) {
// callback(decode(md));
// });
.replace(/require(\(\[[^,]*,[\s\r\n]*function)/g, 'require.async$1')
// fixes: define({}) => module.exports = {}
if(/define\([\s\r\n]*{/.test(str))
str = str
.replace(/define\([\s\r\n]*{/, 'define(function() {return {')
.replace(/\);?[\s\r\n]*$/, '});')
try {
data = parse(str)
} catch (err) {
return cb(new gutil.PluginError('gulp-nodefy', err))
}
file.contents = new Buffer(data)
cb(null, file)
}
return through.obj(transform)
}
gulp.task('clean', function() {
return gulp.src(['./amd', './src', './build'])
.pipe(clean())
})
gulp.task('update', ['clean'], function() {
return gulp.src('./')
.pipe(shell([
'bower install ecomfe/zrender',
'bower install ecomfe/echarts',
'cp -r bower_components/echarts/src ./amd',
'cp -r bower_components/zrender/src ./amd/zrender',
'cp -r bower_components/echarts/build ./'
]))
})
gulp.task('migrate', ['update'], function() {
return merge(
gulp.src('./amd/echarts.js')
.pipe(replace('require(\'zrender\'', 'require(\'./zrender/zrender\''))
.pipe(gulp.dest('./amd/')),
gulp.src('./amd/*.js')
.pipe(replace('require(\'zrender/', 'require(\'./zrender/'))
.pipe(gulp.dest('./amd/')),
gulp.src('./amd/chart/*.js')
.pipe(replace('require(\'zrender', 'require(\'../zrender'))
.pipe(gulp.dest('./amd/chart/')),
gulp.src('./amd/component/*.js')
.pipe(replace('require(\'zrender', 'require(\'../zrender'))
.pipe(gulp.dest('./amd/component/')),
gulp.src('./amd/util/*.js')
.pipe(replace('require(\'zrender', 'require(\'../zrender'))
.pipe(gulp.dest('./amd/util/')),
gulp.src('./amd/util/shape/*.js')
.pipe(replace('require(\'zrender', 'require(\'../../zrender'))
.pipe(gulp.dest('./amd/util/shape/')),
gulp.src('./amd/util/projection/*.js')
.pipe(replace('require(\'zrender', 'require(\'../../zrender'))
.pipe(gulp.dest('./amd/util/projection/'))
)
})
gulp.task('build', ['migrate'], function() {
gulp.src('./amd/**/*.js')
.pipe(nodefy())
.pipe(beautify())
.pipe(gulp.dest('./src'))
})
gulp.task('default', ['build'])