diff --git a/babel.config.json b/babel.config.json new file mode 100644 index 0000000000000..85ccdd2ace0fa --- /dev/null +++ b/babel.config.json @@ -0,0 +1,4 @@ +{ + "presets": ["@babel/preset-env", "@babel/preset-react"] + } + \ No newline at end of file diff --git a/packages/react-reconciler/src/ReactFiberReconciler.js b/packages/react-reconciler/src/ReactFiberReconciler.js index 4c319a14569a1..5f0125aeb72ec 100644 --- a/packages/react-reconciler/src/ReactFiberReconciler.js +++ b/packages/react-reconciler/src/ReactFiberReconciler.js @@ -272,6 +272,47 @@ export function createContainer( ); } +// Import necessary utilities and modules +import { preloadModules as preloadModulesUtil } from './utils'; // Preload utility +export { preloadModulesUtil as preloadModules }; + +export async function createInstance(type, props, rootContainer, hostContext, internalHandle) { + // Dynamically import the module based on the type + const Module = await import(`my-module-path/${type}`); + const instance = new Module.default(props); + return instance; +} + +export function appendChild(parent, child) { + if (parent.appendChild) { + parent.appendChild(child); + } +} + +// Traverses the React component tree and preloads components dynamically +export async function preloadModules(tree) { + const components = new Set(); + traverseTree(tree, (type) => components.add(type)); + await Promise.all( + [...components].map((type) => import(`my-module-path/${type}`)) + ); +} + +// Helper function for traversing the component tree +function traverseTree(node, callback) { + if (!node) return; + if (typeof node.type === 'string') callback(node.type); + if (node.props && node.props.children) { + React.Children.forEach(node.props.children, (child) => + traverseTree(child, callback) + ); + } +} + +// Ensure the rest of the file remains unchanged +// (The remaining logic is retained as-is from your original implementation.) + + export function createHydrationContainer( initialChildren: ReactNodeList, // TODO: Remove `callback` when we delete legacy mode. diff --git a/packages/react-reconciler/src/__tests__/customRenderer.test.js b/packages/react-reconciler/src/__tests__/customRenderer.test.js new file mode 100644 index 0000000000000..7d15c7eed60d5 --- /dev/null +++ b/packages/react-reconciler/src/__tests__/customRenderer.test.js @@ -0,0 +1,26 @@ +import React from 'react'; +import { render, preloadModules } from '../ReactFiberReconciler'; + +const Box = () =>