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鈥檒l 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 3 commits
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
19 changes: 19 additions & 0 deletions packages/plugin-react-refresh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ export default {

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

## Specifying files to include or exclude from refreshing

By default, @vite/plugin-react-refresh will process files ending with `.js`, `.jsx`, and `.tsx`, and excludes all files in `node_modules`.

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 `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 in order to be processed. Note, when using either `include`, or `exclude`, the defaults will not be merged in, so re-apply them if necessary.
IanVS marked this conversation as resolved.
Show resolved Hide resolved

```js
export default {
plugins: [
reactRefresh({
// Exclude storybook stories and node_modules
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/]
// Only .tsx files
include: '**/*.tsx',
IanVS marked this conversation as resolved.
Show resolved Hide resolved
})
]
}
```

### Notes

- If using TSX, any TS-supported syntax will already be transpiled away so you won't need to specify them here.
Expand Down
4 changes: 3 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,9 @@ type PluginFactory = (options?: Options) => Plugin
declare const createPlugin: PluginFactory & { preambleCode: string }

export interface Options {
parserPlugins: ParserOptions['plugins']
parserPlugins?: ParserOptions['plugins']
include?: string | RegExp | Array<string | RegExp>
exclude?: string | RegExp | Array<string | RegExp>
}

export default createPlugin
8 changes: 6 additions & 2 deletions packages/plugin-react-refresh/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
const fs = require('fs')
const { transformSync, ParserOptions } = require('@babel/core')

const { createFilter } = require('@rollup/pluginutils')
const runtimePublicPath = '/@react-refresh'
const runtimeFilePath = require.resolve(
'react-refresh/cjs/react-refresh-runtime.development.js'
Expand Down Expand Up @@ -37,6 +37,10 @@ window.__vite_plugin_react_preamble_installed__ = true
function reactRefreshPlugin(opts) {
let shouldSkip = false
let base = '/'
const filter = createFilter(
(opts && opts.include) || /\.(?:tsx|jsx|js)$/,
antfu marked this conversation as resolved.
Show resolved Hide resolved
(opts && opts.exclude) || /node_modules/
)

return {
name: 'react-refresh',
Expand Down Expand Up @@ -65,7 +69,7 @@ function reactRefreshPlugin(opts) {
return
}

if (!/\.(t|j)sx?$/.test(id) || id.includes('node_modules')) {
if (!filter(id)) {
return
}

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react-refresh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/core": "^7.14.6",
"@babel/plugin-transform-react-jsx-self": "^7.14.5",
"@babel/plugin-transform-react-jsx-source": "^7.14.5",
"@rollup/pluginutils": "^4.1.0",
"react-refresh": "^0.9.0"
}
}