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: use .mjs extension for injected client modules #4061

Merged
merged 1 commit into from Jul 2, 2021

Conversation

sodatea
Copy link
Member

@sodatea sodatea commented Jul 1, 2021

Description

Otherwise, they will not be recognized by Node.js as ES modules.

Usually, it doesn't matter, as they're intended to be run in browsers.

I only encountered this problem in vite-jest, which tries to run vite-transpiled code in Node.js environments.
After this PR, the following workaround would no longer be necessary:
https://github.com/sodatea/vite-jest/blob/1e05b4c62df714b1537c76052d5fec761a46c619/packages/vite-jest/bin/vite-jest.js#L11-L12

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Otherwise, they will not be recognized by Node.js as ES modules.

Usually it doesn't matter, as they're intended to run in browsers.

I only encountered this problem in `vite-jest`, which tries to run
vite-transpiled code in Node.js environments.
After this PR, the following hack would no longer be necessary:
https://github.com/sodatea/vite-jest/blob/1e05b4c62df714b1537c76052d5fec761a46c619/packages/vite-jest/bin/vite-jest.js#L11-L12
@patak-dev
Copy link
Member

Should we also use package.json type module here if we merge #4063 ?

@sodatea
Copy link
Member Author

sodatea commented Jul 1, 2021

Not necessary.

Per Node.js documentation:

Files ending with .mjs are always loaded as ES modules regardless of the nearest parent package.json.

@patak-dev
Copy link
Member

I mean about being consistent. Both approaches look fine 👍🏼

@sodatea
Copy link
Member Author

sodatea commented Jul 1, 2021

I've just fixed the tests in #4064
If consistency is a concern, maybe we should merge that one rather than #4063.

I don't have an opinion on package.json or .mjs for the cache directory.
But in the case of these client modules, as they're inside the vite package, I prefer not to add another package.json.

@antfu antfu merged commit cca92c4 into vitejs:main Jul 2, 2021
maxbeatty added a commit to maxbeatty/vite-jest that referenced this pull request Jul 12, 2021
fixed in vitejs/vite#4061 and released v2.4.0
maxbeatty added a commit to maxbeatty/vite-jest that referenced this pull request Jul 12, 2021
changed in vitejs/vite#4061 and released in v2.4.0
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
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.

None yet

3 participants