-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
fix: invalidate module cache on unlinked #2629
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useState } from 'react' | ||
import Child from './Child' | ||
|
||
function App() { | ||
return ( | ||
<div className="App"> | ||
<Child /> | ||
</div> | ||
) | ||
} | ||
|
||
export default App |
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 @@ | ||
export default function Child() { | ||
return <p>Child state 1</p> | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/playground/file-delete-restore/__tests__/file-delete-restore.spec.ts
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,61 @@ | ||
import { | ||
editFile, | ||
untilUpdated, | ||
removeFile, | ||
addFile, | ||
isBuild | ||
} from '../../testUtils' | ||
|
||
if (!isBuild) { | ||
test('should hmr when file is deleted and restored', async () => { | ||
await untilUpdated(() => page.textContent('p'), 'Child state 1') | ||
|
||
editFile('Child.jsx', (code) => | ||
code.replace('Child state 1', 'Child state 2') | ||
) | ||
|
||
await untilUpdated(() => page.textContent('p'), 'Child state 2') | ||
|
||
editFile('App.jsx', (code) => | ||
code | ||
.replace(`import Child from './Child'`, '') | ||
.replace(`<Child />`, '<p>Child deleted</p>') | ||
) | ||
removeFile('Child.jsx') | ||
await untilUpdated(() => page.textContent('p'), 'Child deleted') | ||
|
||
// restore Child.jsx | ||
addFile( | ||
'Child.jsx', | ||
` export default function Child() { | ||
return <p>Child state 1</p> | ||
} | ||
` | ||
) | ||
|
||
// restore App.jsx | ||
editFile( | ||
'App.jsx', | ||
(code) => | ||
`import { useState } from 'react' | ||
import Child from './Child' | ||
|
||
function App() { | ||
return ( | ||
<div className="App"> | ||
<Child /> | ||
</div> | ||
) | ||
} | ||
|
||
export default App | ||
` | ||
) | ||
|
||
await untilUpdated(() => page.textContent('p'), 'Child state 1') | ||
}) | ||
} else { | ||
test('dummy test to make jest happy', async () => { | ||
// Your test suite must contain at least one test. | ||
}) | ||
} |
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,8 @@ | ||
<div id="app"></div> | ||
<script type="module"> | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './App.jsx' | ||
|
||
ReactDOM.render(React.createElement(App), document.getElementById('app')) | ||
</script> |
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,18 @@ | ||
{ | ||
"name": "test-file-delete-restore", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../vite/bin/vite", | ||
"serve": "vite preview" | ||
}, | ||
"dependencies": { | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1" | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-react-refresh": "^1.3.1" | ||
} | ||
} |
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,15 @@ | ||
const reactRefresh = require('@vitejs/plugin-react-refresh') | ||
|
||
/** | ||
* @type {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
plugins: [reactRefresh()], | ||
build: { | ||
// to make tests faster | ||
minify: false | ||
}, | ||
esbuild: { | ||
jsxInject: `import React from 'react'` | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you are right with line 179
But we could split this
else
branch and convert it to anif
So if the first if fulfills, then we don't need to call
getModulesByFile
to earlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shinigami92 that would change the logic, in the current state modules returned by getModulesByFile are always updated. Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. The
updateModules
should always be called. I think the current logic is correct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay sorry on my side, the code seems really hard to read with all the nested curly braces 🤔
So how else can we achieve it to not call the
getModulesByFile
to early and safe time that wayOkay, completely new rewrite on my side
What do you think about this one?:
IMO it makes the code a bit more readable and track whats going on
modules
is called as lately as possibleI know now that it doesn't change runtime performance, but it's a bit more readable that way
i
and collects all needed modules together and then pushes these at onceI don't know if the local variable
needsUnlink
is the best name here, but from what I read here, I think it is an okay-ish namePlease double check if I didn't broke anything again 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the spacing. About adding
needsUnlink
to delay themodules
init, I would go there when the logic is a bit more complex. I prefer the current branch but I am not opposed to the change.For the internal for loop, I find the previous one more readable. Maybe a for of could be used if you want to avoid the need for indexing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic looks ok but I don't think such a rewrite is necessary.
I think the two ways have same readability. Declaring variables upfront is a common practice.
I think the if-else is better because it is clear in one sight that only one branch will get executed.
I personly prefer the previous way. Maybe the imperative programing style is more intuitive to me.
I want to keep the change minimal so that we don't erase the previous git line blame.