Dev mode: fixed loading plugins located outside of the Flipper source root folder

Summary: Dev mode: fixed loading of plugins located outside of the Flipper source root folder, e.g. in ~/flipper-plugins as suggested in tutorial docs.

Reviewed By: passy

Differential Revision: D21306639

fbshipit-source-id: bb9044b25324065f0c12169b95fbe663da8d4305
This commit is contained in:
Anton Nikolaev
2020-04-30 04:25:28 -07:00
committed by Facebook GitHub Bot
parent b27f8ee236
commit c21ccedf14
11 changed files with 172 additions and 45 deletions

View File

@@ -0,0 +1,60 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {
CallExpression,
isStringLiteral,
identifier,
Identifier,
} from '@babel/types';
import {NodePath} from '@babel/traverse';
const requireReplacements: any = {
flipper: 'global.Flipper',
react: 'global.React',
'react-dom': 'global.ReactDOM',
adbkit: 'global.adbkit',
};
export function tryReplaceFlipperRequire(path: NodePath<CallExpression>) {
const node = path.node;
const args = node.arguments || [];
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
args.length === 1 &&
isStringLiteral(args[0])
) {
const replacement = requireReplacements[args[0].value];
if (replacement) {
path.replaceWith(identifier(replacement));
return true;
}
}
return false;
}
export function tryReplaceGlobalReactUsage(path: NodePath<Identifier>) {
if (
path.node.name === 'React' &&
(path.parentPath.node as any).id !== path.node &&
!isReactImportIdentifier(path)
) {
path.replaceWith(identifier('global.React'));
return true;
}
return false;
}
function isReactImportIdentifier(path: NodePath<Identifier>) {
return (
path.parentPath.node.type === 'ImportNamespaceSpecifier' &&
path.parentPath.node.local.name === 'React'
);
}