Skip to content

Commit 20523d3

Browse files
committed
fix: allow type export on mts
closes #464
1 parent 56e7a11 commit 20523d3

14 files changed

Lines changed: 70 additions & 11 deletions

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@
5050
"build:version": "node ./scripts/replace_urllib_version.js",
5151
"build:cjs:test": "cd test/cjs && rm -rf node_modules && npm link ../.. && node index.js",
5252
"build:esm:test": "cd test/esm && rm -rf node_modules && npm link ../.. && node index.js",
53-
"build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run test-tsc",
54-
"test-tsc": "tsc -p ./test/fixtures/ts/tsconfig.json",
53+
"build:mts:test": "cd test/mts && rm -rf node_modules && npm link ../.. && tsc",
54+
"build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run build:mts:test && npm run test-tsc",
55+
"test-tsc": "npm run test-tsc:cjs",
56+
"test-tsc:cjs": "tsc -p ./test/fixtures/ts/tsconfig.json",
57+
"test-tsc:esm": "tsc -p ./test/fixtures/ts-esm/tsconfig.json",
5558
"test": "npm run lint && vitest run",
5659
"test-keepalive": "cross-env TEST_KEEPALIVE_COUNT=50 vitest run --test-timeout 180000 keep-alive-header.test.ts",
5760
"cov": "vitest run --coverage",
@@ -82,6 +85,8 @@
8285
"@types/selfsigned": "^2.0.1",
8386
"@types/tar-stream": "^2.2.2",
8487
"@vitest/coverage-v8": "^0.32.0",
88+
"@tsconfig/node18": "^18.2.1",
89+
"@tsconfig/strictest": "^2.0.2",
8590
"busboy": "^1.6.0",
8691
"cross-env": "^7.0.3",
8792
"eslint": "^8.25.0",

scripts/esm_import_fix.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ async function main() {
77
const root = path.join(process.cwd(), 'src/esm');
88
const files = await fs.readdir(root);
99
for (const name of files) {
10-
if (!name.endsWith('.js')) continue;
10+
if (!name.endsWith('.js') && !name.endsWith('.d.ts')) continue;
1111
const file = path.join(root, name);
1212
const content = await fs.readFile(file, 'utf-8');
1313
// replace "from './HttpClient'" to "from './HttpClient.js'"
1414
const newContent = content.replace(/ from \'(\.\/[^\.\']+?)\'/g, (match, p1) => {
1515
const after = ` from '${p1}.js'`;
1616
console.log('[%s] %s => %s', file, match, after);
1717
return after;
18+
}).replace(/import\(\"(\.\/[^\.\"]+?)\"\)/g, (match, p1) => {
19+
// import("./Response") => import("./Response.js")
20+
const after = `import("${p1}.js")`;
21+
console.log('[%s] %s => %s', file, match, after);
22+
return after;
1823
});
1924
if (newContent !== content) {
2025
await fs.writeFile(file, newContent);

src/HttpAgent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import dns from 'node:dns';
22
import { LookupFunction, isIP } from 'node:net';
33
import {
44
Agent,
5+
Dispatcher,
6+
buildConnector,
57
} from 'undici';
6-
import type Dispatcher from 'undici/types/dispatcher';
7-
import type buildConnector from 'undici/types/connector';
88

99
export type CheckAddressFunction = (ip: string, family: number | string) => boolean;
1010

src/Request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Readable, Writable } from 'node:stream';
22
import type { IncomingHttpHeaders } from 'node:http';
3-
import type Dispatcher from 'undici/types/dispatcher';
3+
import type { Dispatcher } from 'undici';
44
import type {
55
HttpClientResponse,
66
} from './Response';

test/fixtures/ts-esm/hello.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { RequestURL, RequestOptions2, RequestOptions } from 'urllib';
2+
import { HttpClientResponse } from 'urllib';
3+
import urllib from 'urllib';
4+
import * as urllib2 from 'urllib';
5+
6+
async function request(url: RequestURL, options: RequestOptions): Promise<HttpClientResponse> {
7+
return await urllib.request(url, options);
8+
}
9+
10+
async function request2(url: RequestURL, options: RequestOptions2): Promise<HttpClientResponse> {
11+
return await urllib2.curl(url, options);
12+
}
13+
14+
console.log(request, request2);

test/fixtures/ts-esm/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "ts-esm-demo",
3+
"type": "module"
4+
}

test/fixtures/ts-esm/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"target": "ES2021",
5+
"module": "NodeNext",
6+
"moduleResolution": "NodeNext",
7+
"baseUrl": "./",
8+
"paths": {
9+
"urllib": ["../../.."]
10+
}
11+
}
12+
}

test/fixtures/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"name": "ts-demo"
2+
"name": "ts-cjs-demo"
33
}

test/fixtures/ts/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"strict": true,
44
"target": "ES2021",
5-
"module": "NodeNext",
5+
"module": "CommonJS",
66
"moduleResolution": "Node",
77
"baseUrl": "./",
88
"paths": {

test/mts/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "test-ts"
3+
}

0 commit comments

Comments
 (0)