-
Notifications
You must be signed in to change notification settings - Fork 130
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
Move helpers from tslib.es6.js to modules/index.js #171
base: main
Are you sure you want to change the base?
Conversation
Would this be a patch-change? What's the risk of doing that? |
I think this is a patch-level change. If you're able to import |
While this works because tslib is fairly stateless, I think it's a bad idea because it loads two copies of every helper function into any node program that ends up depending on both the cjs and esm versions of Since you can't reasonably make the cjs helpers pull in the esm ones, only the other way around (as currently written) makes sense.
|
The other option would be to use the |
Yeah, the more I think about it, the more I'm convinced that we should just slam a |
What entrypoint should vite be using then? |
If it doesn't respect |
We should probably add a test for vite as well. |
I opened #173 before seeing this thread. Would it be possible to rename tslib.es6.js to tslib.mjs (that would address the node resolution logic when resolving ES modules)? I've stepped through the node module resolution code a bunch of times (primarily while figuring out why certain modules, like tslib, aren't being resolved), and it doesn't seem to find the I certainly understand the need to not break existing code and not causing duplicate helpers in the codebase. |
I'm not sure renaming the file helps if the export map is specifically pointing to what should be an ESM compatible file. The issue as I understand and experience as Vite team also does is that in trying to follow the export map conditions as they are defined, and explicitly favoring the ESM entry, you will end up with a file that is a not ESM. The name doesn't really matter much here IMO but instead the export map entry could just be changed to point to an ESM file as would be expected given the export map definition in place. |
@thescientist13 : Node has documented their logic for determining whether a module is CJS or ESM. If you follow that (and I have, and have also stepped through it in a debugger), the file extension absolutely matters. The official node resolution doesn't understand either of:
|
Hey @johncrim , good call and yes, I glossed over that part in focusing my comment more on the literal name, rather than also the extension change as well. I was more coming from the perspective of a tool doing its own resolution as per the discussion in #173 and so in that case we would be following the spec of NodeJS resolution, but not relying on NodeJS for the resolution per se (aside from the location of the package on disk and then reading its export map / main / etc) from package.json. In my case I am using all this resolver information to build up an import map for the client side for browser based web development. I think at the end of the day as long as ESM entry points only include ESM, everyone should be happy. 💯 |
I'm having trouble with tslib under Jest with ESM. I'm getting SyntaxError: The requested module 'tslib' does not provide an export named '__decorate'
at Runtime.linkAndEvaluateModule (node_modules/.pnpm/jest-runtime@27.0.6_supports-color@9.0.2/node_modules/jest-runtime/build/index.js:669:5)
at TestScheduler.scheduleTests (node_modules/.pnpm/@jest+core@27.0.6_supports-color@9.0.2/node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/.pnpm/@jest+core@27.0.6_supports-color@9.0.2/node_modules/@jest/core/build/runJest.js:387:19)
at _run10000 (node_modules/.pnpm/@jest+core@27.0.6_supports-color@9.0.2/node_modules/@jest/core/build/cli/index.js:408:7) and when I manually copy the tslib.es6.js file over the modules/index.js, it doesn't make a difference. Editing the package.json to point to modules/index.js has no effect either. When I change the
So it seems that jest somehow imports tslib as cjs. I did do the ESM setup for Jest and the other tests work fine. |
@wmertens - That's correct that jest imports tslib as cjs, as I called out in #173. tslib currently has to be special-cased in node projects using ESM, thanks to the tslib layout and metadata. It would be better to add your comment to an issue instead of a pull request, unless the comment is relevant to the pull request. The reason I commented here is that I wanted to make sure the reviewer was aware of other issues, like being compatible with node logic for determining CJS vs ESM. |
I think the problem I'm encountering is that Jest is parsing the module and not detecting the exports. This PR doesn't solve the problem for me. Instead I manually created the exports and I have a workaround up at https://github.com/StratoKit/tslib |
I'm fairly involved in both Svelte and Vite and can answer questions or otherwise help coordinate.
Vite does use Vite will follow the Node spec and will ignore
It looks like that's been done, but changes to the test are needed to reproduce the issue: #143 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no reason we need to do this, and this loads 2 copies of every helper in node
proper, which is not what we wanted to do and defeats the point of having a modules/index.js
almost entirely.
Workaround until microsoft/tslib#171 is merged
Workaround until microsoft/tslib#171 is merged
The reason to do this would be to fix the bug where loading tslib from a loader that only supports standard JS modules and the standard Node export conditions causes an exception. |
@justinfagnani does the loader in question care about the lack of |
This moves the helpers out of
tslib.es6.js
and intomodules/index.js
so that ES Module imports don't depend on the CommonJS/UMD definitions. In addition,tslib.es6.js
now just re-exportsmodules/index.js
so that we still only have to maintain two copies of the helpers (as opposed to adding a third).This addresses an issue with Svelte/Vite where they don't seem to like the re-export of the CommonJS version of the helpers.
Fixes #143