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

Node ESM compatibility #1942

Merged
merged 2 commits into from
Nov 17, 2020
Merged

Node ESM compatibility #1942

merged 2 commits into from
Nov 17, 2020

Conversation

diachedelic
Copy link

Does two things to ensure turf ESM packages can be imported in Node:

  1. Configures the "exports" in package.json, a successor to the "module" property.
  2. Create a package.json containing {"type":"module"} in dist/es/. This ensures that dist/es/index.js is loaded as an ESM module, even if the package's package.json is not "type": "module"

@diachedelic
Copy link
Author

Fixes #1940

@mfedderly
Copy link
Collaborator

Thanks for catching this! There's one package that isn't quite the same as everything else, can you make the same update in packages/turf/package.json as well?

@diachedelic
Copy link
Author

@mfedderly I thought I caught that one?

@mfedderly
Copy link
Collaborator

Oh I see - not all of the changes in this PR came from the monorepolint changes. You can actually do all of this from the .monorepolint.config.js directly. I pushed a branch that I think does what you want.

1d360cc

If you can verify that the change I made was correct and update your PR I can get this merged tomorrow. I'm pretty sure if you pull my own commit in it will block me from being an approver on a branch I contributed to, so its easiest if you make your own commit with those changes if possible.

@diachedelic
Copy link
Author

Are you saying that you make that change to .monorepolint.config.js and then run mrl check --fix, rather than modifying all of the individual package.json's manually?

Writes a package.json to each dist/es/ directory, which ensures that
Node loads adjacent files as ES modules.

close #1940
@diachedelic
Copy link
Author

Okay I've incorporated your change.

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.

Thanks! I mostly consume Turf through webpack, so I would've missed this entirely.

@mfedderly mfedderly merged commit eafb76c into Turfjs:master Nov 17, 2020
@lcrosetto
Copy link

It is breaking on webpack v5 because of missing file extensions on imports, as described here:

https://nodejs.org/api/esm.html#esm_mandatory_file_extensions

Sample error:

ERROR in ../node_modules/@turf/polygonize/dist/es/index.js 3:0-38
Module not found: Error: Can't resolve './lib/EdgeRing' in './node_modules/@turf/polygonize/dist/es'
Did you mean 'EdgeRing.js'?
BREAKING CHANGE: The request './lib/EdgeRing' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
 @ ../node_modules/@turf/turf/dist/es/index.js 64:0-57 64:0-57

@mfedderly
Copy link
Collaborator

@lcrosetto what version? 6.2.0 released today or another version?

@lcrosetto
Copy link

@lcrosetto what version? 6.2.0 released today or another version?

Yes, 6.2.0. Some of the imports from ./lib would need the file extension added, e.g.

import EdgeRing from "./lib/EdgeRing.js";

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.

4 participants