Transform fb-internal code within plugins
Summary: This diff changes flipper-pkg to allow using fb-internal code in plugins when they are packaged as standalone packages. It basically transforms fb-stubs/fb folders in plugins in the same way as for Flipper app. Reviewed By: passy Differential Revision: D27173406 fbshipit-source-id: 8ba97d5f57b5e276c19d13270a0d810873504add
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b45b5f854a
commit
4d3e631ce6
@@ -7,22 +7,35 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import {resolve, dirname, sep} from 'path';
|
||||
import env from './flipper-env';
|
||||
import {CallExpression} from '@babel/types';
|
||||
import {NodePath} from '@babel/traverse';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
const isFBFile = (filePath: string) =>
|
||||
filePath.includes(`${path.sep}fb${path.sep}`);
|
||||
const isFBFile = (filePath: string) => filePath.includes(`${sep}fb${sep}`);
|
||||
|
||||
const requireFromFolder = (folder: string, path: string) =>
|
||||
new RegExp(folder + '/[\\w.-]+(.js)?$', 'g').test(path);
|
||||
|
||||
const pathExistsCache = new Map<string, boolean>();
|
||||
|
||||
function pathExistsSync(path: string): boolean {
|
||||
const cachedResult = pathExistsCache.get(path);
|
||||
if (cachedResult !== undefined) {
|
||||
return cachedResult;
|
||||
}
|
||||
const result = fs.pathExistsSync(path);
|
||||
pathExistsCache.set(path, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = () => ({
|
||||
name: 'replace-fb-stubs',
|
||||
visitor: {
|
||||
CallExpression(path: NodePath<CallExpression>, state: any) {
|
||||
if (
|
||||
process.env.FLIPPER_FORCE_PUBLIC_BUILD !== 'true' &&
|
||||
env.FLIPPER_FORCE_PUBLIC_BUILD !== 'true' &&
|
||||
path.node.type === 'CallExpression' &&
|
||||
path.node.callee.type === 'Identifier' &&
|
||||
path.node.callee.name === 'require' &&
|
||||
@@ -39,10 +52,21 @@ module.exports = () => ({
|
||||
} else if (
|
||||
requireFromFolder('fb-stubs', path.node.arguments[0].value)
|
||||
) {
|
||||
path.node.arguments[0].value = path.node.arguments[0].value.replace(
|
||||
const fbPath = path.node.arguments[0].value.replace(
|
||||
'/fb-stubs/',
|
||||
'/fb/',
|
||||
);
|
||||
if (
|
||||
env.FLIPPER_FB ||
|
||||
pathExistsSync(
|
||||
resolve(
|
||||
dirname(state.file.opts.filename),
|
||||
fbPath.substr(0, fbPath.indexOf('/fb/') + 4),
|
||||
),
|
||||
)
|
||||
) {
|
||||
path.node.arguments[0].value = fbPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user