[go: nahoru, domu]

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

Fix React HMR in story files #53

Merged
merged 1 commit into from
Jun 28, 2021
Merged

Fix React HMR in story files #53

merged 1 commit into from
Jun 28, 2021

Conversation

IanVS
Copy link
Member
@IanVS IanVS commented Jun 28, 2021

Fixes #3.

This "fixes" HMR in react story files by using the new exclude option in @vitejs/plugin-react-refresh to avoid treating story files as HMR boundaries, instead letting changes bubble up into the core storybook client so that it can update the manager when stories are added / deleted / changed. When storybook detects that a story file has changed, it will trigger a reload of the preview iframe. So, it's not exactly full seamless HMR, but it addresses the main problem of story previews not changing upon save.

To test, run yarn install, then cd packages/example-react and yarn storybook. Open the button story in the browser, then make changes to the button story file (add a story, remove a story, change a story, etc). You should see the changes reflected in the browser without requiring a manual page refresh.

@IanVS IanVS requested a review from eirslett June 28, 2021 13:48
plugins.push(require('@vitejs/plugin-react-refresh')());
plugins.push(require('@vitejs/plugin-react-refresh')({
// Do not treat story files as HMR boundaries, storybook itself needs to handle them.
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],

Choose a reason for hiding this comment

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

Probably, also excluding node_modules would lead to bugs with linked dependencies which HMR should be managed by vite

Choose a reason for hiding this comment

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

There should be also ability to override the exclude, as not everyone names files as stories

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably, also excluding node_modules would lead to bugs with linked dependencies which HMR should be managed by vite

Excluding node_modules is the default behavior of plugin-react-refresh, so I wanted to keep that behavior for now, until/unless we hear of problems.

There should be also ability to override the exclude, as not everyone names files as stories

There is, it is possible to change any of this config through the use of viteFinal in the storybook configuration file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's also possible to read the list of stories (or the list of file globs) from the config variable, I think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately it looks like stories from the config is not exposed to the builder. I logged out the options and didn't see it, and it's not in the type definitions, from what I can tell: https://github.com/storybookjs/storybook/blob/next/lib/core-common/src/types.ts#L164

Copy link
Collaborator
@eirslett eirslett Jun 28, 2021

Choose a reason for hiding this comment

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

@shilman should that config option maybe be exposed in the builder API?

One problem is that if Vite is using another glob library than Storybook, there could be a mismatch between the two even if the glob pattern is the same. So neither will be a fool-proof solution.

Copy link
Member
@shilman shilman Jun 28, 2021

Choose a reason for hiding this comment

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

I believe you can get it from options.presets.apply('stories') but I didn't test @IanVS @eirslett

Copy link
Member Author

Choose a reason for hiding this comment

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

After looking into this, it's true that we can get the stories as @shilman suggested. However, those are micromatch globs, and @vitejs/react-fast-refresh uses picomatch, which does not allow brace expansion. We would need to resolve the entire list of files and pass those to the plugin. Since this whole approach is a bit of a bandaid, I propose that we hold off on any more complicated exclusion logic until/unless we hear of problems. Maybe we'll find a better solution entirely that works for more than just the react framework.

@eirslett
Copy link
Collaborator

I think this looks good as a first fix! It solves the 95 % use case, and the rest can be improved upon later?

@eirslett eirslett merged commit dc1fd3f into main Jun 28, 2021
@eirslett eirslett deleted the 3-react-hmr branch June 28, 2021 19:10
offlinehacker added a commit to offlinehacker/storybook-builder-vite that referenced this pull request Jul 15, 2021
Today I spent some time trying to figure out why autoreload for story files doesn't work. This pull request adds remark to README about naming of story files for autoreload to work.

This is related to: storybookjs#53
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.

Help wanted: React HMR is not working properly when making changes to stories
4 participants