Skip to content

Commit

Permalink
feat: use tshy to support commonjs and esm both (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Sep 14, 2023
1 parent 177137c commit b2576c0
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -12,4 +12,6 @@ src/**/*.d.ts
!src/formstream.d.ts
.DS_Store
test/fixtures/ts/*.js
test/fixtures/ts-esm/*.js
.eslintcache
.tshy/
42 changes: 24 additions & 18 deletions package.json
Expand Up @@ -19,21 +19,28 @@
"author": "fengmk2 <fengmk2@gmail.com> (https://github.com/fengmk2)",
"homepage": "https://github.com/node-modules/urllib",
"type": "module",
"tshy": {
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
}
},
"exports": {
".": {
"import": {
"types": "./src/esm/index.d.ts",
"default": "./src/esm/index.js"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./src/cjs/index.d.ts",
"default": "./src/cjs/index.js"
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"./package.json": "./package.json"
},
"types": "./src/esm/index.d.ts",
"main": "./src/cjs/index.js",
"typings": "./dist/commonjs/index.d.ts",
"files": [
"dist",
"src"
],
"repository": {
Expand All @@ -43,24 +50,22 @@
"scripts": {
"lint": "eslint src test --ext .ts --cache",
"prebuild": "npm run clean",
"build": "tsc --version && npm run build:cjs && npm run build:esm && npm run build:version",
"postbuild": "rm -rf src/*.tsbuildinfo",
"build:cjs": "tsc -p ./tsconfig.build.cjs.json",
"build:esm": "tsc -p ./tsconfig.build.esm.json && node ./scripts/esm_import_fix.js",
"build": "tsc --version && tshy && npm run build:version",
"postbuild": "rm -rf *.tsbuildinfo",
"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: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-tsc": "npm run test-tsc:cjs && npm run test-tsc:esm",
"test-tsc:cjs": "cd test/fixtures/ts && rm -rf node_modules && npm link ../../.. && npm run build",
"test-tsc:esm": "cd test/fixtures/ts-esm && rm -rf node_modules && npm link ../../.. && npm run build",
"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",
"ci": "npm run lint && npm run cov && npm run build:test",
"ci": "npm run lint && npm run cov && node scripts/build_test.js",
"contributor": "git-contributor",
"clean": "rm -rf src/*.tsbuildinfo src/cjs/*.ts src/cjs/*.js src/esm/*.ts src/esm/*.js",
"clean": "rm -rf dist",
"prepublishOnly": "npm run build"
},
"dependencies": {
Expand All @@ -76,6 +81,8 @@
"ylru": "^1.3.2"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.1",
"@tsconfig/strictest": "^2.0.2",
"@types/busboy": "^1.5.0",
"@types/default-user-agent": "^1.0.0",
"@types/mime-types": "^2.1.1",
Expand All @@ -85,8 +92,6 @@
"@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 All @@ -96,6 +101,7 @@
"proxy": "^1.0.2",
"selfsigned": "^2.0.1",
"tar-stream": "^2.2.0",
"tshy": "^1.0.0-3",
"typescript": "^5.0.4",
"vitest": "^0.32.0"
},
Expand Down
15 changes: 15 additions & 0 deletions scripts/build_test.js
@@ -0,0 +1,15 @@
import { execSync } from 'child_process';

function main() {
if (process.version.startsWith('v14.')) {
console.log(`ignore build:test on Node.js ${process.version}`);
return;
}
const cwd = process.cwd()
execSync('npm run build:test', {
cwd,
stdio: [ 'inherit', 'inherit', 'inherit' ],
});
}

main();
31 changes: 0 additions & 31 deletions scripts/esm_import_fix.js

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/replace_urllib_version.js
Expand Up @@ -7,8 +7,8 @@ async function main() {
const root = process.cwd();
const pkg = JSON.parse(await fs.readFile(path.join(root, 'package.json')));
const files = [
path.join(root, 'src/cjs/HttpClient.js'),
path.join(root, 'src/esm/HttpClient.js'),
path.join(root, 'dist/commonjs/HttpClient.js'),
path.join(root, 'dist/esm/HttpClient.js'),
];
for (const file of files) {
const content = await fs.readFile(file, 'utf-8');
Expand Down
10 changes: 0 additions & 10 deletions settings.json

This file was deleted.

12 changes: 6 additions & 6 deletions src/HttpClient.ts
Expand Up @@ -30,12 +30,12 @@ import qs from 'qs';
import pump from 'pump';
// Compatible with old style formstream
import FormStream from 'formstream';
import { HttpAgent, CheckAddressFunction } from './HttpAgent';
import { RequestURL, RequestOptions, HttpMethod, RequestMeta } from './Request';
import { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response';
import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadable } from './utils';
import symbols from './symbols';
import { initDiagnosticsChannel } from './diagnosticsChannel';
import { HttpAgent, CheckAddressFunction } from './HttpAgent.js';
import { RequestURL, RequestOptions, HttpMethod, RequestMeta } from './Request.js';
import { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response.js';
import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadable } from './utils.js';
import symbols from './symbols.js';
import { initDiagnosticsChannel } from './diagnosticsChannel.js';

type Exists<T> = T extends undefined ? never : T;
type UndiciRequestOption = Exists<Parameters<typeof undiciRequest>[1]>;
Expand Down
2 changes: 1 addition & 1 deletion src/Request.ts
Expand Up @@ -3,7 +3,7 @@ import type { IncomingHttpHeaders } from 'node:http';
import type { Dispatcher } from 'undici';
import type {
HttpClientResponse,
} from './Response';
} from './Response.js';

export type HttpMethod = Dispatcher.HttpMethod;

Expand Down
3 changes: 0 additions & 3 deletions src/cjs/package.json

This file was deleted.

4 changes: 2 additions & 2 deletions src/diagnosticsChannel.ts
Expand Up @@ -3,8 +3,8 @@ import { performance } from 'node:perf_hooks';
import { debuglog } from 'node:util';
import { Socket } from 'node:net';
import { DiagnosticsChannel } from 'undici';
import symbols from './symbols';
import { globalId, performanceTime } from './utils';
import symbols from './symbols.js';
import { globalId, performanceTime } from './utils.js';

const debug = debuglog('urllib:DiagnosticsChannel');
let initedDiagnosticsChannel = false;
Expand Down
3 changes: 0 additions & 3 deletions src/esm/package.json

This file was deleted.

10 changes: 5 additions & 5 deletions src/index.ts
@@ -1,6 +1,6 @@
import LRU from 'ylru';
import { HttpClient, HEADER_USER_AGENT } from './HttpClient';
import { RequestOptions, RequestURL } from './Request';
import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
import { RequestOptions, RequestURL } from './Request.js';

let httpclient: HttpClient;
const domainSocketHttpclients = new LRU(50);
Expand Down Expand Up @@ -39,14 +39,14 @@ export {
export {
HttpClient, HttpClient as HttpClient2, HEADER_USER_AGENT as USER_AGENT,
RequestDiagnosticsMessage, ResponseDiagnosticsMessage,
} from './HttpClient';
} from './HttpClient.js';
// RequestOptions2 is keep compatible with urlib@2 RequestOptions2
export {
RequestOptions, RequestOptions as RequestOptions2, RequestURL, HttpMethod,
FixJSONCtlCharsHandler, FixJSONCtlChars,
} from './Request';
} from './Request.js';

export { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response';
export { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response.js';

export default {
request,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
@@ -1,7 +1,7 @@
import { randomBytes, createHash } from 'node:crypto';
import { Readable } from 'node:stream';
import { performance } from 'node:perf_hooks';
import { FixJSONCtlChars } from './Request';
import type { FixJSONCtlChars } from './Request.js';

const JSONCtlCharsMap = {
'"': '\\"', // \u0022
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/ts-esm/package.json
@@ -1,4 +1,7 @@
{
"name": "ts-esm-demo",
"type": "module"
"type": "module",
"scripts": {
"build": "tsc && node hello.js"
}
}
5 changes: 4 additions & 1 deletion test/fixtures/ts/package.json
@@ -1,3 +1,6 @@
{
"name": "ts-cjs-demo"
"name": "ts-cjs-demo",
"scripts": {
"build": "tsc && node hello.js"
}
}
6 changes: 0 additions & 6 deletions tsconfig.build.cjs.json

This file was deleted.

7 changes: 0 additions & 7 deletions tsconfig.build.esm.json

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Expand Up @@ -7,8 +7,8 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"module": "CommonJS",
"moduleResolution": "Node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"newLine": "LF",
"checkJs": false,
"allowJs": true,
Expand Down

0 comments on commit b2576c0

Please sign in to comment.