Skip to content

Commit

Permalink
Convert RSCWebpackLoader to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyr committed Feb 10, 2025
1 parent a3ad230 commit 8f90edc
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 36 deletions.
24 changes: 0 additions & 24 deletions node_package/src/RSCWebpackLoader.js

This file was deleted.

25 changes: 25 additions & 0 deletions node_package/src/RSCWebpackLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { pathToFileURL } from 'url';
import { LoaderDefinition } from 'webpack';

const RSCWebpackLoader: LoaderDefinition = function RSCWebpackLoader(source, sourceMap) {
// Mark loader as async since we're doing async operations
const callback = this.async();

(async () => {
// Convert file path to URL format
const fileUrl = pathToFileURL(this.resourcePath).href;

// eslint-disable-next-line no-new-func
const { load } = await new Function('return import("react-server-dom-webpack/node-loader")')() as
typeof import('react-server-dom-webpack/node-loader');
return load(fileUrl, null, async () => ({
format: 'module',
source,
}));
})().then(
result => callback(null, result.source, sourceMap),
error => callback(error),
);
};

export default RSCWebpackLoader;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"react-server-dom-webpack": "18.3.0-canary-670811593-20240322",
"redux": "^4.2.1",
"ts-jest": "^29.2.5",
"typescript": "^5.6.2"
"typescript": "^5.6.2",
"webpack": "^5.97.1"
},
"dependencies": {},
"peerDependencies": {
Expand All @@ -71,7 +72,7 @@
"prepack": "nps build.prepack",
"prepare": "nps build.prepack",
"prepublishOnly": "yarn run build",
"build": "yarn run clean && yarn run tsc --declaration && cp node_package/src/RSCWebpackLoader.js node_package/lib",
"build": "yarn run clean && yarn run tsc --declaration",
"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: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"target": "es5",
"typeRoots": ["./node_modules/@types", "./node_package/types"]
},
"include": ["node_package/src/**/*", "node_package/types/**/*"],
"exclude": ["node_package/src/RSCWebpackLoader.js"]
"include": ["node_package/src/**/*", "node_package/types/**/*"]
}
Loading

0 comments on commit 8f90edc

Please sign in to comment.