diff --git a/packages/react-client/src/forks/ReactFlightClientHostConfig.dom-next.js b/packages/react-client/src/forks/ReactFlightClientHostConfig.dom-next.js new file mode 100644 index 0000000000000..59e1171c144ea --- /dev/null +++ b/packages/react-client/src/forks/ReactFlightClientHostConfig.dom-next.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its 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/ReactFlightClientHostConfigBrowser'; +export * from 'react-client/src/ReactFlightClientHostConfigStream'; +export * from 'react-server-dom-webpack/src/ReactFlightClientWebpackBundlerConfig'; diff --git a/packages/react-dom/src/server/ReactDOMFizzServerNext.js b/packages/react-dom/src/server/ReactDOMFizzServerNext.js new file mode 100644 index 0000000000000..83e52d38a2e4b --- /dev/null +++ b/packages/react-dom/src/server/ReactDOMFizzServerNext.js @@ -0,0 +1,86 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import type {ReactNodeList} from 'shared/ReactTypes'; +import type {Destination} from 'react-server/src/ReactServerStreamConfigNext'; + +import ReactVersion from 'shared/ReactVersion'; + +import { + createRequest, + startWork, + startFlowing, + abort, +} from 'react-server/src/ReactFizzServer'; + +import { + createResponseState, + createRootFormatContext, +} from './ReactDOMServerFormatConfig'; + +type NextStreamSource = { + start: (controller: Destination) => void, + pull: (controller: Destination) => void, + cancel: (reason: mixed) => void, +}; + +type Options = {| + identifierPrefix?: string, + namespaceURI?: string, + nonce?: string, + bootstrapScriptContent?: string, + bootstrapScripts?: Array, + bootstrapModules?: Array, + progressiveChunkSize?: number, + signal?: AbortSignal, + onCompleteShell?: () => void, + onCompleteAll?: () => void, + onError?: (error: mixed) => void, +|}; + +function renderToNextStream( + children: ReactNodeList, + options?: Options, +): NextStreamSource { + const request = createRequest( + children, + createResponseState( + options ? options.identifierPrefix : undefined, + options ? options.nonce : undefined, + options ? options.bootstrapScriptContent : undefined, + options ? options.bootstrapScripts : undefined, + options ? options.bootstrapModules : undefined, + ), + createRootFormatContext(options ? options.namespaceURI : undefined), + options ? options.progressiveChunkSize : undefined, + options ? options.onError : undefined, + options ? options.onCompleteAll : undefined, + options ? options.onCompleteShell : undefined, + ); + if (options && options.signal) { + const signal = options.signal; + const listener = () => { + abort(request); + signal.removeEventListener('abort', listener); + }; + signal.addEventListener('abort', listener); + } + const stream = { + start(controller) { + startWork(request); + }, + pull(controller) { + startFlowing(request, controller); + }, + cancel(reason) {}, + }; + return stream; +} + +export {renderToNextStream, ReactVersion as version}; diff --git a/packages/react-reconciler/src/forks/ReactFiberHostConfig.dom-next.js b/packages/react-reconciler/src/forks/ReactFiberHostConfig.dom-next.js new file mode 100644 index 0000000000000..d830c8501be27 --- /dev/null +++ b/packages/react-reconciler/src/forks/ReactFiberHostConfig.dom-next.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its 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-dom/src/client/ReactDOMHostConfig'; diff --git a/packages/react-server-dom-webpack/src/ReactFlightDOMServerNext.js b/packages/react-server-dom-webpack/src/ReactFlightDOMServerNext.js new file mode 100644 index 0000000000000..5825ae5a83b43 --- /dev/null +++ b/packages/react-server-dom-webpack/src/ReactFlightDOMServerNext.js @@ -0,0 +1,52 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import type {ReactModel} from 'react-server/src/ReactFlightServer'; +import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig'; +import type {Destination} from 'react-server/src/ReactServerStreamConfigNext'; + +import { + createRequest, + startWork, + startFlowing, +} from 'react-server/src/ReactFlightServer'; + +type NextStreamSource = { + start: (controller: Destination) => void, + pull: (controller: Destination) => void, + cancel: (reason: mixed) => void, +}; + +type Options = { + onError?: (error: mixed) => void, +}; + +function renderToNextStream( + model: ReactModel, + webpackMap: BundlerConfig, + options?: Options, +): NextStreamSource { + const request = createRequest( + model, + webpackMap, + options ? options.onError : undefined, + ); + const stream = { + start(controller) { + startWork(request); + }, + pull(controller) { + startFlowing(request, controller); + }, + cancel(reason) {}, + }; + return stream; +} + +export {renderToNextStream}; diff --git a/packages/react-server-dom-webpack/writer.next.server.js b/packages/react-server-dom-webpack/writer.next.server.js new file mode 100644 index 0000000000000..f50684c5d9afc --- /dev/null +++ b/packages/react-server-dom-webpack/writer.next.server.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its 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 './src/ReactFlightDOMServerNext.js'; diff --git a/packages/react-server/src/ReactServerStreamConfigNext.js b/packages/react-server/src/ReactServerStreamConfigNext.js new file mode 100644 index 0000000000000..946a026e79c40 --- /dev/null +++ b/packages/react-server/src/ReactServerStreamConfigNext.js @@ -0,0 +1,63 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +declare function __next_scheduleReactServerWork(callback: () => void): void; + +export type Destination = { + flush: () => void, + buffer: (buffer: boolean) => void, + close: (error: mixed) => void, + write: (chunk: Uint8Array) => void, + desiredSize: number, +}; + +export type PrecomputedChunk = Uint8Array; +export type Chunk = Uint8Array; + +export function scheduleWork(callback: () => void) { + __next_scheduleReactServerWork(callback); +} + +export function flushBuffered(destination: Destination) { + destination.flush(); +} + +export function beginWriting(destination: Destination) { + destination.buffer(true); +} + +export function writeChunk( + destination: Destination, + chunk: Chunk | PrecomputedChunk, +): boolean { + destination.write(chunk); + return destination.desiredSize > 0; +} + +export function completeWriting(destination: Destination) { + destination.buffer(false); +} + +export function close(destination: Destination) { + destination.close(); +} + +const textEncoder = new TextEncoder(); + +export function stringToChunk(content: string): Chunk { + return textEncoder.encode(content); +} + +export function stringToPrecomputedChunk(content: string): PrecomputedChunk { + return textEncoder.encode(content); +} + +export function closeWithError(destination: Destination, error: mixed): void { + destination.close(error); +} diff --git a/packages/react-server/src/forks/ReactFlightServerConfig.dom-next.js b/packages/react-server/src/forks/ReactFlightServerConfig.dom-next.js new file mode 100644 index 0000000000000..07b5f345f1936 --- /dev/null +++ b/packages/react-server/src/forks/ReactFlightServerConfig.dom-next.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Facebook, Inc. and its 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 '../ReactFlightServerConfigStream'; +export * from 'react-server-dom-webpack/src/ReactFlightServerWebpackBundlerConfig'; diff --git a/packages/react-server/src/forks/ReactServerFormatConfig.dom-next.js b/packages/react-server/src/forks/ReactServerFormatConfig.dom-next.js new file mode 100644 index 0000000000000..c6e482efeb60c --- /dev/null +++ b/packages/react-server/src/forks/ReactServerFormatConfig.dom-next.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its 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-dom/src/server/ReactDOMServerFormatConfig'; diff --git a/packages/react-server/src/forks/ReactServerStreamConfig.dom-next.js b/packages/react-server/src/forks/ReactServerStreamConfig.dom-next.js new file mode 100644 index 0000000000000..4ea04c9b015d3 --- /dev/null +++ b/packages/react-server/src/forks/ReactServerStreamConfig.dom-next.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its 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 '../ReactServerStreamConfigNext'; diff --git a/scripts/rollup/bundles.js b/scripts/rollup/bundles.js index 8b12e8550e365..bb2438dba143b 100644 --- a/scripts/rollup/bundles.js +++ b/scripts/rollup/bundles.js @@ -313,6 +313,14 @@ const bundles = [ wrapWithModuleBoundaries: false, externals: ['react'], }, + { + bundleTypes: [NODE_DEV, NODE_PROD], + moduleType: RENDERER, + entry: 'react-dom/src/server/ReactDOMFizzServerNext', + name: 'react-dom-server-next', + global: 'ReactDOMServer', + externals: ['react'], + }, { bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [], moduleType: RENDERER, diff --git a/scripts/shared/inlinedHostConfigs.js b/scripts/shared/inlinedHostConfigs.js index f6d349502f6ba..62b3a2efb1d34 100644 --- a/scripts/shared/inlinedHostConfigs.js +++ b/scripts/shared/inlinedHostConfigs.js @@ -66,6 +66,22 @@ module.exports = [ isFlowTyped: true, isServerSupported: true, }, + { + shortName: 'dom-next', + entryPoints: [ + 'react-server-dom-webpack/writer.next.server', + 'react-server-dom-webpack', + 'react-dom/src/server/ReactDOMFizzServerNext', // react-dom/server + ], + paths: [ + 'react-dom', + 'react-server-dom-webpack', + 'react-dom/src/server/ReactDOMFizzServerNext.js', // react-dom/server.next + 'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations. + ], + isFlowTyped: true, + isServerSupported: true, + }, { shortName: 'art', entryPoints: ['react-art'],