Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export of type definitions, depending on whether it is ES Module or CommonJS #250

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixup
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ cat >lib/esm/package.json <<!EOF
"type": "module"
}
!EOF

cp index.d.ts lib/cjs/
cp index.d.ts lib/esm/
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as axios from 'axios';

export = IAxiosRetry;
export default IAxiosRetry;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when use =, below error occur. So, i change to default.

error TS1192: Module '"/home/study/workspace/ts-node-oidc/node_modules/axios-retry/lib/esm/index"' has no default export.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the reason for why an import like:

import { IAxiosRetryConfig } from 'axios-retry'

no longer works.

Copy link

@rchl rchl Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yutak23 how/when did you get that error? Did it work with version of axios-retry prior to your change?

I'm thinking that we could just revert to the old code if your error was not introduced by these changes.

Copy link
Contributor Author

@yutak23 yutak23 Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rchl

When compiling to CommonJS, export = is fine, but when compiling to ES Module, export = will result in the following error Therefore, we changed it to default export.
#250 (comment)

However, the modification to export IAxiosRetryConfig etc. for name import was omitted. Sorry about that. I have uploaded the fix in the #252.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did export = really create an issue and in what circumstances? I haven't seen issues after changing it in this project and building.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen issues after changing it in this project and building.

I would be very grateful if you could provide me with details of the configuration information for your TypeScript project.
The problem I am talking about happens when compiling to ES Module with ES Module configuration (set type: module in package.json)

When export =, the following compile error occurs. When export default is used, no error occurs.
image
image

My environment is below.

// package.json
	"type": "module",
	"dependencies": {
		"axios": "^1.6.2",
		"axios-retry": "^3.9.0",
	},
	"devDependencies": {
		"@tsconfig/node16": "^16.1.1",
		"@tsconfig/strictest": "^2.0.2",
		"typescript": "~5.2.2",
	}
// tsconfig.json
{
	"extends": ["@tsconfig/node16/tsconfig.json", "@tsconfig/strictest/tsconfig.json"],
	"compilerOptions": {
		"outDir": "dist",
		"declaration": true,
		"sourceMap": true
	},
	"include": ["srv/**/*.ts"],
	"exclude": ["node_modules", "dist"]
}

Copy link

@rchl rchl Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question is: did this work before (before v3.9.0)?

Because if not then there is not really a need to change that now. Could do that in next major version if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is already becoming Revert's policy, but I think it would be rude not to answer your question, so I want to answer.

The type definition used to be export default and it worked except for the ES Module. . However, this had a problem with issue and has been modified. But then another problem arose and more changes were made.

export default axiosRetry;

export as namespace axiosRetry;
declare const IAxiosRetry: IAxiosRetry.AxiosRetry;

Expand Down
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"files": [
"es",
"lib",
"index.js",
"index.d.ts"
"index.cjs"
],
"scripts": {
"lint": "eslint es/**/*.mjs spec/**/*.spec.mjs",
Expand Down Expand Up @@ -59,14 +58,20 @@
"bugs": {
"url": "https://github.com/softonic/axios-retry/issues"
},
"types": "./index.d.ts",
"types": "lib/esm/index.d.ts",
"main": "index.js",
"module": "lib/esm/index.js",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./lib/esm/index.js",
"require": "./index.js"
"import": {
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
},
"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./index.js"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using Conditional exports, the type definition of the imported axios will be switched within the type definition in each case of CommonJS and ES Module. This eliminates compilation errors.

Incidentally, I don't think there will be any impact on the existing use of JavaScript. Because Conditional exports was already in use.

"default": "./index.js"
},
"./package.json": "./package.json"
}
Expand Down
Loading