UNPKG

flipper-babel-transformer

Version:

Babel transformer for Flipper plugins

54 lines 2.47 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 */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const flipper_env_1 = __importDefault(require("./flipper-env")); const fs_extra_1 = __importDefault(require("fs-extra")); const isFBFile = (filePath) => filePath.includes(`${path_1.sep}fb${path_1.sep}`); const requireFromFolder = (folder, path) => new RegExp(`${folder}/[\\w.-]+(.js)?$`, 'g').test(path); const pathExistsCache = new Map(); function pathExistsSync(path) { const cachedResult = pathExistsCache.get(path); if (cachedResult !== undefined) { return cachedResult; } const result = fs_extra_1.default.pathExistsSync(path); pathExistsCache.set(path, result); return result; } module.exports = () => ({ name: 'replace-fb-stubs', visitor: { CallExpression(path, state) { if (flipper_env_1.default.FLIPPER_FORCE_PUBLIC_BUILD !== 'true' && path.node.type === 'CallExpression' && path.node.callee.type === 'Identifier' && path.node.callee.name === 'require' && path.node.arguments.length > 0 && path.node.arguments[0].type === 'StringLiteral') { if (requireFromFolder('fb', path.node.arguments[0].value) && !isFBFile(state.file.opts.filename)) { throw new Error('For files which are not under fb/ do not require directly from fb/, but rather from fb-stubs/ to not break typescript-typing and make sure stubs are up-to-date.'); } else if (requireFromFolder('fb-stubs', path.node.arguments[0].value)) { const fbPath = path.node.arguments[0].value.replace('/fb-stubs/', '/fb/'); if (flipper_env_1.default.FLIPPER_FB || pathExistsSync((0, path_1.resolve)((0, path_1.dirname)(state.file.opts.filename), fbPath.substr(0, fbPath.indexOf('/fb/') + 4)))) { path.node.arguments[0].value = fbPath; } } } }, }, }); //# sourceMappingURL=fb-stubs.js.map