Files
react/packages/react-devtools-shell/webpack-server.js
Josh Story cb151849e1 [react-dom] move all client code to react-dom/client (#28271)
This PR reorganizes the `react-dom` entrypoint to only pull in code that
is environment agnostic. Previously if you required anything from this
entrypoint in any environment the entire client reconciler was loaded.
In a prior release we added a server rendering stub which you could
alias in server environments to omit this unecessary code. After landing
this change this entrypoint should not load any environment specific
code.

While a few APIs are truly client (browser) only such as createRoot and
hydrateRoot many of the APIs you import from this package are only
useful in the browser but could concievably be imported in shared code
(components running in Fizz or shared components as part of an RSC app).
To avoid making these require opting into the client bundle we are
keeping them in the `react-dom` entrypoint and changing their
implementation so that in environments where they are not particularly
useful they do something benign and expected.

#### Removed APIs
The following APIs are being removed in the next major. Largely they
have all been deprecated already and are part of legacy rendering modes
where concurrent features of React are not available
* `render`
* `hydrate`
* `findDOMNode`
* `unmountComponentAtNode`
* `unstable_createEventHandle`
* `unstable_renderSubtreeIntoContainer`
* `unstable_runWithPrioirty`

#### moved Client APIs
These APIs were available on both `react-dom` (with a warning) and
`react-dom/client`. After this change they are only available on
`react-dom/client`
* `createRoot`
* `hydrateRoot`

#### retained APIs
These APIs still exist on the `react-dom` entrypoint but have normalized
behavior depending on which renderers are currently in scope
* `flushSync`: will execute the function (if provided) inside the
flushSync implemention of FlightServer, Fizz, and Fiber DOM renderers.
* `unstable_batchedUpdates`: This is a noop in concurrent mode because
it is now the only supported behavior because there is no legacy
rendering mode
* `createPortal`: This just produces an object. It can be called from
anywhere but since you will probably not have a handle on a DOM node to
pass to it it will likely warn in environments other than the browser
* preloading APIS such as `preload`: These methods will execute the
preload across all renderers currently in scope. Since we resolve the
Request object on the server using AsyncLocalStorage or the current
function stack in practice only one renderer should act upon the
preload.

In addition to these changes the server rendering stub now just rexports
everything from `react-dom`. In a future minor we will add a warning
when using the stub and in the next major we will remove the stub
altogether
2024-04-24 08:50:32 -07:00

235 lines
6.5 KiB
JavaScript

const {resolve} = require('path');
const Webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const {
DARK_MODE_DIMMED_WARNING_COLOR,
DARK_MODE_DIMMED_ERROR_COLOR,
DARK_MODE_DIMMED_LOG_COLOR,
LIGHT_MODE_DIMMED_WARNING_COLOR,
LIGHT_MODE_DIMMED_ERROR_COLOR,
LIGHT_MODE_DIMMED_LOG_COLOR,
GITHUB_URL,
getVersionString,
} = require('react-devtools-extensions/utils');
const {resolveFeatureFlags} = require('react-devtools-shared/buildUtils');
const semver = require('semver');
const {SUCCESSFUL_COMPILATION_MESSAGE} = require('./constants');
const {ReactVersion: currentReactVersion} = require('../../ReactVersions');
const NODE_ENV = process.env.NODE_ENV;
if (!NODE_ENV) {
console.error('NODE_ENV not set');
process.exit(1);
}
const EDITOR_URL = process.env.EDITOR_URL || null;
const builtModulesDir = resolve(
__dirname,
'..',
'..',
'build',
'oss-experimental',
);
const __DEV__ = NODE_ENV === 'development';
const DEVTOOLS_VERSION = getVersionString();
// If the React version isn't set, we will use the
// current React version instead. Likewise if the
// React version isnt' set, we'll use the build folder
// for both React DevTools and React
const REACT_VERSION = process.env.REACT_VERSION
? semver.coerce(process.env.REACT_VERSION).version
: currentReactVersion;
const E2E_APP_BUILD_DIR = process.env.REACT_VERSION
? resolve(__dirname, '..', '..', 'build-regression', 'node_modules')
: builtModulesDir;
const makeConfig = (entry, alias) => ({
mode: __DEV__ ? 'development' : 'production',
devtool: __DEV__ ? 'cheap-source-map' : 'source-map',
stats: 'normal',
entry,
output: {
publicPath: '/dist/',
},
node: {
global: false,
},
resolve: {
alias,
},
optimization: {
minimize: false,
},
plugins: [
new Webpack.ProvidePlugin({
process: 'process/browser',
}),
new Webpack.DefinePlugin({
__DEV__,
__EXPERIMENTAL__: true,
__EXTENSION__: false,
__PROFILE__: false,
__TEST__: NODE_ENV === 'test',
'process.env.GITHUB_URL': `"${GITHUB_URL}"`,
'process.env.EDITOR_URL': EDITOR_URL != null ? `"${EDITOR_URL}"` : null,
'process.env.DEVTOOLS_PACKAGE': `"react-devtools-shell"`,
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
'process.env.DARK_MODE_DIMMED_WARNING_COLOR': `"${DARK_MODE_DIMMED_WARNING_COLOR}"`,
'process.env.DARK_MODE_DIMMED_ERROR_COLOR': `"${DARK_MODE_DIMMED_ERROR_COLOR}"`,
'process.env.DARK_MODE_DIMMED_LOG_COLOR': `"${DARK_MODE_DIMMED_LOG_COLOR}"`,
'process.env.LIGHT_MODE_DIMMED_WARNING_COLOR': `"${LIGHT_MODE_DIMMED_WARNING_COLOR}"`,
'process.env.LIGHT_MODE_DIMMED_ERROR_COLOR': `"${LIGHT_MODE_DIMMED_ERROR_COLOR}"`,
'process.env.LIGHT_MODE_DIMMED_LOG_COLOR': `"${LIGHT_MODE_DIMMED_LOG_COLOR}"`,
'process.env.E2E_APP_REACT_VERSION': `"${REACT_VERSION}"`,
}),
],
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
configFile: resolve(
__dirname,
'..',
'react-devtools-shared',
'babel.config.js',
),
},
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
localIdentName: '[local]',
},
},
],
},
],
},
});
const clientAsSeparateBuild = semver.gte(REACT_VERSION, '19.0.0');
const app = makeConfig(
{
'app-index': './src/app/index.js',
'app-devtools': './src/app/devtools.js',
'e2e-app': './src/e2e/app.js',
'e2e-devtools': './src/e2e/devtools.js',
'e2e-devtools-regression': './src/e2e-regression/devtools.js',
'multi-left': './src/multi/left.js',
'multi-devtools': './src/multi/devtools.js',
'multi-right': './src/multi/right.js',
'e2e-regression': './src/e2e-regression/app.js',
'perf-regression-app': './src/perf-regression/app.js',
'perf-regression-devtools': './src/perf-regression/devtools.js',
},
{
react: resolve(builtModulesDir, 'react'),
'react-debug-tools': resolve(builtModulesDir, 'react-debug-tools'),
'react-devtools-feature-flags': resolveFeatureFlags('shell'),
'react-dom/client': resolve(
builtModulesDir,
clientAsSeparateBuild ? 'react-dom/unstable_testing' : 'react-dom/client',
),
'react-dom': resolve(
builtModulesDir,
clientAsSeparateBuild ? 'react-dom' : 'react-dom/unstable_testing',
),
'react-is': resolve(builtModulesDir, 'react-is'),
scheduler: resolve(builtModulesDir, 'scheduler'),
},
);
// Prior to React 18, we use ReactDOM.render rather than
// createRoot.
// We also use a separate build folder to build the React App
// so that we can test the current DevTools against older version of React
const e2eRegressionApp = semver.lt(REACT_VERSION, '18.0.0')
? makeConfig(
{
'e2e-app-regression': './src/e2e-regression/app-legacy.js',
},
{
react: resolve(E2E_APP_BUILD_DIR, 'react'),
'react-dom': resolve(E2E_APP_BUILD_DIR, 'react-dom'),
...(semver.satisfies(REACT_VERSION, '16.5')
? {schedule: resolve(E2E_APP_BUILD_DIR, 'schedule')}
: {scheduler: resolve(E2E_APP_BUILD_DIR, 'scheduler')}),
},
)
: makeConfig(
{
'e2e-app-regression': './src/e2e-regression/app.js',
},
{
react: resolve(E2E_APP_BUILD_DIR, 'react'),
'react-dom': resolve(E2E_APP_BUILD_DIR, 'react-dom'),
'react-dom/client': resolve(E2E_APP_BUILD_DIR, 'react-dom/client'),
scheduler: resolve(E2E_APP_BUILD_DIR, 'scheduler'),
},
);
const appCompiler = Webpack(app);
const appServer = new WebpackDevServer(
{
hot: true,
open: true,
port: 8080,
client: {
logging: 'warn',
},
static: {
directory: __dirname,
publicPath: '/',
},
},
appCompiler,
);
const e2eRegressionAppCompiler = Webpack(e2eRegressionApp);
const e2eRegressionAppServer = new WebpackDevServer(
{
port: 8181,
client: {
logging: 'warn',
},
static: {
publicPath: '/dist/',
},
headers: {
'Access-Control-Allow-Origin': '*',
},
},
e2eRegressionAppCompiler,
);
const runServer = async () => {
console.log('Starting server...');
appServer.compiler.hooks.done.tap('done', () =>
console.log(SUCCESSFUL_COMPILATION_MESSAGE),
);
await e2eRegressionAppServer.start();
await appServer.start();
};
runServer();