Skip to content

Commit

Permalink
Fix HMR Regression (#1642)
Browse files Browse the repository at this point in the history
* Fix HMR Regression

Previously changed dependencies will not be regressed when their dependent changes

* Allow HMR accepted nodes to reload as before

* Fix snapshot
  • Loading branch information
aaronjensen committed Dec 6, 2020
1 parent a05e56c commit 01fb71d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 11 additions & 3 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
const allowStale = _allowStale ?? false;
const encoding = _encoding ?? null;
const reqUrlHmrParam = reqUrl.includes('?mtime=') && reqUrl.split('?')[1];
const mtime = reqUrlHmrParam && reqUrlHmrParam.split('=')[1];
let reqPath = decodeURI(url.parse(reqUrl).pathname!);
const originalReqPath = reqPath;
let isProxyModule = false;
Expand Down Expand Up @@ -804,13 +805,20 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
}

let code = wrappedResponse;
if (responseFileExt === '.js' && reqUrlHmrParam)
if (responseFileExt === '.js' && mtime)
code = await transformEsmImports(code as string, (imp) => {
const importUrl = path.posix.resolve(path.posix.dirname(reqPath), imp);
const node = hmrEngine.getEntry(importUrl);
if (node && node.needsReplacement) {
if (node == null) return imp;
if (node.needsReplacement) {
hmrEngine.markEntryForReplacement(node, false);
return `${imp}?${reqUrlHmrParam}`;
node.mtime = mtime;
}
if (node.mtime && !node.isHmrAccepted) {
return `${imp}?mtime=${node.mtime}`;
}
if (node.needsReplacement) {
return `${imp}?mtime=${mtime}`;
}
return imp;
});
Expand Down
2 changes: 2 additions & 0 deletions snowpack/src/hmr-server-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface Dependency {
isHmrAccepted: boolean;
needsReplacement: boolean;
needsReplacementCount: number;
mtime: string | null;
}

type HMRMessage =
Expand Down Expand Up @@ -103,6 +104,7 @@ export class EsmHmrEngine {
needsReplacementCount: 0,
isHmrEnabled: false,
isHmrAccepted: false,
mtime: null,
};
this.dependencyTree.set(sourceUrl, newEntry);
return newEntry;
Expand Down

1 comment on commit 01fb71d

@vercel
Copy link

@vercel vercel bot commented on 01fb71d Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.