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

Converting packages to type: module by default #2565

Merged
merged 13 commits into from
Mar 11, 2024

Conversation

smallsaucepan
Copy link
Member

Given we use the ES module import syntax almost everywhere, and are exporting CJS and ESM versions anyway, we should make our modules "ESM first".

Had to disable a few type tests, though these are only in packages where we import older third party libraries.

Didn't bother replacing node __dirname references in tests as they seem to work fine within each package without.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Have read How To Contribute.
  • Run npm test at the sub modules where changes have occurred.
  • Run npm run lint to ensure code style at the turf module level.

…mpatibility. Flow on changes from that include requiring a js extension for relative includes, and workarounds for a couple of older third party libraries that were no longer importing smoothly. Builds successfully. Updating tests next.
…rences to __dirname. Added some re-exporting wrapper files for older packages like rbush and sweepline-intersections. See turf-line-intersects for an example.
…issues in post type: module. Maybe someone in future can exercise some typescript jujitsu to re-enable them.
@smallsaucepan smallsaucepan added typescript bundling Anything related to issues with bundling labels Jan 10, 2024
.monorepolint.config.mjs Show resolved Hide resolved
.monorepolint.config.mjs Show resolved Hide resolved
packages/turf-along/test.ts Outdated Show resolved Hide resolved
packages/turf-hex-grid/test.ts Show resolved Hide resolved
packages/turf/test.ts Outdated Show resolved Hide resolved
// Manifests as "This expression is not callable ... has no call signatures"
// https://stackoverflow.com/a/74709714

import lib from "rbush";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto for rbush

@@ -0,0 +1,7 @@
// Get around problems with moduleResolution node16 and some older libraries.
// Manifests as "This expression is not callable ... has no call signatures"
// https://stackoverflow.com/a/74709714
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same thing here, it appears to have default function in both the types and the code. Maybe its a weird build step from the package with the code in it?

https://github.com/rowanwins/sweepline-intersections/blob/master/src/main.js
https://github.com/rowanwins/sweepline-intersections/blob/master/types.d.ts

// Manifests as "This expression is not callable ... has no call signatures"
// https://stackoverflow.com/a/74709714

import lib from "sweepline-intersections";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto on sweepline-intersections

@@ -1,5 +1,8 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

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

For this package, what import was causing problems?

Copy link
Member Author

Choose a reason for hiding this comment

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

Problem was cropping up with the import RBush from "rbush" line.

Copy link
Collaborator

@twelch twelch Mar 10, 2024

Choose a reason for hiding this comment

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

With the wrapper, it looks like you're still needing to use ts-ignore and lose the rbush types in turf-clusters-dbscan/index.js?

Following the rabbit hole of one of the links you shared @smallsaucepan I found this module default-import that is also a workaround wrapper that preserves types. I haven't fully tested it in my own work (I'm doing an esm conversion) but it looks promising.

import { defaultImport } from "default-import";
import rbushDefault from "rbush";
const RBush = await defaultImport(rbushDefault);

class RBushIndex extends RBush<Feature> {
...

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for finding that Tim. It does do the trick though it looks like there may be some flow on effects. To use the const RBush = await defaultImport(rbushDefault); approach we need to change our JS target to at least es2022 to support the top level await. More so the umd package we generate for CDN usage can't have that top level await either, so it would be a change to how turf.min.js was included directly in the browser i.e. https://rollupjs.org/configuration-options/#output-format

Btw where were you seeing errors that required a ts-ignore? Previous approach seemed to work without complaining in my VSCode.

The lib/wrapper approach seems ok. We can use defaultImport, though I think that pushes us forward too quickly and I'm not sure the implications.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No problem if can't do it. The migration I'm doing targets es2022 so I didn't think about top-level await. One idea, wonder if can wrap the dynamic import into a little IIFE like:

import { defaultImport } from "default-import";
import rbushDefault from "rbush";

const RBush = await (async () => {
  const RBush = await defaultImport(rbushDefault);
  return RBush
})()

class RBushIndex extends RBush<Feature> {
...

At that point you might as well keep your wrapper if it's all the same.

I guess it was a ts-expect-error that I saw, not ts-ignore. Maybe what I saw was just the original code you'd already replaced.
image

packages/turf-kinks/types.ts Outdated Show resolved Hide resolved
…ntified that the .cjs extension was causing problems for some consumers. Would have like to use .d.ts rather .d.cts for CJS type defs, but there's a but in tsup at the moment that won't allow that to be customised.
… as not supplied automatically by node any more. Updating benchmark tests. Adding some missing bench and tape @types libraries.
…removed a few that had inadvertently been added.
… Got disabled types tests working again by adjusting tsc command (needed to include node16 module and moduleResolution switched on command line). Also needed to refer to "index", etc as "index.js", etc from test.ts files to avoid "Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16'" errors.
@smallsaucepan
Copy link
Member Author

Hi @mfedderly. Have done the best as I can with this for now.

A summary of any item left unanswered seems to be something odd in the build or packaging of a third party library. Happy to chase these down one by one with the authors as it would be good to remove any wrapper files long term.

For now though, this should be good enough for us to push ahead with the goal of developing in an ESM world, and packaging ESM+CJS.

…ort library which takes care of finding the correct default to import from CJS packages.
Copy link
Collaborator

@twelch twelch left a comment

Choose a reason for hiding this comment

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

This all matches my expectations, based on an ESM migration I'm doing in another repo, which I'm updating to use the latest Turf. I look forward to testing this out when it lands.

…faultImport library which takes care of finding the correct default to import from CJS packages."

This reverts commit f8184c3.
@smallsaucepan
Copy link
Member Author

Thanks for approving @twelch. Let me clean up (roll back the defaultImport change) and then I'll merge. Appreciate your input too @mfedderly given your earlier comments.

@smallsaucepan smallsaucepan merged commit e0bdd0a into Turfjs:master Mar 11, 2024
3 checks passed
Copy link
Collaborator

@mfedderly mfedderly left a comment

Choose a reason for hiding this comment

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

My only other followups would be the rbush and sweepline-intersections import issues.

Thanks for doing all of this!

import { Feature, LineString } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found a thing in my travels that tsup will polyfill these sorts of things for us.
https://tsup.egoist.dev/#inject-cjs-and-esm-shims

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bundling Anything related to issues with bundling typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants