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 import.meta.hot for library dev builds #8921

Closed
4 tasks done
doman412 opened this issue Jul 4, 2022 · 4 comments · Fixed by #8944
Closed
4 tasks done

Preserve import.meta.hot for library dev builds #8921

doman412 opened this issue Jul 4, 2022 · 4 comments · Fixed by #8944

Comments

@doman412
Copy link
Contributor

doman412 commented Jul 4, 2022

Description

I would like a way to preserve import.meta.hot in library builds. I'm building a library that will be used in a project developed with vite and there doesn't seem to be a way to add HMR code to the library itself for use in my other project. Ideally I would be able to build a dev version of the library with HRM code using a config flag or during a vite build --watch process.

Suggested solution

I think a solution similar to this #8090 would work. Currently, as I'm developing this library, I'm using a locally linked version of vite (3.0.0-beta.6) with custom modifications to the define plugin file as a workaround but ideally it would be built into vite. My workaround is basically to delete the replacement when in dev mode but someone with a better understanding of vite internals may have a more robust solution including configs.

my workaround: packages/vite/src/node/plugins/define.ts

if(isBuild && nodeEnv === 'development'){
  delete importMetaFallbackKeys['import.meta.hot']
}

Alternative

No response

Additional context

No response

Validations

@poyoho
Copy link
Member

poyoho commented Jul 5, 2022

vite build mode will replace import.meta.hot to false. If you want to kept it in your dist file. I think you need some magic to avoid vite analysis.

const importMeta = import.meta
if(importMeta.hot) {
  ....
}

BTW, for vite build I think to replace import.meta.hot to false is acceptable. 🤔

@doman412
Copy link
Contributor Author

doman412 commented Jul 5, 2022

That doesn't seem to work. Removing my local changes from original issue where I delete the replacement key, importMeta only seems to have the url property and no others.

I agree replacing import.meta.hot with false is good behavior for production builds and would like to keep that, so that those blocks are optimized out. But I would also like to create a development build that keeps import.meta.hot in place. That way when I use my library in a vite based project, I can point to that dev build when developing and have vite hook into the library's HMR code.

@bluwy
Copy link
Member

bluwy commented Jul 5, 2022

One way Vite can allow this is by moving this line:

'import.meta.hot': `false`

into importMetaKeys. That way you can use define: { 'import.meta.hot': 'import.meta.hot' } to opt-in to preserving hot code. If that works for you I can set up a PR for it (or you can if you're interested 🙂 )

@kawazoe
Copy link

kawazoe commented Dec 19, 2022

I too would like a feature like this, but for different reasons. I maintain a library that ships a bunch of components and composables. There's also an optional bundle that users can include to integrate with pinia. I wish it would handle HMR automatically for them, but if I can't have a clean way to preserve import.meta.hot in a production build, while also letting their own bundler remove it, I feel like that's just not something I can do.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants