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

fix: export module with .mjs to fix Vite resolution #1366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kevinji
Copy link

@kevinji kevinji commented Nov 29, 2024

Vite tries to resolve the module as a CommonJS module because the main package.json does not contain "type": "module". Override this behavior by explicitly adding a .mjs extension.

Locally, this fixes vega/svelte-vega#977 for me.

Vite tries to resolve the module as a CommonJS module because the
main package.json does not contain "type": "module". Override this
behavior by explicitly adding a .mjs extension.
@domoritz
Copy link
Member

Thanks for the pull request. Isn't this incorrect behavior from vite? I thought module and import imply esm.

@kevinji
Copy link
Author

kevinji commented Nov 29, 2024

I'm unsure what the correct behavior based on the spec should be, but based on this Node.js package example, it claims:

The preceding example uses explicit extensions .mjs and .cjs. If your files use the .js extension, "type": "module" will cause such files to be treated as ES modules, just as "type": "commonjs" would cause them to be treated as CommonJS. See Enabling ESM.

@domoritz
Copy link
Member

This hasn't come up in years and we have multiple packages that I want to keep consistent so I'm treading carefully here.

Maybe the best move at this point is to cut new major versions and move on to esm for everything.

@kevinji
Copy link
Author

kevinji commented Nov 29, 2024

I understand your concern about not special-casing this repo. My guess is that this was more recently broken by #1301, as vega and vega-lite use the older module field without exports and this seems to be handled correctly.

This also seems consistent with the documentation under Conditional exports, which says

"import" - matches when the package is loaded via import or import(), or via any top-level import or resolve operation by the ECMAScript module loader. Applies regardless of the module format of the target file. Always mutually exclusive with "require". [emphasis added]

which suggests that the target file would default to CommonJS if "type": "module" is missing, even though import is used to load the package.

For consistency, it may be easier to move all repos to "type": "module" and explicitly output .cjs files for CommonJS compatibility; I have no strong preferences other than any fix for ESM seems better than none.

@kevinji
Copy link
Author

kevinji commented Dec 6, 2024

@domoritz did you have a preference for how you wanted to move forward? I can make a larger PR to convert all the Vega projects to using "type": "module" if you prefer, although those will be a bit more invasive as all the other .js files will need to be converted to ESM.

@domoritz
Copy link
Member

domoritz commented Dec 6, 2024

Yeah, I think moving to ESM for everything is a good idea: vega/vega#3990.

We already use import statements so hopefully it's a not too painful transition. I'm a bit busy until the end of the semester but plan to make some time over the break to get this done. It would be awesome if you could help with PRs!

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.

Does not work with SvelteKit SSR
2 participants