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

fix: change tsconfig and package exports for types to match output #71

Closed
wants to merge 1 commit into from
Closed

fix: change tsconfig and package exports for types to match output #71

wants to merge 1 commit into from

Conversation

SimeonC
Copy link
Contributor

@SimeonC SimeonC commented Jul 30, 2024

  • Also removed unneeded dependencies that we can just use node inbuilts as we only support node >= 16.
  • Had to switch jest -> vitest as jest couldn’t handle the node ESM import format correctly in tests.
  • Fixed tslint errors as well.

Closes #24

Also removed unneeded dependencies that we can just use node inbuilts as we only support node >= 16.
Had to switch jest -> vitest as jest couldn’t handle the node ESM import format correctly in tests.
Fixed tslint errors as well.
import { Factory } from '../src/factory';
import { TestSuite } from '../src/test_suite';
import { TestCase } from '../src/test_case';
import { Factory } from '../src/factory.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this importing js file and not typescript files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To answer both of your questions in one place; this is required for "module": "Node16" and no bundler (ie no rollup) when targeting ESM compatible output. So basically the output of tsc is exactly what needs to be run in nodejs. Typescript throws an error with the module/target config combination needed for proper JS output for running in node without an extra bundler. There's more detail in this Stack Overflow but the short of it is that ESM nodejs scripts have to include the extension.

Now why this is .js and not .ts is that node can't run typescript files, so the output that node sees is essentially the output of tsc, meaning at that point all the "files" are .js not .ts. Jest (without a bundler in the middle) and it's ts-node just try and import the .js file in the src not the "compiled" output, so it can't find the file. But changing this to .ts and the node.js output (and tsc) will fail (actually maybe this is more ts-node? I couldn't be bothered figuring out why it didn't work just used the same solution as last time but it's definitely "typescripts" issue not jest).

As you mentioned, including some kind of bundler makes this a whole lot easier - jest essentially has an internal bundler - as they mangle all the imports to make them correct before spitting them out. Here we're just using tsc and not bothering with a bundler so it's a bit more inconvenient. Case of less abstractions/steps means more weirdness just staring you in the face... The bundler approach is definitely more forgiving and easier!

Vitest (and vite) is just newer than Jest so they're using newer tools under the hood that are more "aware" of ESM without having to handle all the older commonjs issues that came out when Jest was growing up I guess.

Confusing right, hope some of that helps explain a bit 😂

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the clarification!

Is there any reason to not use a bundler (except that this PR already exists)?

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 was just going for minimal changes to make the existing changes work, moving to a bundler would have been working at the problem from essentially starting from scratch rather than modifying what's there in my mind.

I know of no reason not to use a bundler, except maybe less dependencies/simpler setup.
Seeing that other issue just raised about v4, maybe that's a good reason to need a bundler so we can export both a CJS and ESM build, in this PR I was just fixing the ESM exports as was done in the typescript upgrade. (or rather what I thought had been done, maybe it wasn't intentional!)

Copy link
Owner

Choose a reason for hiding this comment

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

All right, that's reasonable. And thanks again for another contribution!

The change was not intentional.

@HarelM
Copy link
Contributor

HarelM commented Jul 30, 2024

Can you elaborate on what didn't work with jest and why the imports are using .js? Just out of curiosity, as I maintain a few packages with jest and typescript (and rollup) and I don't have these issues...

@HarelM
Copy link
Contributor

HarelM commented Jul 30, 2024

P.S. I'm not a maintainer of this lib.

@davidparsson davidparsson mentioned this pull request Jul 30, 2024
@HarelM
Copy link
Contributor

HarelM commented Jul 30, 2024

I don't mind working on a PR to introduce a bundler (I like rollup, but I don't mind adding a different one if there's a different preference).
I believe there will be less changes than this PR.
Let me know in which direction you would like to go.
Adding a bundler is a matter of a few hours max for this small library.

@SimeonC
Copy link
Contributor Author

SimeonC commented Jul 30, 2024

@HarelM If you're happy to do that it sounds like it's probably for the best. With this PR, and it seems unintentionally with the v4 upgrade, we broke compatibility with CJS usage so adding a bundler that can do the esm + cjs exports would be best. I don't mind closing off this PR in favour of yours 😁

@HarelM
Copy link
Contributor

HarelM commented Jul 30, 2024

Let me open a PR first, then you can review it and decide which is better.
@davidparsson can you give me a few days to open a PR before you merge this one?

@davidparsson
Copy link
Owner

@HarelM, yes, I'll hold a little while. I have no strong preference when it comes to bundlers, and Rollup seems like a very good choice.

Thank you both again for all of your help!

@HarelM
Copy link
Contributor

HarelM commented Jul 30, 2024

I've opened a PR, see #73

@SimeonC
Copy link
Contributor Author

SimeonC commented Jul 31, 2024

Closing this for #73

@SimeonC SimeonC closed this Jul 31, 2024
@SimeonC SimeonC deleted the fix-types-and-exports branch July 31, 2024 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider adding typescript definitions
3 participants