Skip to content

Commit b24ee8a

Browse files
add rsc webpack loader
1 parent 42b263e commit b24ee8a

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { pathToFileURL } = require('url');
2+
3+
const RSCWebpackLoader = async function Loader(source, sourceMap) {
4+
// Mark loader as async since we're doing async operations
5+
const callback = this.async();
6+
7+
try {
8+
// Convert file path to URL format
9+
const fileUrl = pathToFileURL(this.resourcePath).href;
10+
11+
const { load } = await import('react-server-dom-webpack/node-loader');
12+
const result = await load(fileUrl, null, async () => ({
13+
format: 'module',
14+
source,
15+
}));
16+
17+
callback(null, result.source, sourceMap);
18+
} catch (error) {
19+
callback(error);
20+
}
21+
};
22+
23+
module.exports = RSCWebpackLoader;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare module 'react-server-dom-webpack/node-loader' {
2+
interface LoadOptions {
3+
format: 'module';
4+
source: string;
5+
}
6+
7+
interface LoadResult {
8+
source: string;
9+
}
10+
11+
// eslint-disable-next-line import/prefer-default-export
12+
export function load(
13+
url: string,
14+
context: null | object,
15+
defaultLoad: () => Promise<LoadOptions>
16+
): Promise<LoadResult>;
17+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"rsc-server": "./node_package/lib/ReactOnRailsRSC.js",
1010
"default": "./node_package/lib/ReactOnRails.full.js"
1111
},
12-
"./client": "./node_package/lib/ReactOnRails.client.js"
12+
"./client": "./node_package/lib/ReactOnRails.client.js",
13+
"./RSCWebpackLoader": "./node_package/lib/RSCWebpackLoader.js"
1314
},
1415
"directories": {
1516
"doc": "docs"
@@ -64,7 +65,7 @@
6465
"prepack": "nps build.prepack",
6566
"prepare": "nps build.prepack",
6667
"prepublishOnly": "yarn run build",
67-
"build": "yarn run clean && yarn run tsc --declaration",
68+
"build": "yarn run clean && yarn run tsc --declaration && cp node_package/src/RSCWebpackLoader.js node_package/lib",
6869
"build-watch": "yarn run clean && yarn run tsc --watch",
6970
"lint": "nps eslint",
7071
"check": "yarn run lint && yarn run test && yarn run type-check",

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"incremental": true,
1414
"target": "es2020"
1515
},
16-
"include": ["node_package/src/**/*"]
16+
"include": ["node_package/src/**/*"],
17+
"exclude": ["node_package/src/RSCWebpackLoader.js"]
1718
}

0 commit comments

Comments
 (0)