Use v8 snapshots to cache dependencies

This commit is contained in:
Labhansh Agrawal 2022-04-14 22:20:54 +05:30
parent 009687f8f8
commit d98f340587
12 changed files with 447 additions and 13 deletions

11
lib/hyper.d.ts vendored
View file

@ -7,6 +7,17 @@ declare global {
rpc: Client;
focusActiveTerm: (uid?: string) => void;
}
const snapshotResult: {
customRequire: {
(module: string): NodeModule;
cache: Record<string, {exports: NodeModule}>;
definitions: Record<string, {exports: any}>;
};
setGlobals(global: any, process: any, window: any, document: any, console: any, require: any): void;
};
const __non_webpack_require__: NodeRequire;
}
export type ITermGroup = Immutable<{

View file

@ -1,3 +1,4 @@
import './v8-snapshot-util';
import {webFrame} from 'electron';
import forceUpdate from 'react-deep-force-update';
import {Provider} from 'react-redux';

21
lib/v8-snapshot-util.ts Normal file
View file

@ -0,0 +1,21 @@
if (typeof snapshotResult !== 'undefined') {
const Module = __non_webpack_require__('module');
const originalLoad: (module: string, ...args: any[]) => any = Module._load;
Module._load = function _load(module: string, ...args: any[]): NodeModule {
let cachedModule = snapshotResult.customRequire.cache[module];
if (cachedModule) return cachedModule.exports;
if (snapshotResult.customRequire.definitions[module]) {
cachedModule = {exports: snapshotResult.customRequire(module)};
} else {
cachedModule = {exports: originalLoad(module, ...args)};
}
snapshotResult.customRequire.cache[module] = cachedModule;
return cachedModule.exports;
};
snapshotResult.setGlobals(global, process, window, document, console, global.require);
}