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

feat(plugin-react-refresh): add include / exclude options #3916

Merged
merged 6 commits into from
Jun 26, 2021

Conversation

IanVS
Copy link
Contributor

@IanVS IanVS commented Jun 23, 2021

Description

This PR unblocks a problem in storybook-builder-vite by allowing us to specify an exclude filter option to the plugin which prevents it from treating story files as HMR boundaries, as discussed in #3778.

I've tested this locally in my own storybook vite project, and it does what we want, allowing the change to storybook story files to propagate into storybook itself.

Additional context

I'd love to hear from anyone who is knowledgable about HMR and in particular getting vite HMR and webpack HMR to play nicely together. 🙏 But this PR is pretty straightforward and I think it could be generally useful in other situations as well, outside of storybook.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

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

I think we could follow the rollup's conversion to have include / exclude pair to construct the filter, same as the vue plugin, ref:

const filter = createFilter(
rawOptions.include || /\.vue$/,
rawOptions.exclude
)

antfu
antfu previously approved these changes Jun 24, 2021
@antfu antfu self-requested a review June 24, 2021 15:21
@IanVS IanVS requested a review from antfu June 24, 2021 15:29
packages/plugin-react-refresh/README.md Outdated Show resolved Hide resolved
packages/plugin-react-refresh/README.md Outdated Show resolved Hide resolved
IanVS and others added 2 commits June 24, 2021 11:52
@IanVS IanVS requested a review from patak-dev June 24, 2021 16:01
@IanVS
Copy link
Contributor Author

IanVS commented Jun 24, 2021

Thanks for the reviews, @patak-js and @antfu, and sorry for my silly mistakes.

packages/plugin-react-refresh/index.js Outdated Show resolved Hide resolved
patak-dev
patak-dev previously approved these changes Jun 24, 2021
@patak-dev patak-dev dismissed their stale review June 24, 2021 18:13

See comment

@antfu antfu changed the title feat: add option to specify files to exclude from plugin-react-refresh feat(plugin-react-refresh): add include / exclude options Jun 26, 2021
@antfu antfu merged commit c0a4ea1 into vitejs:main Jun 26, 2021
@IanVS IanVS deleted the add-react-refresh-exclude branch June 28, 2021 12:49
@fwouts
Copy link
Contributor

fwouts commented Jul 23, 2021

I noticed a behaviour change in this PR that might not have been intentional.

I use a plugin similar to https://github.com/rollup/plugins/blob/master/packages/virtual/src/index.ts that provides virtual files for Vite. IDs need to start with \0 (otherwise Vite expects the file to exist on disk and crashes, see source).

It appears createFilter always returns false for a string that starts with \0, so no files provided by this plugin can be processed by the react-refresh plugin.

This could probably be fixed by stripping any \0 prefix in the ID before passing it to filter(). Would there be any objections if I sent a PR doing that?

@patak-dev
Copy link
Member

We should probably align ourselves with rollup here. But it is strange to me that they would not even let you add these virtual files patterns to include somehow. You can't even opt in. Maybe you could send an issue to https://github.com/rollup/plugins to better understand why this is the case and fix it upstream?

@fwouts
Copy link
Contributor

fwouts commented Jul 23, 2021

Thanks @patak-js, good call :)

Before posting there, here's what I found looking through the history of the Rollup pluginutils package:

In particular, this comment seems to indicate that we're expected to remove \0 on our side.

Do you think this requires further clarification?

@patak-dev
Copy link
Member

Good digging, let's discuss in the PR then 👍🏼
There are other places in Vite where we are using createFilter if you would like to check them out also. This call by rollup seems to make it hard for the ecosystem to play nicely with virtual modules using \0 though, but maybe it is needed.
In the Vite ecosystem, instead of \0 some plugins are using virtual:{package-name}/{virtual-file} because this plays nicely with URLs in dev. Check out https://github.com/hannoeru/vite-plugin-pages/blob/main/src/constants.ts for example. The first id is for back compat, then they started using virtual:. Vite internally needs to convert \0 to another string also for dev.

@fwouts
Copy link
Contributor

fwouts commented Jul 23, 2021

Indeed I was able to get rid of \0 in my plugin with a little effort. In case someone else running into issues in the future is wondering, I had to circumvent injectSourcesContent() which would fail because the path didn't exist by returning { code: ..., map: "{}" } from my plugin's load() function, so map.mappings would be empty.

Given this and the fact that I was probably the only one affected by this, maybe we don't need to fix it after all :)

aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
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.

4 participants