Skip to content

Commit c0a4ea1

Browse files
IanVSpatak-dev
andauthored
feat(plugin-react-refresh): add include / exclude options (#3916)
Co-authored-by: patak <[email protected]>
1 parent 7381d27 commit c0a4ea1

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

packages/plugin-react-refresh/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ export default {
2727

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

30+
## Specifying files to include or exclude from refreshing
31+
32+
By default, @vite/plugin-react-refresh will process files ending with `.js`, `.jsx`, `.ts`, and `.tsx`, and excludes all files in `node_modules`.
33+
34+
In some situations you may not want a file to act as an HMR boundary, instead preferring that the changes propagate higher in the stack before being handled. In these cases, you can provide an `include` and/or `exclude` option, which can be regex or a [picomatch](https://github.com/micromatch/picomatch#globbing-features) pattern, or array of either. Files must match include and not exclude to be processed. Note, when using either `include`, or `exclude`, the defaults will not be merged in, so re-apply them if necessary.
35+
36+
```js
37+
export default {
38+
plugins: [
39+
reactRefresh({
40+
// Exclude storybook stories and node_modules
41+
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],
42+
// Only .tsx files
43+
include: '**/*.tsx'
44+
})
45+
]
46+
}
47+
```
48+
3049
### Notes
3150

3251
- If using TSX, any TS-supported syntax will already be transpiled away so you won't need to specify them here.

packages/plugin-react-refresh/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ type PluginFactory = (options?: Options) => Plugin
66
declare const createPlugin: PluginFactory & { preambleCode: string }
77

88
export interface Options {
9-
parserPlugins: ParserOptions['plugins']
9+
parserPlugins?: ParserOptions['plugins']
10+
include?: string | RegExp | Array<string | RegExp>
11+
exclude?: string | RegExp | Array<string | RegExp>
1012
}
1113

1214
export default createPlugin

packages/plugin-react-refresh/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22
const fs = require('fs')
33
const { transformSync, ParserOptions } = require('@babel/core')
4-
4+
const { createFilter } = require('@rollup/pluginutils')
55
const runtimePublicPath = '/@react-refresh'
66
const runtimeFilePath = require.resolve(
77
'react-refresh/cjs/react-refresh-runtime.development.js'
@@ -37,6 +37,10 @@ window.__vite_plugin_react_preamble_installed__ = true
3737
function reactRefreshPlugin(opts) {
3838
let shouldSkip = false
3939
let base = '/'
40+
const filter = createFilter(
41+
(opts && opts.include) || /\.(t|j)sx?$/,
42+
(opts && opts.exclude) || /node_modules/
43+
)
4044

4145
return {
4246
name: 'react-refresh',
@@ -65,7 +69,7 @@ function reactRefreshPlugin(opts) {
6569
return
6670
}
6771

68-
if (!/\.(t|j)sx?$/.test(id) || id.includes('node_modules')) {
72+
if (!filter(id)) {
6973
return
7074
}
7175

packages/plugin-react-refresh/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@babel/core": "^7.14.6",
3030
"@babel/plugin-transform-react-jsx-self": "^7.14.5",
3131
"@babel/plugin-transform-react-jsx-source": "^7.14.5",
32+
"@rollup/pluginutils": "^4.1.0",
3233
"react-refresh": "^0.9.0"
3334
}
3435
}

0 commit comments

Comments
 (0)