Summary: Fixes required to be able to run Flipper in node.js: * Adds checks if the `window`-object exists before using it, to allow running in node. * Imports from within Flipper should directly reference the file they are requiring instead of `import from 'flipper'`. This was done in most of the places. Fixed a few occurrences where this wasn't the case. This is to prevent cyclic dependencies in node. * shared packages (React, ReactDOM and Flipper) were exposed on the `window` before, changed this to `global` as this works in browser and node. * Adds some missing methods to our electron stubs (used for testing and headless Flipper) Reviewed By: passy Differential Revision: D13786577 fbshipit-source-id: 145d560f1446e7d0bdec2acd8dd54dae983d7b36
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
/**
|
|
* Copyright 2018-present Facebook.
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
* @format
|
|
*/
|
|
|
|
const {resolve, dirname} = require('path');
|
|
|
|
// do not apply this transform for these paths
|
|
const EXCLUDE_PATHS = [
|
|
'/node_modules/react-devtools-core/',
|
|
'relay-devtools/DevtoolsUI',
|
|
];
|
|
|
|
function isExcludedPath(path) {
|
|
for (const epath of EXCLUDE_PATHS) {
|
|
if (path.indexOf(epath) > -1) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
} // $FlowFixMe
|
|
module.exports = ({types: t}) => ({
|
|
visitor: {
|
|
// $FlowFixMe
|
|
CallExpression(path, state) {
|
|
if (isExcludedPath(state.file.opts.filename)) {
|
|
return;
|
|
}
|
|
const node = path.node;
|
|
const args = node.arguments || [];
|
|
|
|
if (
|
|
node.callee.name === 'require' &&
|
|
args.length === 1 &&
|
|
t.isStringLiteral(args[0])
|
|
) {
|
|
if (args[0].value === 'flipper') {
|
|
path.replaceWith(t.identifier('global.Flipper'));
|
|
} else if (args[0].value === 'react') {
|
|
path.replaceWith(t.identifier('global.React'));
|
|
} else if (args[0].value === 'react-dom') {
|
|
path.replaceWith(t.identifier('global.ReactDOM'));
|
|
} else if (
|
|
// require a file not a pacakge
|
|
args[0].value.indexOf('/') > -1 &&
|
|
// in the plugin itself and not inside one of its dependencies
|
|
state.file.opts.filename.indexOf('node_modules') === -1 &&
|
|
// the resolved path for this file is outside the plugins root
|
|
!resolve(dirname(state.file.opts.filename), args[0].value).startsWith(
|
|
state.file.opts.root,
|
|
) &&
|
|
!resolve(dirname(state.file.opts.filename), args[0].value).indexOf(
|
|
'/static/',
|
|
) < 0
|
|
) {
|
|
throw new Error(
|
|
`Plugins cannot require files from outside their folder. Attempted to require ${resolve(
|
|
dirname(state.file.opts.filename),
|
|
args[0].value,
|
|
)} which isn't inside ${state.file.opts.root}`,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
Identifier(path, state) {
|
|
if (
|
|
path.node.name === 'React' &&
|
|
path.parentPath.node.id !== path.node &&
|
|
!isExcludedPath(state.file.opts.filename)
|
|
) {
|
|
path.replaceWith(t.identifier('global.React'));
|
|
}
|
|
},
|
|
},
|
|
});
|