Skip to content

Commit

Permalink
Updating forking implementation to match against more general fork im…
Browse files Browse the repository at this point in the history
…plementations (facebook#27205)

Search for more generic fork files if an exact match does not exist. If
`forks/MyFile.dom.js` exists but `forks/MyFile.dom-node.js` does not
then use it when trying to resolve forks for the `"dom-node"` renderer
in flow, tests, and build

consolidate certain fork files that were identical and make semantic
sense to be generalized
add `dom-browser-esm` bundle and use it for
`react-server-dom-esm/client.browser` build
  • Loading branch information
gnoff authored Aug 17, 2023
1 parent e505316 commit 5623f2a
Show file tree
Hide file tree
Showing 26 changed files with 340 additions and 321 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from 'react-client/src/ReactFlightClientConfigBrowser';
export * from 'react-server-dom-esm/src/ReactFlightClientConfigESMBundler';
export * from 'react-dom-bindings/src/shared/ReactFlightClientConfigDOM';
export const usedWithSSR = false;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @flow
*/

// This should really have a Node and a Browser fork but to avoid too many configs we limit this to build the same for both
export * from 'react-client/src/ReactFlightClientConfigBrowser';
export * from 'react-server-dom-esm/src/ReactFlightClientConfigESMBundler';
export * from 'react-dom-bindings/src/shared/ReactFlightClientConfigDOM';
Expand Down
10 changes: 0 additions & 10 deletions packages/react-reconciler/src/forks/ReactFiberConfig.dom-bun.js

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions packages/react-reconciler/src/forks/ReactFiberConfig.dom-fb.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/react-reconciler/src/forks/ReactFiberConfig.dom-legacy.js

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions packages/react-reconciler/src/forks/ReactFiberConfig.dom-node.js

This file was deleted.

14 changes: 0 additions & 14 deletions packages/react-server/src/forks/ReactFizzConfig.dom-bun.js

This file was deleted.

14 changes: 0 additions & 14 deletions packages/react-server/src/forks/ReactFizzConfig.dom-fb.js

This file was deleted.

17 changes: 0 additions & 17 deletions packages/react-server/src/forks/ReactFizzConfig.dom-node-esm.js

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {AsyncLocalStorage} from 'async_hooks';

import type {Request} from 'react-server/src/ReactFlightServer';

export * from 'react-server-dom-webpack/src/ReactFlightServerConfigWebpackBundler';
export * from 'react-server-dom-esm/src/ReactFlightServerConfigESMBundler';
export * from 'react-dom-bindings/src/server/ReactFlightServerConfigDOM';

export const supportsRequestStorage = true;
Expand Down

This file was deleted.

This file was deleted.

76 changes: 58 additions & 18 deletions scripts/flow/createFlowConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,48 @@

const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const inlinedHostConfigs = require('../shared/inlinedHostConfigs');

const configTemplate = fs
.readFileSync(__dirname + '/config/flowconfig')
.toString();

// stores all forks discovered during config generation
const allForks = new Set();
// maps forked file to the base path containing it and it's forks (it's parent)
const forkedFiles = new Map();

function findForks(file) {
const basePath = path.join(file, '..');
const forksPath = path.join(basePath, 'forks');
const forks = fs.readdirSync(path.join('packages', forksPath));
forks.forEach(f => allForks.add('forks/' + f));
forkedFiles.set(file, basePath);
return basePath;
}

function addFork(forks, renderer, file) {
let basePath = forkedFiles.get(file);
if (!basePath) {
basePath = findForks(file);
}

const baseFilename = file.slice(basePath.length + 1);

const parts = renderer.split('-');
while (parts.length) {
const candidate = `forks/${baseFilename}.${parts.join('-')}.js`;
if (allForks.has(candidate)) {
forks.set(candidate, `${baseFilename}$$`);
return;
}
parts.pop();
}
throw new Error(`Cannot find fork for ${file} for renderer ${renderer}`);
}

function writeConfig(
renderer,
rendererInfo,
Expand Down Expand Up @@ -44,34 +79,39 @@ function writeConfig(
}
ignoredPaths.push(`.*/packages/${otherPath}`);
});
});

const forks = new Map();
addFork(forks, renderer, 'react-reconciler/src/ReactFiberConfig');
addFork(forks, serverRenderer, 'react-server/src/ReactServerStreamConfig');
addFork(forks, serverRenderer, 'react-server/src/ReactFizzConfig');
addFork(forks, flightRenderer, 'react-server/src/ReactFlightServerConfig');
addFork(forks, flightRenderer, 'react-client/src/ReactFlightClientConfig');
forks.set(
'react-devtools-shared/src/config/DevToolsFeatureFlags.default',
'react-devtools-feature-flags',
);

if (
otherRenderer.shortName !== serverRenderer &&
otherRenderer.shortName !== flightRenderer
) {
ignoredPaths.push(
`.*/packages/.*/forks/.*\\.${otherRenderer.shortName}.js`,
);
allForks.forEach(fork => {
if (!forks.has(fork)) {
ignoredPaths.push(`.*/packages/.*/${fork}`);
}
});

let moduleMappings = '';
forks.forEach((source, target) => {
moduleMappings += `module.name_mapper='${source.slice(
source.lastIndexOf('/') + 1,
)}' -> '${target}'\n`;
});

const config = configTemplate
.replace(
'%CI_MAX_WORKERS%\n',
// On CI, we seem to need to limit workers.
process.env.CI ? 'server.max_workers=4\n' : '',
)
.replace(
'%REACT_RENDERER_FLOW_OPTIONS%',
`
module.name_mapper='ReactFiberConfig$$' -> 'forks/ReactFiberConfig.${renderer}'
module.name_mapper='ReactServerStreamConfig$$' -> 'forks/ReactServerStreamConfig.${serverRenderer}'
module.name_mapper='ReactFizzConfig$$' -> 'forks/ReactFizzConfig.${serverRenderer}'
module.name_mapper='ReactFlightServerConfig$$' -> 'forks/ReactFlightServerConfig.${flightRenderer}'
module.name_mapper='ReactFlightClientConfig$$' -> 'forks/ReactFlightClientConfig.${flightRenderer}'
module.name_mapper='react-devtools-feature-flags' -> 'react-devtools-shared/src/config/DevToolsFeatureFlags.default'
`.trim(),
)
.replace('%REACT_RENDERER_FLOW_OPTIONS%', moduleMappings.trim())
.replace('%REACT_RENDERER_FLOW_IGNORES%', ignoredPaths.join('\n'));

const disclaimer = `
Expand Down
16 changes: 15 additions & 1 deletion scripts/jest/setupHostConfigs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const fs = require('fs');
const nodePath = require('path');
const inlinedHostConfigs = require('../shared/inlinedHostConfigs');

function resolveEntryFork(resolvedEntry, isFBBundle) {
Expand Down Expand Up @@ -117,7 +118,20 @@ function mockAllConfigs(rendererInfo) {
jest.mock(path, () => {
let idx = path.lastIndexOf('/');
let forkPath = path.slice(0, idx) + '/forks' + path.slice(idx);
return jest.requireActual(`${forkPath}.${rendererInfo.shortName}.js`);
let parts = rendererInfo.shortName.split('-');
while (parts.length) {
try {
const candidate = `${forkPath}.${parts.join('-')}.js`;
fs.statSync(nodePath.join(process.cwd(), 'packages', candidate));
return jest.requireActual(candidate);
} catch (error) {
// try without a part
}
parts.pop();
}
throw new Error(
`Expected to find a fork for ${path} but did not find one.`
);
});
});
}
Expand Down
Loading

0 comments on commit 5623f2a

Please sign in to comment.