diff --git a/CHANGES.md b/CHANGES.md index d3e9897219d2..b6b66b3e545e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/Source/Core/GoogleEarthEnterpriseMetadata.js b/Source/Core/GoogleEarthEnterpriseMetadata.js index 32fc531a5387..ef5f4859a817 100644 --- a/Source/Core/GoogleEarthEnterpriseMetadata.js +++ b/Source/Core/GoogleEarthEnterpriseMetadata.js @@ -1,6 +1,7 @@ define([ - '../ThirdParty/google-earth-dbroot-parser', + '../ThirdParty/protobuf-minimal', '../ThirdParty/when', + './buildModuleUrl', './Check', './Credit', './defaultValue', @@ -8,14 +9,16 @@ define([ './defineProperties', './GoogleEarthEnterpriseTileInformation', './isBitSet', + './loadAndExecuteScript', './Math', './Request', './Resource', './RuntimeError', './TaskProcessor' ], function( - dbrootParser, + protobufMinimal, when, + buildModuleUrl, Check, Credit, defaultValue, @@ -23,6 +26,7 @@ define([ defineProperties, GoogleEarthEnterpriseTileInformation, isBitSet, + loadAndExecuteScript, CesiumMath, Request, Resource, @@ -488,6 +492,8 @@ define([ }); } + var dbrootParser; + var dbrootParserPromise; function requestDbRoot(that) { var resource = that._resource.getDerivedResource({ url: 'dbRoot.v5', @@ -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; diff --git a/Source/Core/Resource.js b/Source/Core/Resource.js index 67301d5fb446..44605eb83007 100644 --- a/Source/Core/Resource.js +++ b/Source/Core/Resource.js @@ -10,7 +10,6 @@ define([ './defineProperties', './deprecationWarning', './DeveloperError', - './FeatureDetection', './freezeObject', './getAbsoluteUri', './getBaseUri', @@ -18,6 +17,7 @@ define([ './isBlobUri', './isCrossOriginUrl', './isDataUri', + './loadAndExecuteScript', './objectToQuery', './queryToObject', './Request', @@ -38,7 +38,6 @@ define([ defineProperties, deprecationWarning, DeveloperError, - FeatureDetection, freezeObject, getAbsoluteUri, getBaseUri, @@ -46,6 +45,7 @@ define([ isBlobUri, isCrossOriginUrl, isDataUri, + loadAndExecuteScript, objectToQuery, queryToObject, Request, @@ -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); }; /** diff --git a/Source/Core/loadAndExecuteScript.js b/Source/Core/loadAndExecuteScript.js new file mode 100644 index 000000000000..877608ad291e --- /dev/null +++ b/Source/Core/loadAndExecuteScript.js @@ -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; +}); diff --git a/Source/ThirdParty/google-earth-dbroot-parser.js b/Source/ThirdParty/google-earth-dbroot-parser.js index ef369c84aaea..561fc4aa9642 100644 --- a/Source/ThirdParty/google-earth-dbroot-parser.js +++ b/Source/ThirdParty/google-earth-dbroot-parser.js @@ -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'; @@ -8147,4 +8145,4 @@ define([ // End generated code return $root.keyhole.dbroot; -}); +}; diff --git a/gulpfile.js b/gulpfile.js index 296ce6f724ed..dda17ece6d1e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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']; @@ -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; @@ -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() { @@ -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'], { diff --git a/package.json b/package.json index 85886fc29248..6235b74719f8 100644 --- a/package.json +++ b/package.json @@ -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",