remove window dependency

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
This commit is contained in:
Daniel Büchele
2019-01-25 12:10:31 -08:00
committed by Facebook Github Bot
parent 7ac6a09af1
commit 771be72b3f
15 changed files with 77 additions and 46 deletions

View File

@@ -17,36 +17,36 @@ const babelOptions = {
filename: 'index.js',
};
test('transform react requires to global window', () => {
test('transform react requires to global object', () => {
const src = 'require("react")';
const ast = parse(src);
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
const {code} = generate(transformed);
expect(code).toBe('window.React;');
expect(code).toBe('global.React;');
});
test('transform react-dom requires to global window', () => {
test('transform react-dom requires to global object', () => {
const src = 'require("react-dom")';
const ast = parse(src);
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
const {code} = generate(transformed);
expect(code).toBe('window.ReactDOM;');
expect(code).toBe('global.ReactDOM;');
});
test('transform flipper requires to global window', () => {
test('transform flipper requires to global object', () => {
const src = 'require("flipper")';
const ast = parse(src);
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
const {code} = generate(transformed);
expect(code).toBe('window.Flipper;');
expect(code).toBe('global.Flipper;');
});
test('transform React identifier to window.React', () => {
test('transform React identifier to global.React', () => {
const src = 'React;';
const ast = parse(src);
const transformed = transformFromAstSync(ast, src, babelOptions).ast;
const {code} = generate(transformed);
expect(code).toBe('window.React;');
expect(code).toBe('global.React;');
});
test.skip('throw error when requiring outside the plugin', () => {

View File

@@ -29,6 +29,7 @@ const BUILTINS = [
'assert',
'util',
'path',
'perf_hooks',
'punycode',
'querystring',
'cluster',

View File

@@ -37,11 +37,11 @@ module.exports = ({types: t}) => ({
t.isStringLiteral(args[0])
) {
if (args[0].value === 'flipper') {
path.replaceWith(t.identifier('window.Flipper'));
path.replaceWith(t.identifier('global.Flipper'));
} else if (args[0].value === 'react') {
path.replaceWith(t.identifier('window.React'));
path.replaceWith(t.identifier('global.React'));
} else if (args[0].value === 'react-dom') {
path.replaceWith(t.identifier('window.ReactDOM'));
path.replaceWith(t.identifier('global.ReactDOM'));
} else if (
// require a file not a pacakge
args[0].value.indexOf('/') > -1 &&
@@ -70,7 +70,7 @@ module.exports = ({types: t}) => ({
path.parentPath.node.id !== path.node &&
!isExcludedPath(state.file.opts.filename)
) {
path.replaceWith(t.identifier('window.React'));
path.replaceWith(t.identifier('global.React'));
}
},
},