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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/plugin-react-refresh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ export default {

[Full list of Babel parser plugins](https://babeljs.io/docs/en/babel-parser#ecmascript-proposalshttpsgithubcombabelproposals).

## Specifying files to exclude from refreshing

In some situations you may not want a file to act as an HMR boundary, instead preferring that the changes propigate higher in the stack before being handled. In these cases, you can provide an `exclude` function that receives filenames and should return true if that file should not accept hot module replacement.

```js
export default {
plugins: [
reactRefresh({
exclude(filename) {
// Exclude storybook stories
return /\.stories\.(t|j)sx?$/.test(filename)
}
})
]
}
```

### Notes

- If using TSX, any TS-supported syntax will already be transpiled away so you won't need to specify them here.
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-react-refresh/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type PluginFactory = (options?: Options) => Plugin
declare const createPlugin: PluginFactory & { preambleCode: string }

export interface Options {
parserPlugins: ParserOptions['plugins']
parserPlugins?: ParserOptions['plugins']
exclude?: (filename: string) => boolean
IanVS marked this conversation as resolved.
Show resolved Hide resolved
}

export default createPlugin
4 changes: 4 additions & 0 deletions packages/plugin-react-refresh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function reactRefreshPlugin(opts) {
return
}

if (typeof opts?.exclude === 'function' && opts.exclude(id)) {
return
}

/**
* @type ParserOptions["plugins"]
*/
Expand Down