Files
flipper/desktop/babel-transformer/src/app-flipper-requires.ts
Anton Nikolaev c21ccedf14 Dev mode: fixed loading plugins located outside of the Flipper source root folder
Summary: Dev mode: fixed loading of plugins located outside of the Flipper source root folder, e.g. in ~/flipper-plugins as suggested in tutorial docs.

Reviewed By: passy

Differential Revision: D21306639

fbshipit-source-id: bb9044b25324065f0c12169b95fbe663da8d4305
2020-04-30 04:29:41 -07:00

44 lines
1.3 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, Identifier} from '@babel/types';
import {NodePath} from '@babel/traverse';
import {
tryReplaceFlipperRequire,
tryReplaceGlobalReactUsage,
} from './replace-flipper-requires';
import {resolve} from 'path';
const sourceRootDir = resolve(__dirname, '..', '..');
function isExcludedPath(path: string) {
// We shouldn't apply transformations for the plugins which are part of the repository
// as they are bundled with Flipper app and all use the single "react" dependency.
// But we should apply it for the plugins outside of Flipper folder, so they can be loaded
// in dev mode and use "react" from Flipper bundle.
return path.startsWith(sourceRootDir);
}
module.exports = () => ({
visitor: {
CallExpression(path: NodePath<CallExpression>, state: any) {
if (isExcludedPath(state.file.opts.filename)) {
return;
}
tryReplaceFlipperRequire(path);
},
Identifier(path: NodePath<Identifier>, state: any) {
if (isExcludedPath(state.file.opts.filename)) {
return;
}
tryReplaceGlobalReactUsage(path);
},
},
});