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

Support index-modules in exports #229

Closed
wants to merge 1 commit into from
Closed

Conversation

simonihmig
Copy link
Collaborator

This is not necessarily something I expect to get merged immediately, more so to start a conversation...

The problem this is addressing is that index-files, which are supported in v1 addons and the default node module resolution, are not working ootb in v2 addons, due to our explicit exports mapping. Say you put a foo/index.js module into /src, then currently the only way to import that is by import foo from 'addon/foo/index', which I think is pretty much unexpected.

This is especially annoying when migrating from v1 addons. For example a common suggestion would be to move your addon's addon-test-support to src/test-support. But again that does not allow you to import from addon/test-support as expected, so makes this a breaking change.

Ideally I think, we would allow alternatives, so that import foo from 'addon/foo' can be satisfied either by src/foo.js or src/foo/index.js.

And actually this seems to be possible, with the changes in this PR. Both webpack (docs)and TypeScript seem to support an array of alternative modules in exports. And TypeScript has supported that for long in typesVersions, which is also what the v1 blueprint does!

The only problem: vite supports array-based alternatives, but does no file-check, just takes whatever matches first AFAIU, see vitejs/vite#4439 (comment). Which is not what we need to support this use case.

The alternative to this "just works" approach would be to force users to explicitly define the exports mapping. But this also does not seem good, because

  • not too many folks really know about exports and how they work I think. And if resolving an index-module does not work, you won't get a really helpful error message like saying "tweak your exports".
  • you might ship breaking changes accidentally
  • if you have many nested files using index files, it become very tedious to explicitly map them, as AFAIK you cannot do that with any globs, so you would have an explicit entry per index-file. Which also can get easily out of sync.

Thoughts?

@simonihmig simonihmig requested a review from a team November 13, 2023 10:37
"types": "./declarations/*.d.ts",
"default": "./dist/*.js"
"types": ["./declarations/*.d.ts", "./declarations/*/index.d.ts"],
"default": ["./dist/*.js", "./dist/*/index.js"]
},<% } else { %>
".": "./dist/index.js",
"./*": "./dist/*.js",<% } %>
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this syntax support the array form?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure what exactly you mean here? Support for arrays in general is given as backed by the above links. You mean in this particular place, like L109?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, like, if we add /index stuff, it should be both for and without the TS exports

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah right! Not sure why I didn't even think about it. I'll add it, should we agree to actually go forward with this!

Copy link
Collaborator

@NullVoxPopuli NullVoxPopuli Nov 13, 2023

Choose a reason for hiding this comment

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

I mean, you have my vote!
(not that this is a democracy, since we need to convince everyone the technical merits of a thing lol)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But what about vite? My understanding is this will fail under vite. Unless we can convince vite maintainers to change course? (not too likely?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why's vite matter here?

The need to declare something about index for ember's conventions helps all packagers

@ef4
Copy link
Contributor

ef4 commented Nov 20, 2023

I don't think this is part of Node's spec for package.json exports. I just tried it and Node only attempts to resolve the first thing. I'd rather not diverge from Node.

We already auto-generate the app-js entry in package.json, based on your actual files. It doesn't seem that much worse to generate exports too?

@simonihmig
Copy link
Collaborator Author

Closing this, as we agreed to not do this change in the team meeting. The question if we should make our rollup tooling auto-generate this didn't really come to any definite conclusion. We can discuss pros and cons again...

@simonihmig simonihmig closed this Nov 23, 2023
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.

3 participants