-
Notifications
You must be signed in to change notification settings - Fork 506
/
gulpfile.js
222 lines (202 loc) · 6.17 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
const minimist = require('minimist'),
fs = require('fs'),
path = require('path'),
gulp = require('gulp'),
del = require('del'),
zip = require('gulp-zip'),
concat = require('gulp-concat'),
cssnano = require('gulp-cssnano'),
connect = require('gulp-connect'),
uglify = require('uglify-js').minify,
zlib = require('zlib'),
Server = require('karma').Server,
package = require('./package.json');
const knownOptions = {
string: ['browsers', 'pattern'],
boolean: 'coverage',
alias: {
'coverage': 'cov'
},
default: {
browsers: null,
coverage: false
}
};
const options = minimist(process.argv.slice(2), knownOptions);
const browsers = [];
const configBrowsers = options.browsers || process.env['MAPTALKS_BROWSERS'] || '';
configBrowsers.split(',').forEach(name => {
if (!name || name.length < 1) {
return;
}
browsers.push(name);
});
const stylesPattern = './assets/css/**/*.css';
gulp.task('images', () => {
return gulp.src('./assets/images/**/*')
.pipe(gulp.dest('./dist/images'));
});
gulp.task('styles', gulp.series(['images'], () => {
return gulp.src(stylesPattern)
.pipe(concat('maptalks.css'))
.pipe(cssnano())
.pipe(gulp.dest('./dist/'));
}));
gulp.task('transform-proto', done => {
//a silly work round as transform proto-to-assign is not working in babel 7
const filename = __dirname + '/dist/maptalks.js';
const code = fs.readFileSync(filename).toString('utf-8');
const transformed = code.replace('subClass.__proto__ = superClass;',
"if (typeof document !== 'undefined' && document.documentMode < 11) { _defaults(subClass, superClass); } else { subClass.__proto__ = superClass;}"
);
// fs.writeFileSync(__dirname + '/dist/compiled.js', transformed);
fs.writeFileSync(filename, transformed);
done();
});
gulp.task('minify', done => {
const name = package.name;
const dest = package.main;
const code = fs.readFileSync(dest).toString('utf-8');
const u = uglify(code, {
'output': {
'ascii_only': true,
'comments' : /^!/
}
});
const minified = u.code;
fs.writeFileSync('dist/' + name + '.min.js', minified);
const gzipped = zlib.gzipSync(minified);
fs.writeFileSync('dist/' + name + '.min.js.gz', gzipped);
done();
});
/**
* Run test once and exit
*/
gulp.task('test', done => {
// const karmaConfig = {
// configFile: path.join(__dirname, 'build/karma.test.config.js')
// };
let file = 'build/karma.test.config.js';
if (options.coverage) {
file = 'build/karma.cover.config.js';
}
const cfg = require('karma').config;
const karmaConfig = cfg.parseConfig(path.join(__dirname, file));
if (browsers.length > 0) {
karmaConfig.browsers = browsers;
}
if (configBrowsers.indexOf('IE') >= 0) {
let idx = -1;
for (let i = 0; i < karmaConfig.files.length; i++) {
if (karmaConfig.files[i].pattern.indexOf('test/core/ClassSpec.js') >= 0) {
idx = i;
break;
}
}
if (idx >= 0) {
karmaConfig.files.splice(idx, 1);
}
}
if (configBrowsers === 'IE9') {
//override IE9's pattern
options.pattern = 'IE9.Specs';
}
if (options.pattern) {
if (!karmaConfig.client) {
karmaConfig.client = {
mocha: {
grep: options.pattern
}
};
} else {
karmaConfig.client.mocha.grep = options.pattern;
}
}
new Server(karmaConfig, done).start();
});
/**
* Watch for file changes and re-run tests on each change
*/
gulp.task('tdd', done => {
const karmaConfig = {
configFile: path.join(__dirname, 'build/karma.tdd.config.js')
};
if (browsers.length > 0) {
karmaConfig.browsers = browsers;
}
if (options.pattern) {
karmaConfig.client = {
mocha: {
grep: options.pattern
}
};
}
if (options.pattern) {
if (!karmaConfig.client) {
karmaConfig.client = {
mocha: {
grep: options.pattern
}
};
} else {
karmaConfig.client.mocha.grep = options.pattern;
}
}
const karmaServer = new Server(karmaConfig, done);
karmaServer.start();
started = true;
});
gulp.task('connect', () => {
return connect.server({
root: ['dist', 'debug'],
livereload: true,
port: 20000
});
});
gulp.task('doc', done => {
const sources = require('./build/api-files.js');
del([
'./docs/api/1.x/**/*'
], {
force : true
});
const conf = require('./jsdoc.json');
const cmd = 'jsdoc';
const args = ['-c', 'jsdoc.json'].concat(['-P', 'package.json']).concat(sources);
const exec = require('child_process').exec;
exec([cmd].concat(args).join(' '), (error, stdout, stderr) => {
if (error) {
console.error('JSDoc returned with error: ' + stderr ? stderr : '');
done(error);
return;
}
if (stderr) {
console.error(stderr);
}
if (stdout) { console.log(stdout); }
console.log('Documented ' + sources.length + ' files in:');
console.log(conf.opts.destination);
done(stderr);
});
});
gulp.task('editdoc', gulp.series(['doc'], () => {
return gulp.watch(['src/**/*.js'], ['doc']);
}));
gulp.task('beforeZip', () => {
return gulp.src(['LICENSE', 'ACKNOWLEDGEMENT', 'docs/**/*'])
.pipe(gulp.dest('dist'))
});
gulp.task('zip', gulp.series(['beforeZip'], done => {
gulp.src(['dist/*.js', 'dist/*.mjs', 'dist/images/**/*', 'dist/maptalks.css', 'dist/LICENSE', 'dist/ACKNOWLEDGEMENT', 'dist/api/**/*'], { base: 'dist/' })
.pipe(zip('maptalks-' + package.version + '.zip'))
.pipe(gulp.dest('dist'));
setTimeout(function () {
del([
'dist/api/**/*', 'dist/LICENSE', 'dist/ACKNOWLEDGEMENT'
], {
force : true
});
done();
}, 2000);
}));
gulp.task('default', gulp.series(['connect']));