diff --git a/closure/goog/module/moduleloader.js b/closure/goog/module/moduleloader.js index f940d29187..d5e92ce51b 100644 --- a/closure/goog/module/moduleloader.js +++ b/closure/goog/module/moduleloader.js @@ -68,6 +68,13 @@ function ModuleLoader() { * @private */ this.loadingModulesStatus_ = {}; + + /** + * Whether the module loader has prefetched a module. + * @type {boolean} + * @private + */ + this.hasPrefetched_ = false; } goog.inherits(ModuleLoader, EventTarget); @@ -258,6 +265,9 @@ ModuleLoader.prototype.usingSourceUrlInjection_ = function() { /** @override */ ModuleLoader.prototype.loadModules = function( ids, moduleInfoMap, {forceReload, onError, onSuccess, onTimeout} = {}) { + if (this.hasPrefetched_ && ids.length > 1) { + throw new Error('Modules prefetching is not supported in batch mode'); + } const loadStatus = this.loadingModulesStatus_[ids] || ModuleLoader.LoadStatus.createForIds_(ids, moduleInfoMap); loadStatus.loadRequested = true; @@ -362,6 +372,7 @@ ModuleLoader.prototype.handleSuccess_ = function(bulkLoader, moduleIds) { /** @override */ ModuleLoader.prototype.prefetchModule = function(id, moduleInfo) { + this.hasPrefetched_ = true; // Do not prefetch in debug mode if (this.getDebugMode()) { return; diff --git a/closure/goog/module/moduleloader_test.js b/closure/goog/module/moduleloader_test.js index 9a1c5acffa..1579e90fbc 100644 --- a/closure/goog/module/moduleloader_test.js +++ b/closure/goog/module/moduleloader_test.js @@ -528,8 +528,10 @@ testSuite({ testPrefetchModuleWithBatchModeEnabled() { moduleManager.setBatchModeEnabled(true); + moduleManager.prefetchModule('modA'); + assertThrows('Modules prefetching is not supported in batch mode', () => { - moduleManager.prefetchModule('modA'); + moduleManager.execOnLoad('modB', () => {}); }); }, diff --git a/closure/goog/module/modulemanager.js b/closure/goog/module/modulemanager.js index c84bbb01d0..b069dd16e0 100644 --- a/closure/goog/module/modulemanager.js +++ b/closure/goog/module/modulemanager.js @@ -473,14 +473,10 @@ goog.module.ModuleManager.prototype.preloadModule = function(id, opt_timeout) { /** @override */ goog.module.ModuleManager.prototype.prefetchModule = function(id) { - if (this.batchModeEnabled_) { - throw new Error('Modules prefetching is not supported in batch mode'); - } else { - var idWithDeps = this.getNotYetLoadedTransitiveDepIds_(id); - for (var i = 0; i < idWithDeps.length; i++) { - const moduleInfoOfDep = this.getModuleInfo(idWithDeps[i]); - this.getLoader().prefetchModule(idWithDeps[i], moduleInfoOfDep); - } + var idWithDeps = this.getNotYetLoadedTransitiveDepIds_(id); + for (var i = 0; i < idWithDeps.length; i++) { + const moduleInfoOfDep = this.getModuleInfo(idWithDeps[i]); + this.getLoader().prefetchModule(idWithDeps[i], moduleInfoOfDep); } };