Skip to content

Commit

Permalink
use routerVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 22, 2024
1 parent 108e588 commit a243697
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-vite-hmr",
"version": "1.4.6",
"version": "1.4.7",
"keywords": [
"ember-addon"
],
Expand Down
18 changes: 17 additions & 1 deletion src/services/vite-hot-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (import.meta.hot) {
changed: {},
subscribers: [],
version: 1,
routerVersion: 1,
moduleDepCallbacks: {},
versionMap: {},

Expand Down Expand Up @@ -131,7 +132,7 @@ export default class ViteHotReloadService extends Service {
v.getRoute = function (name: string) {
const route = getRoute.call(
this,
`${name}--hot-version--${window.emberHotReloadPlugin.version}`,
`${name}--hot-version--${window.emberHotReloadPlugin.routerVersion}`,
);
route.fullRouteName = `route:${name}`.replace(
/--hot-version--.*$/,
Expand All @@ -147,6 +148,21 @@ export default class ViteHotReloadService extends Service {
});
this.container = (getOwner(this) as any)?.__container__;
window.emberHotReloadPlugin.subscribe((oldModule, newModule) => {
let changed = false;
if (oldModule.exports.default?.prototype && oldModule.exports.default.prototype instanceof Router) {
changed = true;
}
if (oldModule.exports.default?.prototype && oldModule.exports.default.prototype instanceof Controller) {
changed = true;
}
if (oldModule.id.startsWith('./templates/') && !oldModule.id.startsWith('./templates/components/')) {
changed = true;
}
if (oldModule.id.startsWith(`./${podModulePrefix}/`)) {
changed = true;
}
if (!changed) return;
window.emberHotReloadPlugin.routerVersion += 1;
const types = [
'route',
'controller',
Expand Down
5 changes: 3 additions & 2 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ declare module 'thread-loader' {
declare module '*.hbs';

type ReqJSEntry = {
module: Module
}
module: Module;
};

type Module = {
id: string;
Expand All @@ -19,6 +19,7 @@ interface Window {
subscribers: any;
loadNew(old: any, _new: any): unknown;
version: number;
routerVersion: number;
changed: Record<string, any>;
notifyNew(): unknown;
register: any;
Expand Down

0 comments on commit a243697

Please sign in to comment.