-
Notifications
You must be signed in to change notification settings - Fork 60
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(file-router): avoid errors and full reloads from Vite HMR when a route is added or removed #3367
base: main
Are you sure you want to change the base?
Conversation
…route is added or removed Fixes #2552
fb7dc53
to
f8d4b34
Compare
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3367 +/- ##
==========================================
+ Coverage 86.89% 86.95% +0.05%
==========================================
Files 115 115
Lines 8289 8310 +21
Branches 1271 1283 +12
==========================================
+ Hits 7203 7226 +23
+ Misses 1072 1070 -2
Partials 14 14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Overall, PR looks good. I have only a couple of stylistic changes
// be considered within the HMR update that caused the routes update. | ||
|
||
const mg = this.environment.moduleGraph; | ||
const fileRoutesModules = mg.getModulesByFile(fileURLToPath(runtimeUrls.code).replaceAll('\\', '/'))!; |
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.
const fileRoutesModules = mg.getModulesByFile(fileURLToPath(runtimeUrls.code).replaceAll('\\', '/'))!; | |
const fileRoutesModules = mg.getModulesByFile(fileURLToPath(runtimeUrls.code, { windows: false }))!; |
// Make eager update for file routes in Vite module graph | ||
// for consistency with the generated file contents. | ||
await Promise.all( | ||
[...fileRoutesModules].map(async (fileRouteModule) => { |
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.
[...fileRoutesModules].map(async (fileRouteModule) => { | |
Array.from(fileRoutesModules, async (fileRouteModule) => { |
configResolved({ logger, root, build: { outDir } }) { | ||
const _root = pathToFileURL(root); | ||
const _generatedDir = new URL(generatedDir, _root); | ||
|
||
_viewsDir = new URL(viewsDir, _root); | ||
_viewsDirUsingSlashes = fileURLToPath(_viewsDir, {}).replaceAll('\\', '/'); |
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.
_viewsDirUsingSlashes = fileURLToPath(_viewsDir, {}).replaceAll('\\', '/'); | |
_viewsDirPosix = fileURLToPath(_viewsDir, { windows: false }); |
@@ -62,14 +62,18 @@ export default function vitePluginFileSystemRouter({ | |||
let _outDir: URL; | |||
let _logger: Logger; | |||
let runtimeUrls: RuntimeFileUrls; | |||
let _generateRuntimeFiles: () => Promise<boolean>; | |||
let _viewsDirUsingSlashes: string; |
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.
let _viewsDirUsingSlashes: string; | |
let _viewsDirPosix: string; |
// Updates to file-routes.ts are not processed separately, instead they | ||
// are combined with the update of the triggering file. | ||
expect(mockEnvironment.hot.send).to.not.be.called; | ||
expect(modulesToUpdate).to.be.deep.equal([]); |
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.
expect(modulesToUpdate).to.be.deep.equal([]); | |
expect(modulesToUpdate).to.be.an('array').that.is.empty; |
// HMR for routes and view file together. | ||
expect(modulesToUpdate).to.not.equal(undefined); | ||
expect(modulesToUpdate!.length).to.equal(2); | ||
expect(modulesToUpdate![0]).to.eq(fileRoutesModule); |
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.
Let's not use that weird chai
's aliases 😓 I'm always confused about what they mean. If it is a deep equality, let's have to.be.deep.equal()
, otherwise to.be.equal
.
const modulesToUpdate = await hotUpdate({ type: 'delete', file: viewFilePath, modules: [viewFileModule] }); | ||
expect(mockEnvironment.fetchModule).to.be.calledWith(fileRoutesTsPath); | ||
// HMR for only file routes. | ||
expect(modulesToUpdate).to.not.equal(undefined); |
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.
Here and in other places:
expect(modulesToUpdate).to.not.equal(undefined); | |
expect(modulesToUpdate).to.not.be.undefined; |
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.
Since SonarCloud is upset about repeating code, I'd suggest to either put the repeated part into a separate describe
block with custom beforeEach
, or to put the repeating code into a function.
expect(modulesToUpdate).to.not.equal(undefined); | ||
expect(modulesToUpdate!.length).to.equal(1); | ||
expect(modulesToUpdate![0]).to.eq(fileRoutesModule); |
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.
expect(modulesToUpdate).to.not.equal(undefined); | |
expect(modulesToUpdate!.length).to.equal(1); | |
expect(modulesToUpdate![0]).to.eq(fileRoutesModule); | |
expect(modulesToUpdate).to.be.deep.equal([fileRoutesModule]); |
Fixes #2552