Skip to content

Commit

Permalink
fix: fix the calculation of hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Mar 24, 2023
1 parent 62cc3c4 commit 114af40
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
76 changes: 43 additions & 33 deletions packages/platform-web/src/shuvi-app/dev/hotDevClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ async function tryApplyUpdates(onHotUpdateSuccess) {
}

if (!isUpdateAvailable() || !canApplyUpdates()) {
onBuildOk();
// HMR failed, need to refresh
const hmrStatus = module.hot.status();
if (hmrStatus === 'abort' || hmrStatus === 'fail') {
window.location.reload();
} else {
onBuildOk();
}
return;
}
Expand Down Expand Up @@ -359,37 +360,46 @@ async function tryApplyUpdates(onHotUpdateSuccess) {
}
}

try {
// https://webpack.js.org/api/hot-module-replacement/#check
let updatedModules = await module.hot.check(/* autoApply */ false);

if (!updatedModules) {
return;
}

// if there is another updating, delay the update
// multiple hotupdate occurs during import() will cause hmr error
// so we delay the adjacent hotupdates
// import() == loade module script ----> require(module)
// |
// |
// if applyUpdate happens here, require will cause a error
if (isUpdateAvailable()) {
await new Promise(resolve => {
setTimeout(resolve, 50);
});
}

if (updatedModules) {
if (module.hot.status() !== 'ready') {
await waitForReady();
}

updatedModules = await module.hot.apply();
module.hot.check(/* autoApply */ true).then(
(updatedModules: any) => {
handleApplyUpdates(null, updatedModules);
},
(err: any) => {
handleApplyUpdates(err, null);
}

handleApplyUpdates(null, updatedModules);
} catch (err) {
handleApplyUpdates(err, null);
}
);

// try {
// // https://webpack.js.org/api/hot-module-replacement/#check
// let updatedModules = await module.hot.check(/* autoApply */ false);

// if (!updatedModules) {
// return;
// }

// // if there is another updating, delay the update
// // multiple hotupdate occurs during import() will cause hmr error
// // so we delay the adjacent hotupdates
// // import() == loade module script ----> require(module)
// // |
// // |
// // if applyUpdate happens here, require will cause a error
// if (isUpdateAvailable()) {
// await new Promise(resolve => {
// setTimeout(resolve, 50);
// });
// }

// if (updatedModules) {
// if (module.hot.status() !== 'ready') {
// await waitForReady();
// }

// updatedModules = await module.hot.apply();
// }

// handleApplyUpdates(null, updatedModules);
// } catch (err) {
// handleApplyUpdates(err, null);
// }
}
2 changes: 1 addition & 1 deletion packages/service/src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class WebpackBundler implements Bundler {
);
}

compiler.hooks.beforeCompile.tap('beforeCompile', () => {
compiler.hooks.invalid.tap('invalid', () => {
if (this._startTime === null) {
this._startTime = performance.now();
}
Expand Down

0 comments on commit 114af40

Please sign in to comment.