Skip to content

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

@doman412

Description

@doman412

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions