From 666dc98f17e3f1f16687a1973740860153d64769 Mon Sep 17 00:00:00 2001 From: Abanoub Ghadban Date: Thu, 16 Jan 2025 15:23:38 +0200 Subject: [PATCH] add rsc webpack loader --- node_package/src/RSCWebpackLoader.js | 23 +++++++++++++++++++ .../types/react-server-dom-webpack.d.ts | 17 ++++++++++++++ package.json | 5 ++-- tsconfig.json | 3 ++- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 node_package/src/RSCWebpackLoader.js create mode 100644 node_package/types/react-server-dom-webpack.d.ts diff --git a/node_package/src/RSCWebpackLoader.js b/node_package/src/RSCWebpackLoader.js new file mode 100644 index 000000000..478dd5665 --- /dev/null +++ b/node_package/src/RSCWebpackLoader.js @@ -0,0 +1,23 @@ +const { pathToFileURL } = require('url'); + +const RSCWebpackLoader = async function Loader(source, sourceMap) { + // Mark loader as async since we're doing async operations + const callback = this.async(); + + try { + // Convert file path to URL format + const fileUrl = pathToFileURL(this.resourcePath).href; + + const { load } = await import('react-server-dom-webpack/node-loader'); + const result = await load(fileUrl, null, async () => ({ + format: 'module', + source, + })); + + callback(null, result.source, sourceMap); + } catch (error) { + callback(error); + } +}; + +module.exports = RSCWebpackLoader; diff --git a/node_package/types/react-server-dom-webpack.d.ts b/node_package/types/react-server-dom-webpack.d.ts new file mode 100644 index 000000000..652fbd9ea --- /dev/null +++ b/node_package/types/react-server-dom-webpack.d.ts @@ -0,0 +1,17 @@ +declare module 'react-server-dom-webpack/node-loader' { + interface LoadOptions { + format: 'module'; + source: string; + } + + interface LoadResult { + source: string; + } + + // eslint-disable-next-line import/prefer-default-export + export function load( + url: string, + context: null | object, + defaultLoad: () => Promise + ): Promise; +} diff --git a/package.json b/package.json index 23e2d609f..b56ac04c8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ ".": { "rsc-server": "./node_package/lib/ReactOnRailsRSC.js", "default": "./node_package/lib/ReactOnRails.js" - } + }, + "./RSCWebpackLoader": "./node_package/lib/RSCWebpackLoader.js" }, "directories": { "doc": "docs" @@ -70,7 +71,7 @@ "prepack": "nps build.prepack", "prepare": "nps build.prepack", "prepublishOnly": "yarn run build", - "build": "yarn run clean && yarn run tsc --declaration", + "build": "yarn run clean && yarn run tsc --declaration && cp node_package/src/RSCWebpackLoader.js node_package/lib", "build-watch": "yarn run clean && yarn run tsc --watch", "lint": "nps eslint", "check": "yarn run lint && yarn run test && yarn run type-check", diff --git a/tsconfig.json b/tsconfig.json index 13fa3eb7a..db2a73dae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,6 @@ "incremental": true, "target": "es5" }, - "include": ["node_package/src/**/*"] + "include": ["node_package/src/**/*"], + "exclude": ["node_package/src/RSCWebpackLoader.js"] }