mirror of
https://github.com/facebook/react.git
synced 2026-02-25 13:13:03 +00:00
* Refactor Flight to require a module reference to be brand checked This exposes a host environment (bundler) specific hook to check if an object is a module reference. This will be used so that they can be passed directly into Flight without needing additional wrapper objects. * Emit module references as a special type of value We already have JSON and errors as special types of "rows". This encodes module references as a special type of row value. This was always the intention because it allows those values to be emitted first in the stream so that as a large models stream down, we can start preloading as early as possible. We preload the module when they resolve but we lazily require them as they are referenced. * Emit module references where ever they occur This emits module references where ever they occur. In blocks or even directly in elements. * Don't special case the root row I originally did this so that a simple stream is also just plain JSON. However, since we might want to emit things like modules before the root module in the stream, this gets unnecessarily complicated. We could add this back as a special case if it's the first byte written but meh. * Update the protocol * Add test for using a module reference as a client component * Relax element type check Since Flight now accepts a module reference as returned by any bundler system, depending on the renderer running. We need to drastically relax the check to include all of them. We can add more as we discover them. * Move flow annotation Seems like our compiler is not happy with stripping this. * Some bookkeeping bug * Can't use the private field to check
102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const inlinedHostConfigs = require('../shared/inlinedHostConfigs');
|
|
|
|
jest.mock('react-reconciler/src/ReactFiberReconciler', () => {
|
|
return require.requireActual(
|
|
__VARIANT__
|
|
? 'react-reconciler/src/ReactFiberReconciler.new'
|
|
: 'react-reconciler/src/ReactFiberReconciler.old'
|
|
);
|
|
});
|
|
|
|
// When testing the custom renderer code path through `react-reconciler`,
|
|
// turn the export into a function, and use the argument as host config.
|
|
const shimHostConfigPath = 'react-reconciler/src/ReactFiberHostConfig';
|
|
jest.mock('react-reconciler', () => {
|
|
return config => {
|
|
jest.mock(shimHostConfigPath, () => config);
|
|
return require.requireActual('react-reconciler');
|
|
};
|
|
});
|
|
const shimServerStreamConfigPath = 'react-server/src/ReactServerStreamConfig';
|
|
const shimServerFormatConfigPath = 'react-server/src/ReactServerFormatConfig';
|
|
const shimFlightServerConfigPath = 'react-server/src/ReactFlightServerConfig';
|
|
jest.mock('react-server', () => {
|
|
return config => {
|
|
jest.mock(shimServerStreamConfigPath, () => config);
|
|
jest.mock(shimServerFormatConfigPath, () => config);
|
|
return require.requireActual('react-server');
|
|
};
|
|
});
|
|
jest.mock('react-server/flight', () => {
|
|
return config => {
|
|
jest.mock(shimServerStreamConfigPath, () => config);
|
|
jest.mock(shimServerFormatConfigPath, () => config);
|
|
jest.mock('react-server/src/ReactFlightServerBundlerConfigCustom', () => ({
|
|
isModuleReference: config.isModuleReference,
|
|
resolveModuleMetaData: config.resolveModuleMetaData,
|
|
}));
|
|
jest.mock(shimFlightServerConfigPath, () =>
|
|
require.requireActual(
|
|
'react-server/src/forks/ReactFlightServerConfig.custom'
|
|
)
|
|
);
|
|
return require.requireActual('react-server/flight');
|
|
};
|
|
});
|
|
const shimFlightClientHostConfigPath =
|
|
'react-client/src/ReactFlightClientHostConfig';
|
|
jest.mock('react-client/flight', () => {
|
|
return config => {
|
|
jest.mock(shimFlightClientHostConfigPath, () => config);
|
|
return require.requireActual('react-client/flight');
|
|
};
|
|
});
|
|
|
|
const configPaths = [
|
|
'react-reconciler/src/ReactFiberHostConfig',
|
|
'react-client/src/ReactFlightClientHostConfig',
|
|
'react-server/src/ReactServerStreamConfig',
|
|
'react-server/src/ReactServerFormatConfig',
|
|
'react-server/src/ReactFlightServerConfig',
|
|
];
|
|
|
|
function mockAllConfigs(rendererInfo) {
|
|
configPaths.forEach(path => {
|
|
// We want the reconciler to pick up the host config for this renderer.
|
|
jest.mock(path, () => {
|
|
let idx = path.lastIndexOf('/');
|
|
let forkPath = path.substr(0, idx) + '/forks' + path.substr(idx);
|
|
return require.requireActual(`${forkPath}.${rendererInfo.shortName}.js`);
|
|
});
|
|
});
|
|
}
|
|
|
|
// But for inlined host configs (such as React DOM, Native, etc), we
|
|
// mock their named entry points to establish a host config mapping.
|
|
inlinedHostConfigs.forEach(rendererInfo => {
|
|
if (rendererInfo.shortName === 'custom') {
|
|
// There is no inline entry point for the custom renderers.
|
|
// Instead, it's handled by the generic `react-reconciler` entry point above.
|
|
return;
|
|
}
|
|
rendererInfo.entryPoints.forEach(entryPoint => {
|
|
jest.mock(entryPoint, () => {
|
|
mockAllConfigs(rendererInfo);
|
|
return require.requireActual(entryPoint);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Make it possible to import this module inside
|
|
// the React package itself.
|
|
jest.mock('shared/ReactSharedInternals', () =>
|
|
require.requireActual('react/src/ReactSharedInternals')
|
|
);
|
|
|
|
jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));
|
|
jest.mock('scheduler/src/SchedulerHostConfig', () =>
|
|
require.requireActual('scheduler/src/forks/SchedulerHostConfig.mock.js')
|
|
);
|