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

Differing default exports #83

Closed
probins opened this issue Oct 26, 2020 · 5 comments
Closed

Differing default exports #83

probins opened this issue Oct 26, 2020 · 5 comments

Comments

@probins
Copy link

probins commented Oct 26, 2020

I'm using several functions from Turfjs, a library of functions for manipulating GeoJSON files. With most of them, I can just do, for example:
import lineSplit from "https://jspm.dev/@turf/line-split";
and then lineSplit(...)
but with
import nearest from "https://jspm.dev/@turf/nearest-point-on-line";
I cannot do nearest(...), but have to do nearest.default(...).

Is this expected behaviour, and if so why?

@guybedford
Copy link
Member

This is exactly the standard interop problem.

This edge case applies only for modules that export both an __esModule property and default export. This is a relatively rare edge case since roughly 10% of packages have an __esModule property, and less than 10% of those also have a default export.

jspm has gone the other way to Webpack and RollupJS on this one specific edge case to instead follow Node.js semantics which are more well-defined.

You can read more about the strict Node.js interop semantics here - https://nodejs.org/dist/latest-v15.x/docs/api/esm.html#esm_commonjs_namespaces.

If you want to write you import in a way that works with any bundler for this edge case, write:

import _nearest from "https://jspm.dev/@turf/nearest-point-on-line";
const nearest = _nearest.default || _nearest;

@probins
Copy link
Author

probins commented Oct 26, 2020

ok. I did notice that the package.json was set up differently, and assumed it was something to do with that.

Thanks for the prompt response.

@probins probins closed this as completed Oct 26, 2020
@guybedford
Copy link
Member

Sure, and if you'd like a beta invite just ping me.

@teohhanhui
Copy link

teohhanhui commented Nov 10, 2020

import _nearest from "https://jspm.dev/@turf/nearest-point-on-line";
const nearest = _nearest.default || _nearest;

That's not an elegant solution as it pushes the burden onto the consumer of the module.

What would be the recommended way to fix this on the module side? In my case, it's https://github.com/staltz/xstream/blob/v11.14.0/dist/xstream.js which is apparently generated by https://www.npmjs.com/package/babel-plugin-transform-es2015-modules-umd

jspm sandbox

@guybedford
Copy link
Member

The module.exports.default value is exactly what is being defined, see https://github.com/staltz/xstream/blob/v11.14.0/dist/xstream.js#L1527.

If you are interested in why this is the case, read the interop issue here - rollup/plugins#635.

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

No branches or pull requests

3 participants