Skip to content

Commit

Permalink
Merge pull request #7140 from AnalyticalGraphicsInc/delay-load-gihugi…
Browse files Browse the repository at this point in the history
…c-file

Delay load google-earth-dbroot-parser
  • Loading branch information
ggetz authored Oct 16, 2018
2 parents e0e76ba + b7cae12 commit ae98c62
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
### 1.51 - 2018-11-01

##### Additions :tada:
* Shrink minified and gzipped Cesium.js by 27 KB (~3.7%) by delay loading seldom-used third-party dependencies. [#7140](https://github.com/AnalyticalGraphicsInc/cesium/pull/7140)
* Added WMS-T (time) support in WebMapServiceImageryProvider [#2581](https://github.com/AnalyticalGraphicsInc/cesium/issues/2581)
* Added `cutoutRectangle` to `ImageryLayer`, which allows cutting out rectangular areas in imagery layers to reveal underlying imagery. [#7056](https://github.com/AnalyticalGraphicsInc/cesium/pull/7056)

Expand Down
29 changes: 25 additions & 4 deletions Source/Core/GoogleEarthEnterpriseMetadata.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
define([
'../ThirdParty/google-earth-dbroot-parser',
'../ThirdParty/protobuf-minimal',
'../ThirdParty/when',
'./buildModuleUrl',
'./Check',
'./Credit',
'./defaultValue',
'./defined',
'./defineProperties',
'./GoogleEarthEnterpriseTileInformation',
'./isBitSet',
'./loadAndExecuteScript',
'./Math',
'./Request',
'./Resource',
'./RuntimeError',
'./TaskProcessor'
], function(
dbrootParser,
protobufMinimal,
when,
buildModuleUrl,
Check,
Credit,
defaultValue,
defined,
defineProperties,
GoogleEarthEnterpriseTileInformation,
isBitSet,
loadAndExecuteScript,
CesiumMath,
Request,
Resource,
Expand Down Expand Up @@ -488,6 +492,8 @@ define([
});
}

var dbrootParser;
var dbrootParserPromise;
function requestDbRoot(that) {
var resource = that._resource.getDerivedResource({
url: 'dbRoot.v5',
Expand All @@ -496,8 +502,23 @@ define([
}
});

return resource.fetchArrayBuffer()
.then(function(buf) {
if (!defined(dbrootParserPromise)) {
var url = buildModuleUrl('ThirdParty/google-earth-dbroot-parser.js');
var oldValue = window.cesiumGoogleEarthDbRootParser;
dbrootParserPromise = loadAndExecuteScript(url)
.then(function() {
dbrootParser = window.cesiumGoogleEarthDbRootParser(protobufMinimal);
if (defined(oldValue)) {
window.cesiumGoogleEarthDbRootParser = oldValue;
} else {
delete window.cesiumGoogleEarthDbRootParser;
}
});
}

return dbrootParserPromise.then(function() {
return resource.fetchArrayBuffer();
}).then(function(buf) {
var encryptedDbRootProto = dbrootParser.EncryptedDbRootProto.decode(new Uint8Array(buf));

var byteArray = encryptedDbRootProto.encryptionData;
Expand Down
19 changes: 3 additions & 16 deletions Source/Core/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ define([
'./defineProperties',
'./deprecationWarning',
'./DeveloperError',
'./FeatureDetection',
'./freezeObject',
'./getAbsoluteUri',
'./getBaseUri',
'./getExtensionFromUri',
'./isBlobUri',
'./isCrossOriginUrl',
'./isDataUri',
'./loadAndExecuteScript',
'./objectToQuery',
'./queryToObject',
'./Request',
Expand All @@ -38,14 +38,14 @@ define([
defineProperties,
deprecationWarning,
DeveloperError,
FeatureDetection,
freezeObject,
getAbsoluteUri,
getBaseUri,
getExtensionFromUri,
isBlobUri,
isCrossOriginUrl,
isDataUri,
loadAndExecuteScript,
objectToQuery,
queryToObject,
Request,
Expand Down Expand Up @@ -1934,20 +1934,7 @@ define([
};

Resource._Implementations.loadAndExecuteScript = function(url, functionName, deferred) {
var script = document.createElement('script');
script.async = true;
script.src = url;

var head = document.getElementsByTagName('head')[0];
script.onload = function() {
script.onload = undefined;
head.removeChild(script);
};
script.onerror = function(e) {
deferred.reject(e);
};

head.appendChild(script);
return loadAndExecuteScript(url, functionName).otherwise(deferred.reject);
};

/**
Expand Down
32 changes: 32 additions & 0 deletions Source/Core/loadAndExecuteScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
define([
'../ThirdParty/when'
], function(
when) {
'use strict';

/**
* @private
*/
function loadAndExecuteScript(url) {
var deferred = when.defer();
var script = document.createElement('script');
script.async = true;
script.src = url;

var head = document.getElementsByTagName('head')[0];
script.onload = function() {
script.onload = undefined;
head.removeChild(script);
deferred.resolve();
};
script.onerror = function(e) {
deferred.reject(e);
};

head.appendChild(script);

return deferred.promise;
}

return loadAndExecuteScript;
});
6 changes: 2 additions & 4 deletions Source/ThirdParty/google-earth-dbroot-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
define([
'./protobuf-minimal'
], function(
window.cesiumGoogleEarthDbRootParser = function(
$protobuf) {
/* jshint curly: false, sub: true, newcap: false, shadow: true, unused: false*/
'use strict';
Expand Down Expand Up @@ -8147,4 +8145,4 @@ define([
// End generated code

return $root.keyhole.dbroot;
});
};
14 changes: 12 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var sourceFiles = ['Source/**/*.js',
'!Source/*.js',
'!Source/Workers/**',
'!Source/ThirdParty/Workers/**',
'!Source/ThirdParty/google-earth-dbroot-parser.js',
'!Source/ThirdParty/pako_inflate.js',
'!Source/ThirdParty/crunch.js',
'Source/Workers/createTaskProcessorWorker.js'];
Expand Down Expand Up @@ -891,6 +892,14 @@ function minifyCSS(outputDirectory) {
});
}

var gulpUglify = require('gulp-uglify');

function minifyModules(outputDirectory) {
return streamToPromise(gulp.src('Source/ThirdParty/google-earth-dbroot-parser.js')
.pipe(gulpUglify())
.pipe(gulp.dest(outputDirectory + '/ThirdParty/')));
}

function combineJavaScript(options) {
var optimizer = options.optimizer;
var outputDirectory = options.outputDirectory;
Expand All @@ -901,7 +910,8 @@ function combineJavaScript(options) {

var promise = Promise.join(
combineCesium(!removePragmas, optimizer, combineOutput),
combineWorkers(!removePragmas, optimizer, combineOutput)
combineWorkers(!removePragmas, optimizer, combineOutput),
minifyModules(outputDirectory)
);

return promise.then(function() {
Expand Down Expand Up @@ -1264,7 +1274,7 @@ function buildCesiumViewer() {

gulp.src(['Build/Cesium/Assets/**',
'Build/Cesium/Workers/**',
'Build/Cesium/ThirdParty/Workers/**',
'Build/Cesium/ThirdParty/**',
'Build/Cesium/Widgets/**',
'!Build/Cesium/Widgets/**/*.css'],
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.6.1",
"gulp-tap": "^1.0.1",
"gulp-uglify": "^3.0.0",
"gulp-zip": "^4.0.0",
"jasmine-core": "^3.1.0",
"jsdoc": "^3.4.3",
Expand Down

0 comments on commit ae98c62

Please sign in to comment.