flipper-babel-transformer
Version:
Babel transformer for Flipper plugins
38 lines • 1.57 kB
JavaScript
;
/**
* 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 });
const types_1 = require("@babel/types");
module.exports = () => ({
name: 'infinity-import-react',
visitor: {
Program: {
exit(path, state) {
if (state.get('NEEDS_REACT')) {
path.unshiftContainer('body', [
(0, types_1.variableDeclaration)('var', [
(0, types_1.variableDeclarator)((0, types_1.identifier)('React'), (0, types_1.callExpression)((0, types_1.identifier)('require'), [(0, types_1.stringLiteral)('react')])),
]),
]);
}
},
},
ReferencedIdentifier(path, state) {
// mark react as needing to be imported
if (path.node.name === 'React' && !path.scope.getBinding('React')) {
state.set('NEEDS_REACT', true);
}
// replace Buffer with require('buffer')
if (path.node.name === 'Buffer' && !path.scope.getBinding('Buffer')) {
path.replaceWith((0, types_1.memberExpression)((0, types_1.callExpression)((0, types_1.identifier)('require'), [(0, types_1.stringLiteral)('buffer')]), (0, types_1.identifier)('Buffer')));
}
},
},
});
//# sourceMappingURL=import-react.js.map