Emit events for when webpack modules are loaded

This commit is contained in:
Samuel Elliott 2018-06-23 23:15:45 +01:00
parent 4d2347cf84
commit ee0b13dab2
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 49 additions and 2 deletions

View File

@ -9,6 +9,7 @@
*/
import { Filters } from 'common';
import Events from './events';
const KnownModules = {
React: Filters.byProperties(['createElement', 'cloneElement']),
@ -264,6 +265,15 @@ class WebpackModules {
*/
static get require() {
if (this._require) return this._require;
const __webpack_require__ = this.getWebpackRequire();
if (!__webpack_require__) return;
this.hookWebpackRequireCache(__webpack_require__);
return this._require = __webpack_require__;
}
static getWebpackRequire() {
const id = 'bd-webpackmodules';
if (typeof window.webpackJsonp === 'function') {
@ -272,7 +282,7 @@ class WebpackModules {
}, [id]).default;
delete __webpack_require__.m[id];
delete __webpack_require__.c[id];
return this._require = __webpack_require__;
return __webpack_require__;
} else if (window.webpackJsonp && window.webpackJsonp.push) {
const __webpack_require__ = window['webpackJsonp'].push([[], {
[id]: (module, exports, req) => exports.default = req
@ -280,10 +290,47 @@ class WebpackModules {
window['webpackJsonp'].pop();
delete __webpack_require__.m[id];
delete __webpack_require__.c[id];
return this._require = __webpack_require__;
return __webpack_require__;
}
}
static hookWebpackRequireCache(__webpack_require__) {
__webpack_require__.c = new Proxy(__webpack_require__.c, {
set(module_cache, module_id, module) {
// Add it to our emitter cache and emit a module-loading event
this.moduleLoading(module_id, module);
Events.emit('module-loading', module);
// Add the module to the cache as normal
module_cache[module_id] = module;
}
});
}
static moduleLoading(module_id, module) {
if (this.require.c[module_id]) return;
if (!this.moduleLoadedEventTimeout) {
this.moduleLoadedEventTimeout = setTimeout(() => {
this.moduleLoadedEventTimeout = undefined;
// Emit a module-loaded event for every module
for (let module of this.modulesLoadingCache) {
Events.emit('module-loaded', module);
}
// Emit a modules-loaded event
Events.emit('modules-loaded', this.modulesLoadingCache);
this.modulesLoadedCache = [];
}, 0);
}
// Add this to our own cache
if (!this.modulesLoadingCache) this.modulesLoadingCache = [];
this.modulesLoadingCache.push(module);
}
/**
* Returns all loaded modules.
* @return {Array}