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;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -16,6 +16,7 @@ type FlipperEnvVars = {
|
||||
FLIPPER_TEST_RUNNER?: string;
|
||||
FLIPPER_ELECTRON_VERSION?: string;
|
||||
NODE_ENV?: string;
|
||||
FLIPPER_FORCE_PUBLIC_BUILD?: string;
|
||||
};
|
||||
|
||||
const flipperEnv = new Proxy(
|
||||
@@ -24,6 +25,7 @@ const flipperEnv = new Proxy(
|
||||
FLIPPER_TEST_RUNNER: undefined,
|
||||
FLIPPER_ELECTRON_VERSION: undefined,
|
||||
NODE_ENV: undefined,
|
||||
FLIPPER_FORCE_PUBLIC_BUILD: undefined,
|
||||
} as FlipperEnvVars,
|
||||
{
|
||||
get: function (obj, prop) {
|
||||
|
||||
@@ -9,17 +9,14 @@
|
||||
|
||||
import {default as doTransform} from './transform';
|
||||
import {default as getCacheKey} from './get-cache-key';
|
||||
import {default as flipperEnv} from './flipper-env';
|
||||
|
||||
const presets = [require('@babel/preset-react')];
|
||||
const plugins = [
|
||||
require('./electron-requires'),
|
||||
require('./import-react'),
|
||||
require('./app-flipper-requires'),
|
||||
require('./fb-stubs'),
|
||||
];
|
||||
if (flipperEnv.FLIPPER_FB) {
|
||||
plugins.unshift(require('./fb-stubs'));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
transform,
|
||||
|
||||
@@ -9,13 +9,9 @@
|
||||
|
||||
import {default as doTransform} from './transform';
|
||||
import {default as getCacheKey} from './get-cache-key';
|
||||
import {default as flipperEnv} from './flipper-env';
|
||||
|
||||
const presets = [require('@babel/preset-react')];
|
||||
const plugins = [require('./import-react')];
|
||||
if (flipperEnv.FLIPPER_FB) {
|
||||
plugins.unshift(require('./fb-stubs'));
|
||||
}
|
||||
const plugins = [require('./import-react'), require('./fb-stubs')];
|
||||
|
||||
module.exports = {
|
||||
transform,
|
||||
|
||||
@@ -17,10 +17,7 @@ const presets = [
|
||||
{targets: {electron: flipperEnv.FLIPPER_ELECTRON_VERSION}},
|
||||
],
|
||||
];
|
||||
const plugins = [require('./electron-requires-main')];
|
||||
if (flipperEnv.FLIPPER_FB) {
|
||||
plugins.unshift(require('./fb-stubs'));
|
||||
}
|
||||
const plugins = [require('./electron-requires-main'), require('./fb-stubs')];
|
||||
|
||||
module.exports = {
|
||||
transform,
|
||||
|
||||
@@ -8,16 +8,13 @@
|
||||
*/
|
||||
|
||||
import {default as doTransform} from './transform';
|
||||
import {default as flipperEnv} from './flipper-env';
|
||||
|
||||
const presets = [require('@babel/preset-react')];
|
||||
const plugins = [
|
||||
require('./electron-requires'),
|
||||
require('./plugin-flipper-requires'),
|
||||
require('./fb-stubs'),
|
||||
];
|
||||
if (flipperEnv.FLIPPER_FB) {
|
||||
plugins.unshift(require('./fb-stubs'));
|
||||
}
|
||||
export default function transform({
|
||||
filename,
|
||||
options,
|
||||
|
||||
Reference in New Issue
Block a user