UNPKG

flipper-babel-transformer

Version:

Babel transformer for Flipper plugins

57 lines 2.08 kB
"use strict"; /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ Object.defineProperty(exports, "__esModule", { value: true }); exports.tryReplaceGlobalReactUsage = exports.tryReplaceFlipperRequire = void 0; const types_1 = require("@babel/types"); // This list should match `dispatcher/plugins.tsx` and `builtInModules` in `desktop/.eslintrc.js` const requireReplacements = { flipper: 'global.Flipper', 'flipper-plugin': 'global.FlipperPlugin', react: 'global.React', 'react-dom': 'global.ReactDOM', 'react-dom/client': 'global.ReactDOMClient', 'react-is': 'global.ReactIs', antd: 'global.antd', immer: 'global.Immer', '@emotion/styled': 'global.emotion_styled', '@ant-design/icons': 'global.antdesign_icons', }; function tryReplaceFlipperRequire(path) { const node = path.node; const args = node.arguments || []; if (node.callee.type === 'Identifier' && node.callee.name === 'require' && args.length === 1 && (0, types_1.isStringLiteral)(args[0])) { const replacement = requireReplacements[args[0].value]; if (replacement) { path.replaceWith((0, types_1.identifier)(replacement)); return true; } } return false; } exports.tryReplaceFlipperRequire = tryReplaceFlipperRequire; function tryReplaceGlobalReactUsage(path) { if (path.node.name === 'React' && path.parentPath.node.id !== path.node && path.parent.type !== 'ObjectProperty' && !isReactImportIdentifier(path)) { path.replaceWith((0, types_1.identifier)('global.React')); return true; } return false; } exports.tryReplaceGlobalReactUsage = tryReplaceGlobalReactUsage; function isReactImportIdentifier(path) { return (path.parentPath.node.type === 'ImportNamespaceSpecifier' && path.parentPath.node.local.name === 'React'); } //# sourceMappingURL=replace-flipper-requires.js.map