Node.js Loader

πŸ‘¨β€πŸ’Ό Great job!
πŸ¦‰ So when Node.js imports the file, instead of this:
'use client'
import { createElement as h, useRef, useState } from 'react'
import { flushSync } from 'react-dom'

// ...

export function EditableText({ id, shipId, initialValue = '' }) {
	// ...
}
It's getting this:
import {registerClientReference} from "react-server-dom-esm/server";
export const EditableText = registerClientReference(function() {throw new Error("Attempted to call EditableText() from the server but EditableText is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.");},"file:///Users/kentcdodds/code/epicweb-dev/react-server-components/playground/ui/edit-text.js","EditableText");
And when I console.log(EditableText.toString()) in , I'm getting this:
function() {throw new Error("Attempted to call EditableText() from the server but EditableText is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.");}
Meaning if I try to call it in the RSC environment, I get an error.
(If you added the logs, you should have seen something like that in the console). That transformation is happening thanks to the loader you've registered.
The important elements here are:
  1. All exports of this module are wrapped in registerClientReference.
  2. The error message is thrown when the function is called on the server (because it should not be).
  3. The path to the file is passed as the second argument to registerClientReference.
  4. The name of the export is passed as the third argument to registerClientReference.
The registerClientReference function uses the path and name to generate a unique identifier for the client-side function. This identifier is used to generate a reference when rendering our server components that use these client components.
No build tool. Just built-in runtime features of Node.js. Cool huh!?
πŸ§β€β™‚οΈ I'm going to add some error boundaries in our app to get it ready for the work you're going to do. Check out my changes if you like.