Skip to content

Commit 666dc98

Browse files
add rsc webpack loader
1 parent d67e78d commit 666dc98

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
@@ -6,7 +6,8 @@
66
".": {
77
"rsc-server": "./node_package/lib/ReactOnRailsRSC.js",
88
"default": "./node_package/lib/ReactOnRails.js"
9-
}
9+
},
10+
"./RSCWebpackLoader": "./node_package/lib/RSCWebpackLoader.js"
1011
},
1112
"directories": {
1213
"doc": "docs"
@@ -70,7 +71,7 @@
7071
"prepack": "nps build.prepack",
7172
"prepare": "nps build.prepack",
7273
"prepublishOnly": "yarn run build",
73-
"build": "yarn run clean && yarn run tsc --declaration",
74+
"build": "yarn run clean && yarn run tsc --declaration && cp node_package/src/RSCWebpackLoader.js node_package/lib",
7475
"build-watch": "yarn run clean && yarn run tsc --watch",
7576
"lint": "nps eslint",
7677
"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
@@ -11,5 +11,6 @@
1111
"incremental": true,
1212
"target": "es5"
1313
},
14-
"include": ["node_package/src/**/*"]
14+
"include": ["node_package/src/**/*"],
15+
"exclude": ["node_package/src/RSCWebpackLoader.js"]
1516
}

0 commit comments

Comments
 (0)