From e9682ab3d117eac009b87c89665a24f067c683a8 Mon Sep 17 00:00:00 2001 From: Timo Welde Date: Tue, 15 May 2018 15:58:36 +0200 Subject: [PATCH] Fix plugin module hijacking on main process (#2866) Fixes some plugin loading (like hyperline) --- app/plugins.js | 34 ++++++++++++++++++++++++++++++++++ lib/utils/plugins.js | 2 ++ 2 files changed, 36 insertions(+) diff --git a/app/plugins.js b/app/plugins.js index efb085f9..c3f5c421 100644 --- a/app/plugins.js +++ b/app/plugins.js @@ -17,6 +17,8 @@ const cache = new Config(); const path = plugs.base; const localPath = plugs.local; +patchModuleLoad(); + // caches let plugins = config.getPlugins(); let paths = getPaths(); @@ -43,6 +45,38 @@ config.subscribe(() => { } }); +// patching Module._load +// so plugins can `require` them without needing their own version +// https://github.com/zeit/hyper/issues/619 +function patchModuleLoad() { + const React = require('react'); + const PureComponent = React.PureComponent; + const ReactDOM = require('react-dom'); + + const Module = require('module'); + const originalLoad = Module._load; + Module._load = function _load(modulePath) { + // PLEASE NOTE: Code changes here, also need to be changed in + // lib/utils/plugins.js + switch (modulePath) { + case 'react': + return React; + case 'react-dom': + return ReactDOM; + case 'hyper/component': + return PureComponent; + // These return Object, since they work differently on the backend, than on the frontend. + // Still needs to be here, to prevent errors, while loading plugins. + case 'hyper/Notification': + case 'hyper/notify': + case 'hyper/decorate': + return Object; + default: + return originalLoad.apply(this, arguments); + } + }; +} + function checkDeprecatedExtendKeymaps() { modules.forEach(plugin => { if (plugin.extendKeymaps) { diff --git a/lib/utils/plugins.js b/lib/utils/plugins.js index 71700f0e..8400db11 100644 --- a/lib/utils/plugins.js +++ b/lib/utils/plugins.js @@ -14,6 +14,8 @@ import notify from './notify'; const Module = require('module'); const originalLoad = Module._load; Module._load = function _load(path) { + // PLEASE NOTE: Code changes here, also need to be changed in + // app/plugins.js switch (path) { case 'react': return React;