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

Preserve Vite's default extensions #1157

Merged
merged 2 commits into from
Apr 8, 2024
Merged

Preserve Vite's default extensions #1157

merged 2 commits into from
Apr 8, 2024

Conversation

edemaine
Copy link
Collaborator

@edemaine edemaine commented Apr 8, 2024

It turns out the default behavior of our Vite unplugin disables all of Vite's default resolve extensions, including e.g. .jsx which was breaking a project I have that uses external JSX libraries (written in Solid, so distributes .jsx files).

config.resolve.extensions ??= [
// Vite's DEFAULT_EXTENSIONs from
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts#L29
'.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json',
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we import this from Vite if present or would it be too much of a hassle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not exported from Vite. I was thinking of submitting a Vite PR to expose it.

But there's also an issue of adding Vite as a dependency. Is there a nice way to copy the constant over with just a dev dependency? I guess I could do it in a build script without much trouble.

Copy link
Collaborator Author

@edemaine edemaine Apr 8, 2024

Choose a reason for hiding this comment

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

I succeeded in getting this to work via import { DEFAULT_EXTENSIONS } from '../../../node_modules/vite/dist/node/constants.js', but sadly esbuild wouldn't treeshake that module, so it ended up adding a bunch of unnecessary code to the bundle.

To fix this, I've added a Node step to build.sh that extracts the one constant we need to an also-checked-in file. Does that seem reasonable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that seems like a good solution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another idea I had: we could put this in a prepare script so it runs when getting dependencies but not when building, which would slightly speed up the dev cycle (I yarn build more often than yarn install). Not sure the time penalty is enough to bother though.

@edemaine edemaine merged commit dbccd00 into main Apr 8, 2024
3 checks passed
@edemaine edemaine deleted the vite-extensions branch April 8, 2024 17:22
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.

2 participants