1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-10-14 06:51:58 +00:00

More webpack 4 code. Make sure to asynchronously await the availability of our webpack hook everywhere that we use it that it's reasonable to wait.

This adds a new module called switchboard that abuses the root React Router instance to forcibly load a chunk, letting us grab `require()` quickly rather than waiting potentially forever for another chunk to be loaded due to user action, etc.
This commit is contained in:
SirStendec 2018-05-18 17:48:10 -04:00
parent 194f93414d
commit 86c5fee033
13 changed files with 128 additions and 24 deletions

View file

@ -52,7 +52,7 @@ export default class Apollo extends Module {
this.inject('..fine');
}
onEnable() {
async onEnable() {
// TODO: Come up with a better way to await something existing.
let client = this.client;
@ -63,11 +63,11 @@ export default class Apollo extends Module {
client = this.client = inst && inst.props && inst.props.client;
}
this.printer = this.web_munch.getModule('gql-printer');
this.gql_print = this.printer && this.printer.print;
if ( ! client )
return new Promise(s => setTimeout(s,50)).then(() => this.onEnable());
return new Promise(() => this.onEnable(), 50);
this.printer = await this.web_munch.findModule('gql-printer');
this.gql_print = this.printer && this.printer.print;
// Register middleware so that we can intercept requests.
if ( ! this.client.link || ! this.client.queryManager || ! this.client.queryManager.link ) {

View file

@ -23,7 +23,7 @@ export default class WebMunch extends Module {
this._module_names = {};
this._mod_cache = {};
this.v4 = false;
this.v4 = null;
this.hookLoader();
this.hookRequire();
@ -47,7 +47,9 @@ export default class WebMunch extends Module {
if ( typeof window.webpackJsonp === 'function' ) {
// v3
this.v4 = false;
this._original_loader = window.webpackJsonp;
try {
window.webpackJsonp = this.webpackJsonpv3.bind(this);
} catch(err) {
@ -249,7 +251,7 @@ export default class WebMunch extends Module {
const loader = require.e && require.e.toString();
let modules;
if ( loader && loader.indexOf('Loading chunk') !== -1 ) {
const data = /({0:.*?})/.exec(loader);
const data = this.v4 ? /assets\/"\+\(({1:.*?})/.exec(loader) : /({0:.*?})/.exec(loader);
if ( data )
try {
modules = JSON.parse(data[1].replace(/(\d+):/g, '"$1":'))

View file

@ -402,8 +402,9 @@ export class Module extends EventEmitter {
}
inject(name, module) {
inject(name, module, require = true) {
if ( name instanceof Module || name.prototype instanceof Module ) {
require = module != null ? module : true;
module = name;
name = null;
}
@ -447,7 +448,8 @@ export class Module extends EventEmitter {
if ( ! module )
throw new Error(`cannot find module ${name} or no module provided`);
requires.push(module.abs_path('.'));
if ( require )
requires.push(module.abs_path('.'));
if ( this.enabled && ! module.enabled )
module.enable();