-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: invalidate message and fix HMR for HOC, class component & style…
…d component
- Loading branch information
1 parent
545aa67
commit e20e791
Showing
16 changed files
with
195 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { copyFileSync } from 'node:fs' | ||
|
||
copyFileSync('src/refreshUtils.js', 'dist/refreshUtils.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
function debounce(fn, delay) { | ||
let handle | ||
return () => { | ||
clearTimeout(handle) | ||
handle = setTimeout(fn, delay) | ||
} | ||
} | ||
|
||
const enqueueUpdate = debounce(exports.performReactRefresh, 16) | ||
|
||
// Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141 | ||
// This allows to resister components not detected by SWC like styled component | ||
function registerExportsForReactRefresh(filename, moduleExports) { | ||
for (const key in moduleExports) { | ||
if (key === '__esModule') continue | ||
const exportValue = moduleExports[key] | ||
if (exports.isLikelyComponentType(exportValue)) { | ||
exports.register(exportValue, filename + ' ' + key) | ||
} | ||
} | ||
} | ||
|
||
function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) { | ||
if (!predicateOnExport(prevExports, (key) => !!nextExports[key])) { | ||
return 'Could not Fast Refresh (export removed)' | ||
} | ||
|
||
let hasExports = false | ||
const allExportsAreComponentsOrUnchanged = predicateOnExport( | ||
nextExports, | ||
(key, value) => { | ||
hasExports = true | ||
if (exports.isLikelyComponentType(value)) return true | ||
if (!prevExports[key]) return false | ||
return prevExports[key] === nextExports[key] | ||
}, | ||
) | ||
if (hasExports && allExportsAreComponentsOrUnchanged) { | ||
enqueueUpdate() | ||
} else { | ||
return 'Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react#consistent-components-exports' | ||
} | ||
} | ||
|
||
function predicateOnExport(moduleExports, predicate) { | ||
for (const key in moduleExports) { | ||
if (key === '__esModule') continue | ||
const desc = Object.getOwnPropertyDescriptor(moduleExports, key) | ||
if (desc && desc.get) return false | ||
if (!predicate(key, moduleExports[key])) return false | ||
} | ||
return true | ||
} | ||
|
||
exports.registerExportsForReactRefresh = registerExportsForReactRefresh | ||
exports.validateRefreshBoundaryAndEnqueueUpdate = | ||
validateRefreshBoundaryAndEnqueueUpdate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import styled from '@emotion/styled' | ||
import { css } from '@emotion/react' | ||
import { useState } from 'react' | ||
|
||
// Ensure HMR of styled component alongside other components | ||
export const StyledCode = styled.code` | ||
color: #646cff; | ||
` | ||
|
||
export function Counter() { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<button | ||
css={css` | ||
border: 2px solid #000; | ||
`} | ||
onClick={() => setCount((count) => count + 1)} | ||
> | ||
count is: {count} | ||
</button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.