forked from TerriaJS/terriajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
206 lines (164 loc) · 6.44 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
"use strict";
/*global require*/
var fs = require('fs');
var glob = require('glob-all');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var jsdoc = require('gulp-jsdoc');
var uglify = require('gulp-uglify');
var jasmine = require('gulp-jasmine');
var exec = require('child_process').exec;
var sourcemaps = require('gulp-sourcemaps');
var exorcist = require('exorcist');
var buffer = require('vinyl-buffer');
var transform = require('vinyl-transform');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
var concatCss = require('gulp-concat-css');
var appJSName = 'ausglobe.js';
var specJSName = 'ausglobe-specs.js';
var appEntryJSName = './src/main.js';
var workerGlob = [
'./third_party/cesium/Source/Workers/*.js',
'!./third_party/cesium/Source/Workers/*.profile.js',
'!./third_party/cesium/Source/Workers/cesiumWorkerBootstrapper.js',
'!./third_party/cesium/Source/Workers/transferTypedArrayTest.js',
'!./third_party/cesium/Source/Workers/createTaskProcessorWorker.js'
];
var specGlob = './spec/**/*.js';
// Create the build directory, because browserify flips out if the directory that might
// contain an existing source map doesn't exist.
if (!fs.existsSync('public/build')) {
fs.mkdirSync('public/build');
}
gulp.task('build-app', ['prepare-cesium'], function() {
return build(appJSName, appEntryJSName, false);
});
gulp.task('build-specs', ['prepare-cesium'], function() {
return build(specJSName, glob.sync(specGlob), false);
});
gulp.task('build', ['build-app', 'build-specs']);
gulp.task('release-app', ['prepare-cesium'], function() {
return build(appJSName, appEntryJSName, true);
});
gulp.task('release-specs', ['prepare-cesium'], function() {
return build(specJSName, glob.sync(specGlob), true);
});
gulp.task('release', ['release-app', 'release-specs']);
gulp.task('watch-app', ['prepare-cesium'], function() {
return watch(appJSName, appEntryJSName, false);
});
gulp.task('watch-specs', ['prepare-cesium'], function() {
return watch(specJSName, glob.sync(specGlob), false);
});
gulp.task('watch', ['watch-app', 'watch-specs']);
gulp.task('lint', function(){
return gulp.src(['src/**/*.js', 'spec/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
});
gulp.task('docs', function(){
return gulp.src('src/**/*.js')
.pipe(jsdoc('./public/doc', undefined, {
plugins : ['plugins/markdown']
}));
});
gulp.task('prepare-cesium', ['build-cesium', 'copy-cesium-assets', 'copy-cesiumWorkerBootstrapper', 'concat-css']);
gulp.task('build-cesium', function(cb) {
return exec('"Tools/apache-ant-1.8.2/bin/ant" build', {
cwd : 'third_party/cesium'
}, function(err, stdout, stderr) {
if (stderr) {
console.log('Error while building Cesium: ');
console.log(stderr);
}
cb(err);
});
});
gulp.task('copy-cesium-assets', function() {
return gulp.src([
'third_party/cesium/Source/Workers/transferTypedArrayTest.js',
'third_party/cesium/Source/ThirdParty/Workers/**',
'third_party/cesium/Source/Assets/**',
'third_party/cesium/Source/Widgets/**/*.css',
'third_party/cesium/Source/Widgets/Images/**'
], { base: 'third_party/cesium/Source' })
.pipe(gulp.dest('public/build/Cesium/'));
});
gulp.task('copy-cesiumWorkerBootstrapper', function() {
return gulp.src('src/cesiumWorkerBootstrapper.js')
.pipe(gulp.dest('public/build/Cesium/Workers'));
});
gulp.task('concat-css', function() {
return gulp.src( ['public/build/Cesium/Widgets/widgets.css'], {base: 'Widgets'})
.pipe(concatCss("build/Cesium/Widgets/cesiumwidgetsbundle.css"))
.pipe(gulp.dest('public/'));
});
gulp.task('default', ['lint', 'build']);
function bundle(name, bundler, minify, catchErrors) {
requireWebWorkers(bundler);
// Combine main.js and its dependencies into a single file.
// The poorly-named "debug: true" causes Browserify to generate a source map.
var result = bundler.bundle({
debug: true
});
if (catchErrors) {
// Display errors to the user, and don't let them propagate.
result = result.on('error', function(e) {
gutil.log('Browserify Error', e);
});
}
result = result
.pipe(source(name))
.pipe(buffer());
if (minify) {
// Minify the combined source.
// sourcemaps.init/write maintains a working source map after minification.
// "preserveComments: 'some'" preserves JSDoc-style comments tagged with @license or @preserve.
result = result
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify({preserveComments: 'some', mangle: true, compress: true}))
.pipe(sourcemaps.write());
}
result = result
// Extract the embedded source map to a separate file.
.pipe(transform(function () { return exorcist('public/build/' + name + '.map'); }))
// Write the finished product.
.pipe(gulp.dest('public/build'));
return result;
}
function build(name, files, minify) {
return bundle(name, browserify(files).transform('deamdify'), minify, false);
}
function watch(name, files, minify) {
var bundler = watchify(files).transform('deamdify');
function rebundle() {
var start = new Date();
var result = bundle(name, bundler, minify, true);
result.on('end', function() {
console.log('Rebuilt ' + name + ' in ' + (new Date() - start) + ' milliseconds.');
});
return result;
}
bundler.on('update', rebundle);
return rebundle();
}
function requireWebWorkers(bundler) {
// Explicitly require the Cesium Web Workers, and expose them with the name the cesiumWorkerBootstrapper will use for them.
var workers = glob.sync(workerGlob);
for (var i = 0; i < workers.length; ++i) {
var workerFilename = workers[i];
var lastSlashIndex = workerFilename.lastIndexOf('/');
if (lastSlashIndex < 0) {
continue;
}
var exposeName = 'Workers/' + workerFilename.substring(lastSlashIndex + 1);
var dotJSIndex = exposeName.lastIndexOf('.js');
exposeName = exposeName.substring(0, dotJSIndex);
bundler.require(workerFilename, { expose : exposeName });
}
}