Skip to content

Commit 6235711

Browse files
refactor(dts-plugin): replace Koa dev server with native HTTP (#4419)
Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Zack Jackson <ScriptedAlchemy@users.noreply.github.com>
1 parent 3be31f3 commit 6235711

File tree

15 files changed

+77
-108
lines changed

15 files changed

+77
-108
lines changed

.changeset/bright-squids-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
Replace the Koa-based dev types server with a native Node HTTP server to reduce dependencies and keep type generation behavior unchanged.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pluginManagement { includeBuild("../../../node_modules/@react-native/gradle-plugin") }
1+
pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") }
22
plugins { id("com.facebook.react.settings") }
33
extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand(['npx', 'rnef', 'config', '-p', 'android']) }
44
rootProject.name = "MFExampleHost"
55
include ':app'
6-
includeBuild('../../../node_modules/@react-native/gradle-plugin')
6+
includeBuild('../node_modules/@react-native/gradle-plugin')

apps/metro-example-host/metro.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module.exports = withModuleFederation(
4747
lodash: {
4848
singleton: false,
4949
eager: false,
50-
requiredVersion: '4.16.6',
51-
version: '4.16.6',
50+
requiredVersion: '4.17.23',
51+
version: '4.17.23',
5252
},
5353
},
5454
shareStrategy: 'loaded-first',

apps/metro-example-host/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@module-federation/runtime": "workspace:*",
3535
"@react-native/babel-preset": "0.80.0",
3636
"@react-native/eslint-config": "0.80.0",
37+
"@react-native/gradle-plugin": "0.80.0",
3738
"@react-native/metro-config": "0.80.0",
3839
"@react-native/typescript-config": "0.80.0",
3940
"@rnef/cli": "^0.7.25",

apps/metro-example-mini/metro.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = withModuleFederation(
4848
lodash: {
4949
singleton: false,
5050
eager: false,
51-
version: '4.17.21',
51+
version: '4.17.23',
5252
},
5353
},
5454
shareStrategy: 'version-first',

apps/metro-example-nested-mini/metro.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ module.exports = withModuleFederation(
5151
lodash: {
5252
singleton: false,
5353
eager: false,
54-
requiredVersion: '4.17.21',
55-
version: '4.17.21',
54+
requiredVersion: '4.17.23',
55+
version: '4.17.23',
5656
},
5757
},
5858
shareStrategy: 'version-first',

packages/dts-plugin/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"chalk": "3.0.0",
5959
"fs-extra": "9.1.0",
6060
"isomorphic-ws": "5.0.0",
61-
"koa": "3.0.3",
6261
"lodash.clonedeepwith": "4.5.0",
6362
"log4js": "6.9.1",
6463
"node-schedule": "2.1.1",
@@ -67,7 +66,6 @@
6766
},
6867
"devDependencies": {
6968
"@module-federation/runtime": "workspace:*",
70-
"@types/koa": "2.15.0",
7169
"@types/node-schedule": "2.1.7",
7270
"@types/ws": "8.5.12",
7371
"@vue/tsconfig": "^0.7.0",

packages/dts-plugin/src/core/lib/typeScriptCompiler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ describe('typeScriptCompiler', () => {
449449
name: 'constant.d.ts',
450450
},
451451
{
452-
name: 'createKoaServer.d.ts',
452+
name: 'createHttpServer.d.ts',
453453
},
454454
{
455455
name: 'createWebsocket.d.ts',

packages/dts-plugin/src/dev-worker/forkDevWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
UpdateCallbackOptions,
1515
UpdateKind,
1616
ModuleFederationDevServer,
17-
createKoaServer,
17+
createHttpServer,
1818
fileLog,
1919
getIPV4,
2020
DEFAULT_TAR_NAME,
@@ -114,7 +114,7 @@ export async function forkDevWorker(
114114
const mfTypesZipPath = retrieveTypesZipPath(mfTypesPath, remoteOptions);
115115

116116
await Promise.all([
117-
createKoaServer({
117+
createHttpServer({
118118
typeTarPath: mfTypesZipPath,
119119
}).then((res) => {
120120
serverAddress = res.serverAddress;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import http from 'http';
2+
import fs from 'fs-extra';
3+
import { getFreePort, getIPV4 } from './utils';
4+
import { DEFAULT_TAR_NAME } from './constant';
5+
6+
interface CreateHttpServerOptions {
7+
typeTarPath: string;
8+
}
9+
10+
export async function createHttpServer(
11+
options: CreateHttpServerOptions,
12+
): Promise<{
13+
server: http.Server;
14+
serverAddress: string;
15+
}> {
16+
const { typeTarPath } = options;
17+
const freeport = await getFreePort();
18+
const server = http.createServer((req, res) => {
19+
const requestPath = req.url?.split('?')[0] ?? '/';
20+
if (requestPath === `/${DEFAULT_TAR_NAME}`) {
21+
res.statusCode = 200;
22+
res.setHeader('Content-Type', 'application/x-gzip');
23+
if (req.method === 'HEAD') {
24+
res.end();
25+
return;
26+
}
27+
const stream = fs.createReadStream(typeTarPath);
28+
stream.on('error', () => {
29+
if (!res.headersSent) {
30+
res.statusCode = 500;
31+
}
32+
res.end();
33+
});
34+
res.on('close', () => {
35+
stream.destroy();
36+
});
37+
stream.pipe(res);
38+
return;
39+
}
40+
res.statusCode = 404;
41+
res.end();
42+
});
43+
44+
server.listen(freeport);
45+
46+
return {
47+
server,
48+
serverAddress: `http://${getIPV4()}:${freeport}`,
49+
};
50+
}

0 commit comments

Comments
 (0)