Skip to content

Commit

Permalink
fix: allow type export on mts (#465)
Browse files Browse the repository at this point in the history
closes #464
  • Loading branch information
fengmk2 committed Sep 11, 2023
1 parent 56e7a11 commit 166403f
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 11 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
"build:version": "node ./scripts/replace_urllib_version.js",
"build:cjs:test": "cd test/cjs && rm -rf node_modules && npm link ../.. && node index.js",
"build:esm:test": "cd test/esm && rm -rf node_modules && npm link ../.. && node index.js",
"build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run test-tsc",
"test-tsc": "tsc -p ./test/fixtures/ts/tsconfig.json",
"build:mts:test": "cd test/mts && rm -rf node_modules && npm link ../.. && tsc",
"build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run build:mts:test && npm run test-tsc",
"test-tsc": "npm run test-tsc:cjs",
"test-tsc:cjs": "tsc -p ./test/fixtures/ts/tsconfig.json",
"test-tsc:esm": "tsc -p ./test/fixtures/ts-esm/tsconfig.json",
"test": "npm run lint && vitest run",
"test-keepalive": "cross-env TEST_KEEPALIVE_COUNT=50 vitest run --test-timeout 180000 keep-alive-header.test.ts",
"cov": "vitest run --coverage",
Expand Down Expand Up @@ -82,6 +85,8 @@
"@types/selfsigned": "^2.0.1",
"@types/tar-stream": "^2.2.2",
"@vitest/coverage-v8": "^0.32.0",
"@tsconfig/node18": "^18.2.1",
"@tsconfig/strictest": "^2.0.2",
"busboy": "^1.6.0",
"cross-env": "^7.0.3",
"eslint": "^8.25.0",
Expand Down
7 changes: 6 additions & 1 deletion scripts/esm_import_fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ async function main() {
const root = path.join(process.cwd(), 'src/esm');
const files = await fs.readdir(root);
for (const name of files) {
if (!name.endsWith('.js')) continue;
if (!name.endsWith('.js') && !name.endsWith('.d.ts')) continue;
const file = path.join(root, name);
const content = await fs.readFile(file, 'utf-8');
// replace "from './HttpClient'" to "from './HttpClient.js'"
const newContent = content.replace(/ from \'(\.\/[^\.\']+?)\'/g, (match, p1) => {
const after = ` from '${p1}.js'`;
console.log('[%s] %s => %s', file, match, after);
return after;
}).replace(/import\(\"(\.\/[^\.\"]+?)\"\)/g, (match, p1) => {
// import("./Response") => import("./Response.js")
const after = `import("${p1}.js")`;
console.log('[%s] %s => %s', file, match, after);
return after;
});
if (newContent !== content) {
await fs.writeFile(file, newContent);
Expand Down
4 changes: 2 additions & 2 deletions src/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import dns from 'node:dns';
import { LookupFunction, isIP } from 'node:net';
import {
Agent,
Dispatcher,
buildConnector,
} from 'undici';
import type Dispatcher from 'undici/types/dispatcher';
import type buildConnector from 'undici/types/connector';

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

Expand Down
2 changes: 1 addition & 1 deletion src/Request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Readable, Writable } from 'node:stream';
import type { IncomingHttpHeaders } from 'node:http';
import type Dispatcher from 'undici/types/dispatcher';
import type { Dispatcher } from 'undici';
import type {
HttpClientResponse,
} from './Response';
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/ts-esm/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RequestURL, RequestOptions2, RequestOptions } from 'urllib';
import { HttpClientResponse } from 'urllib';
import urllib from 'urllib';
import * as urllib2 from 'urllib';

async function request(url: RequestURL, options: RequestOptions): Promise<HttpClientResponse> {
return await urllib.request(url, options);
}

async function request2(url: RequestURL, options: RequestOptions2): Promise<HttpClientResponse> {
return await urllib2.curl(url, options);
}

console.log(request, request2);
4 changes: 4 additions & 0 deletions test/fixtures/ts-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ts-esm-demo",
"type": "module"
}
12 changes: 12 additions & 0 deletions test/fixtures/ts-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2021",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"baseUrl": "./",
"paths": {
"urllib": ["../../.."]
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"name": "ts-demo"
"name": "ts-cjs-demo"
}
2 changes: 1 addition & 1 deletion test/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"strict": true,
"target": "ES2021",
"module": "NodeNext",
"module": "CommonJS",
"moduleResolution": "Node",
"baseUrl": "./",
"paths": {
Expand Down
3 changes: 3 additions & 0 deletions test/mts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "test-ts"
}
8 changes: 8 additions & 0 deletions test/mts/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { request } from "urllib";
const responseObj = await request("test");

type IsAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
(x: IsAny<number, true, never>) => x; // never
(x: IsAny<any, true, never>) => x; // true

(x: IsAny<typeof responseObj, true, never>) => x; // true
9 changes: 9 additions & 0 deletions test/mts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["@tsconfig/strictest/tsconfig", "@tsconfig/node18/tsconfig"],
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"outDir": "dist",
"skipLibCheck": false
},
"include": ["src/**/*"]
}
1 change: 0 additions & 1 deletion tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "./src/cjs",
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"module": "NodeNext",
"moduleResolution": "node",
"module": "CommonJS",
"moduleResolution": "Node",
"newLine": "LF",
"checkJs": false,
"allowJs": true,
Expand Down

0 comments on commit 166403f

Please sign in to comment.