forked from xagriff/cesium-sensors
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgulpfile.js
175 lines (153 loc) · 4.07 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
'use strict';
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var through = require('through2');
var del = require('del');
var xo = require('gulp-xo');
var rollup = require('rollup');
var { string } = require('rollup-plugin-string');
var terser = require('@rollup/plugin-terser');
var glslStripComments = require('glsl-strip-comments');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
function runLint(src) {
return gulp.src(src)
.pipe(xo());
}
function lint() {
return runLint(['lib/**/*.js', 'gulp/**/*.js', 'gulpfile.js']);
}
exports.lint = lint;
function clean() {
return del(['coverage', '.tmp', 'dist']);
}
exports.clean = clean;
function preprocessShaders() {
return gulp.src('lib/**/*.glsl')
.pipe(through.obj(function(file, _, cb) {
if (file.isBuffer()) {
const output = glslStripComments(file.contents.toString(), { version: '300 es' });
file.contents = Buffer.from(output);
}
cb(null, file);
}))
.pipe(gulp.dest('.tmp'));
}
function preprocessJs() {
return gulp.src(['lib/**/*.js'])
.pipe(gulp.dest('.tmp'));
}
function getCopyrightHeaders() {
return fs.readFileSync('lib/copyright-header.js').toString();
}
async function buildEs() {
const bundle = await rollup.rollup({
input: '.tmp/cesium-sensor-volumes.js',
plugins: [
string({
include: '**/*.glsl'
})
],
external: id => /Cesium/.test(id)
});
await bundle.write({
file: 'dist/cesium-sensor-volumes.es.js',
format: 'es',
banner: getCopyrightHeaders()
});
await bundle.write({
file: 'dist/cesium-sensor-volumes.es.min.js',
format: 'es',
plugins: [terser({
format: {
comments: function(node, comment) {
if (comment.type === 'comment2') {
return /Copyright/i.test(comment.value);
}
}
}
})],
banner: getCopyrightHeaders()
});
}
exports.buildEs = gulp.series(clean, gulp.parallel(preprocessShaders, preprocessJs), buildEs);
function generateShims() {
// Search for Cesium modules and add shim modules that pull from the Cesium global
return gulp.src(['./dist/cesium-sensor-volumes.es.js'])
.pipe(through.obj(function(file, _, cb) {
if (file.isBuffer()) {
var cesiumRequireRegex = /import (\w*) from 'Cesium\/\w*\/(\w*)'/;
const output = file.contents.toString().split('\n').map(line => {
const match = cesiumRequireRegex.exec(line);
if (match) {
return `const ${match[1]} = Cesium['${match[2]}'];`;
}
return line;
});
file.contents = Buffer.from(output.join('\n'));
}
cb(null, file);
}))
.pipe(gulp.dest('.tmp/shimmed'));
}
async function buildUmd() {
const bundle = await rollup.rollup({
input: '.tmp/shimmed/cesium-sensor-volumes.es.js'
});
await bundle.write({
file: 'dist/cesium-sensor-volumes.js',
name: 'CesiumSensorVolumes',
format: 'umd'
});
await bundle.write({
file: 'dist/cesium-sensor-volumes.min.js',
name: 'CesiumSensorVolumes',
plugins: [terser({
format: {
comments: function(node, comment) {
if (comment.type === 'comment2') {
return /Copyright/i.test(comment.value);
}
}
}
})],
format: 'umd'
});
}
exports.build = gulp.series(exports.buildEs, generateShims, buildUmd);
exports.buildReload = gulp.series(exports.build, reload);
function run(cb) {
browserSync.init({
server: '.'
}, cb);
}
function watch(cb) {
gulp.watch(['examples/**/*.html', 'examples/**/*.czml'], reload);
gulp.watch(['lib/**/*.glsl'], exports.buildReload);
gulp.watch(['lib/**/*.js'], exports.buildReload);
cb();
}
exports.serve = gulp.series(exports.build, run, watch);
function lintTest() {
return runLint(['test/**/*.js']);
}
function test(done, options) {
var Server = require('karma').Server;
var server = new Server(Object.assign({
configFile: path.join(__dirname, '/test/karma.conf.js'),
singleRun: true
}, options), done);
server.start();
}
exports.test = gulp.series(lintTest, test);
function testCI(done) {
test(done, {
browsers: ['Electron'],
client: {
args: [true]
}
});
}
exports.testCI = gulp.series(lintTest, testCI);
exports.ci = gulp.series(lint, testCI, exports.build);