Skip to content

Commit

Permalink
add rsc webpack loader
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Jan 16, 2025
1 parent d67e78d commit 666dc98
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
23 changes: 23 additions & 0 deletions node_package/src/RSCWebpackLoader.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions node_package/types/react-server-dom-webpack.d.ts
Original file line number Diff line number Diff line change
@@ -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<LoadOptions>
): Promise<LoadResult>;
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"incremental": true,
"target": "es5"
},
"include": ["node_package/src/**/*"]
"include": ["node_package/src/**/*"],
"exclude": ["node_package/src/RSCWebpackLoader.js"]
}

0 comments on commit 666dc98

Please sign in to comment.