Skip to content

Commit 53f1f7d

Browse files
add rsc webpack loader
1 parent a9999e4 commit 53f1f7d

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
@@ -7,7 +7,8 @@
77
"node": "./node_package/lib/ReactOnRails.node.js",
88
"rsc-server": "./node_package/lib/ReactOnRailsRSC.js",
99
"default": "./node_package/lib/ReactOnRails.js"
10-
}
10+
},
11+
"./RSCWebpackLoader": "./node_package/lib/RSCWebpackLoader.js"
1112
},
1213
"directories": {
1314
"doc": "docs"
@@ -60,7 +61,7 @@
6061
"prepack": "nps build.prepack",
6162
"prepare": "nps build.prepack",
6263
"prepublishOnly": "yarn run build",
63-
"build": "yarn run clean && yarn run tsc --declaration",
64+
"build": "yarn run clean && yarn run tsc --declaration && cp node_package/src/RSCWebpackLoader.js node_package/lib",
6465
"build-watch": "yarn run clean && yarn run tsc --watch",
6566
"lint": "nps eslint",
6667
"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)