Files
flipper/desktop/babel-transformer/src/replace-flipper-requires.ts
Michel Weststrate 9987c8ee89 Make sure antd is used from Flipper
Summary: Fix build job that didn't include require rewrites for antd

Reviewed By: timur-valiev

Differential Revision: D26311554

fbshipit-source-id: 473a9c7d343e4534a33e5938ea27667f7795d8ac
2021-02-08 04:16:53 -08:00

67 lines
1.7 KiB
TypeScript

/**
* 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';
// This list should match `dispatcher/plugins.tsx` and `builtInModules` in `desktop/.eslintrc.js`
const requireReplacements: any = {
flipper: 'global.Flipper',
'flipper-plugin': 'global.FlipperPlugin',
react: 'global.React',
'react-dom': 'global.ReactDOM',
adbkit: 'global.adbkit',
antd: 'global.antd',
immer: 'global.Immer',
'@emotion/styled': 'global.emotion_styled',
'@ant-design/icons': 'global.antdesign_icons',
};
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'
);
}