Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating forking implementation to match against more general fork implementations #27205

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the browser build usedWithSSR should have always been false, but now that we can separate these renderers we can slightly optimize the build with this config difference

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dom-browser-esm now exists

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all dom based ReactFiberConfigs have been consolidated in ReactFiberConfig.dom.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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same impl as ReactFizzConfig.dom

This file was deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not webpack specific so renaming .dom-edge

File renamed without changes.
14 changes: 0 additions & 14 deletions packages/react-server/src/forks/ReactFizzConfig.dom-fb.js
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same impl as ReactFizzConfig.dom

This file was deleted.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same impl as ReactFizzConfig.dom-node

This file was deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same impl as ReactFizzConfig.dom-node

This file was deleted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Git is showing this as a rename but really we deleted the dom-node-webpack to pickup the dom-node version and we added dom-browser-esm for that new renderer

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stream configs don't depend on webpack vs esm. using dom-node to generalize

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