vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
823 stars 52 forks source link

"Mixed" exports #8

Closed silverwind closed 1 year ago

silverwind commented 1 year ago

Readme says "For React refresh to work, your file should only export React components". I fell like this is a huge drawback to this module as I often employ a pattern to export both functions and components in the same file:

export const helper = () => "example"; // exported for unit testing
export const component = () => <div>{helper()}</div>; // exported for rendering

Can it be elaborated what exactly breaks when this pattern is employed? Can it be fixed?

ArnaudBarre commented 1 year ago

It misses some official documentations for this. But for now the React core team don't plan to make this possible.

With the new beta just released, this now possible, it will just warn you when you do it and propagate the update.

What "breaks" is that fast-refresh is only able to create proxies for component, other exports would be stable without the new invalidation in beta.

The way I structure my codebase is that I have helpers|utils|display.ts files (some at the root, some in views folders) to share this kind of helpers

silverwind commented 1 year ago

What "breaks" is that fast-refresh is only able to create proxies for component, other exports would be stable without the new invalidation in beta.

Okay, if changes to a function export requires a full page reload, that'd be acceptable to me. Suggesting to update the docs with this info.

ArnaudBarre commented 1 year ago

The new behaviour (beta version) is that it propagates the invalidation, and full-reload only if an entry point is encounter.