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

Provide an option to exclude files from HMR #321

Closed
IanVS opened this issue Apr 28, 2022 · 3 comments
Closed

Provide an option to exclude files from HMR #321

IanVS opened this issue Apr 28, 2022 · 3 comments
Labels

Comments

@IanVS
Copy link

IanVS commented Apr 28, 2022

Describe the problem

In the storybook vite builder, we have a problem with svelte stories not correctly reloading when changes are made to files. I don't completely understand what's going on, but story files are watched by the manager webpack so that it can HMR the list of files, but that makes them incompatible for vite HMR, it seems. We had the same problem with react stories, and the solution was to exclude stories from HMR, allowing the page to reload instead when changes are made.

However, it seems there's no such option for svelte to control when HMR happens. It's only on or off, as far as I can tell.

Describe the proposed solution

It would be great if you could support an include/exclude set of globs, much like rollup transform plugins or @vitejs/plugin-react: https://github.com/vitejs/vite/tree/main/packages/plugin-react#filter-which-files-use-fast-refresh

Alternatives considered

I can turn off HMR entirely for svelte projects, but that doesn't seem like a great option. HMR is working just fine when editing components, just not stories.

Importance

would make my life the life of storybook users easier

@IanVS IanVS added enhancement New feature or request triage Awaiting triage by a project member labels Apr 28, 2022
@dominikg
Copy link
Member

As far as i am aware storybook is not going to support authoring stories in svelte format going forward, and focuses on their own component story format (csf) instead.

So i'm not really sure how csf stories would end up being processed by vite-plugin-svelte.

As a workaround you could create 2 instances of vite-plugin-svelte that operate on disjunct sets with include/exclude:

const storyPattern = "**/*.story.svelte";
export default {
  plugins:[
    svelte({exclude: storyPattern}),
    {
      ...svelte({include: storyPattern, hot: false}), 
      name: "vite-plugin-svelte-stories" // avoid 2 plugin instances with same name
    }
  ]
}

@dominikg dominikg added has-workaround hmr and removed triage Awaiting triage by a project member labels Apr 29, 2022
@IanVS
Copy link
Author

IanVS commented Apr 29, 2022

Thanks for the workaround suggestion. That looks like it will work.

It sounds like it's a bit uncertain what will happen in storybook 7.0, but I want to make sure everything works correctly in 6.x.

@bluwy
Copy link
Member

bluwy commented Apr 30, 2022

If the workaround works, I think it is the solution instead of adding another option for this case, as include and exclude options are meant for this usecase.

@IanVS IanVS closed this as completed Apr 30, 2022
IanVS added a commit to storybookjs/builder-vite that referenced this issue May 1, 2022
Fixes #52

Svelte story files have the same issue as react story files.  The webpack HMR seems to prevent vite's HMR from working correctly, so we need to exclude story files as HMR boundaries in vite.  The svelte plugin doesn't expose as nice of a way to do this, but thanks to a suggestion from @domyen in sveltejs/vite-plugin-svelte#321 (comment), I was able to make it work.

To test:
Start up the svelte example, edit a `.stories.svelte` file, and confirm that the page reloads and shows your changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants