Skip to content

fix cjs with nodenext interop issue #391

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

Closed
wants to merge 16 commits into from

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented Dec 1, 2022

Resolves #389

I tried to keep a centralized types-folder in dist, but unfortunately but after 2h of trial and error, this was the most straight forward way to fix the issue. Now we have typings next to the index.js for esm and cjs and it works.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

@EvanHahn
@kibertoad
@climba03003

PTAL

@Uzlopak Uzlopak changed the title fix cjs typings fix cjs with nodenext interop issue Dec 1, 2022
@EvanHahn
Copy link
Member

EvanHahn commented Dec 1, 2022

I'll look soon, but probably won't have time in the next few days.

Copy link

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

Have you double check the emitted file using npm run build-helmet.
Since the tsconfig.json is using "module": "NodeNext", it will emit ESM types in both folder.
The CJS file is transpile by rollup from ESM to CJS instead of TypeScript.

@climba03003
Copy link

It is actually why I remove rollup in all my package.
The types is not reflect what the actual files when coupling with rollup.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

Yes.

I used your example code and created a new test-project.
You can clone it here:
https://github.com/Uzlopak/helmet-test

Then I did npm run build-helmet in my fork and branch of helmet and then did npm link and in my test-project I did npm link helmet

Then I just did "npm run build" in my test-project with the all combinations of
package.json - type: "module", type: "commonjs"
tsconfig.json - compilerOptions.moduleResolution: "Node", compilerOptions.moduleResolution: "NodeNext"

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

@climba03003
I would not rip out rollup because I assume @EvanHahn would not be OK with it.

@climba03003
Copy link

Yes.

Have you look inside the type file? It may works depends on setting, or bug in TypeScript. But the emitted types is not the correct one.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

You mean because of the .js suffix of the imports?

@climba03003
Copy link

climba03003 commented Dec 1, 2022

I means the types of CJS is incorrect. It is the same version as ESM.
The problem still persist in this case.

image

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

Even with typescript original tsc it will result in the same typings.

Modify in helmet the tsconfig-commonjs.json include to index.ts

run npx tsc -p tsconfig-common.json

It doesnt drop the .js suffix.

But the last line you claim is missing is with direct use of tsc there

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

The last line of exports is removed on purpose by @EvanHahn

See

const startLine = lines.findIndex((line) =>

@climba03003
Copy link

npx tsc -p tsconfig-common.json is emiting ESNext types.
It inherit the setting of tsconfig.json

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 1, 2022

Doesnt actually matter. It works in helmet-test.

The only thing, I think is relevant is the question if I should remove the code, which suppresses the export of the Interfaces for CJS

@climba03003
Copy link

After deep dive into TypeScript document. I see how the resolution of types works.

Old Code

When the project is CommonJS, "exports.require" is used for JavaScript and since there is no *.d.ts files. It will fallback to types field which means the package.json#module is ESM.
Here is the cause why the types are the same but treats as ESM.

New Code

When the project is CommonJS, "exports.require" is used for JavaScript and TypeScript found the *.d.ts files. It use the resolved types and reference to the dist/cjs/package.json#module.
So, it is consider as CommonJS.

Remark

The package.json#exports.types is placed in last place and it is actually ignored by TypeScript. But, when you follow the guide and place at top. It break the above theory to use the *.d.ts files beside the *.js file.
So, either keep it or remove it do not affect the code.

Regrading the types is not export = in CommonJS. I do not found any reference on why it is happened. But when I look through my projects, it is the same. I will not border it much.

@kibertoad
Copy link

kibertoad commented Dec 2, 2022

Perhaps, tsd tests or some simple TS file with self-import that would be compiled in CI, could be added here, that would have been failing previously?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 2, 2022

@kibertoad
@climba03003

https://github.com/Uzlopak/helmet/actions/runs/3601124357/jobs/6066567174
image

I guess some combinations make no sense and thats why it is failing?

@kibertoad
Copy link

yeah, looks like it

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 2, 2022

Here for comparison against the main branch

https://github.com/Uzlopak/helmet/actions/runs/3601170615

image

@EvanHahn
Copy link
Member

EvanHahn commented Dec 9, 2022

I plan to review later this month. Sorry for the delay.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 14, 2022

@EvanHahn

I ignore in the workflow the failing cases. So if @climba03003 or somebody tries to improve the interoperability, it should be possible to remove that specific case from the in the workfllow. But on the other hand it will be mergable at the current state and it is having now a test, so a regression should be impossible.

https://github.com/Uzlopak/helmet/actions/runs/3693726445

@EvanHahn
Copy link
Member

Could someone show me a library that does something similar? I want to make sure I don't break any existing workflows by integrating this change.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Dec 14, 2022

I dont know. I wrote this without any template.

@aqibgatoo
Copy link

Is there some quick fix till this gets merged?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Feb 9, 2023

@EvanHahn

Wazzup? Any ETA?

@EvanHahn
Copy link
Member

EvanHahn commented Feb 9, 2023 via email

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Mar 12, 2023

@EvanHahn

Hi, we got another issue opened because of this issue. fastify/fastify-helmet#216

Can you spare some time to review, please?

@EvanHahn
Copy link
Member

Yes, sorry for my slowness. I'll plan to review by Friday.

@EvanHahn
Copy link
Member

I modified this branch and made #405. Please try it out by running:

npm install 'https://evanhahn.com/tape/helmet-6.0.1.tgz'

Let me know if this works for you. If it does, I'll release a new version.

@EvanHahn
Copy link
Member

I'm closing this in favor of #405. Please tell me if that works for you (or doesn't). I'll be merging that PR soon.

@EvanHahn EvanHahn closed this Mar 28, 2023
@EvanHahn
Copy link
Member

EvanHahn commented Apr 8, 2023

I modified this and deployed it in [email protected]. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

TS1479, helmet typings issue in combination with fastify
5 participants