Files
flipper/desktop/babel-transformer/src/replace-flipper-requires.ts
Michel Weststrate 740fbe57fd Fix release build issue
Summary:
D24358369 (8a31e984b3) introduced a dependency outside the `sonar/` dir, which probably should not have happened. D27324576 finally broke this, by adding a dep to QPL core, which will never be installed in our CI.

This diff unbreaks that rewriting the require to a locally provided version of crc32, however this is a stop gap, and it would be great if someone would look into a sustainable solution :)

Reviewed By: passy

Differential Revision: D27327272

fbshipit-source-id: 70cdf21c7ecf081ef804d6338ec11e498e3cb7cf
2021-03-25 08:42:08 -07:00

68 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',
crc32: 'global.crc32_hack_fix_me',
};
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'
);
}