Skip to content

Plugins relying on module hijacking won't load. #2866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down