-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgulpfile.js
80 lines (70 loc) · 2.24 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
'use strict';
const gulp = require('gulp'),
pkg = require('./package.json'),
rollup = require('rollup'),
connect = require('gulp-connect'),
BundleHelper = require('maptalks-build-helpers').BundleHelper;
const bundleHelper = new BundleHelper(pkg);
// https://github.com/mrdoob/three.js/blob/dev/rollup.config.js#L1
function glsl() {
return {
transform(code, id) {
if (/\.glsl$/.test(id) === false) return undefined;
const transformedCode = 'export default ' + JSON.stringify(
code
.replace(/[ \t]*\/\/.*\n/g, '') // remove //
.replace(/[ \t]*\/\*[\s\S]*?\*\//g, '') // remove /* */
.replace(/\n{2,}/g, '\n') // # \n+ to \n
) + ';';
return {
code: transformedCode,
map: {
mappings: ''
}
};
}
};
}
gulp.task('build', () => {
const config = bundleHelper.getDefaultRollupConfig();
config.plugins.unshift(glsl());
return bundleHelper.bundle('src/index.js', config);
});
gulp.task('minify', ['build'], () => {
bundleHelper.minify();
});
gulp.task('watch', () => {
const config = bundleHelper.getDefaultRollupConfig();
config.plugins.unshift(glsl());
config.input = 'src/index.js';
const year = new Date().getFullYear();
const banner = `/*!\n * ${pkg.name} v${pkg.version}\n * LICENSE : ${pkg.license}\n * (c) 2016-${year} maptalks.org\n */`;
config.banner = banner;
config.output = [{
file: `dist/${pkg.name}.js`,
format: 'umd',
name: 'maptalks',
extend: true
}];
const watcher = rollup.watch(config);
watcher.on('event', e => {
if (e.code === 'START') {
console.log('[ROLLUP] Starting...');
console.time('[ROLLUP]');
} else if (e.code === 'END') {
console.timeEnd('[ROLLUP]');
gulp.src('./dist/*.js')
.pipe(connect.reload());
} else if (e.code === 'ERROR') {
console.error(e);
}
});
});
gulp.task('connect', ['watch'], () => {
connect.server({
root: ['.'],
livereload: true,
port: 20001
});
});
gulp.task('default', ['connect']);