Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 22, 2024
1 parent 17d5d0b commit 108e588
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
35 changes: 27 additions & 8 deletions lib/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ export function hmr(enableViteHmrForModes: string[] = ['development']): Plugin {
name: 'hmr-plugin',
enforce: 'post',
configResolved(config) {
process.env['EMBER_VITE_HMR_ENABLED'] = enableViteHmrForModes.includes(config.mode).toString()
process.env['EMBER_VITE_HMR_ENABLED'] = enableViteHmrForModes
.includes(config.mode)
.toString();
},
resolveId(id) {
if (id.startsWith('/@id/embroider_virtual:')) {
return this.resolve(id.replace('/@id/', ''), path.join(process.cwd(), 'package.json'));
return this.resolve(
id.replace('/@id/', ''),
path.join(process.cwd(), 'package.json'),
);
}
if (id === '/ember-vite-hmr/services/vite-hot-reload') {
return this.resolve(
Expand All @@ -30,11 +35,23 @@ export function hmr(enableViteHmrForModes: string[] = ['development']): Plugin {
return ctx.modules;
}
const otherModules = [];
const pairedModule = ctx.modules.find(m => [...m.importers].find(i => i.id.startsWith('embroider_virtual') && i.id.endsWith('-embroider-pair-component')));
const pairedModule = ctx.modules.find((m) =>
[...m.importers].find(
(i) =>
i.id.startsWith('embroider_virtual') &&
i.id.endsWith('-embroider-pair-component'),
),
);
if (pairedModule) {
const pairComponent = [...pairedModule.importers].find(i => i.id.startsWith('embroider_virtual') && i.id.endsWith('-embroider-pair-component'));
const pairComponent = [...pairedModule.importers].find(
(i) =>
i.id.startsWith('embroider_virtual') &&
i.id.endsWith('-embroider-pair-component'),
);
if (pairComponent) {
const componentModule = [...pairComponent.clientImportedModules].find(cim => cim.id.split('?')[0].match(/\/component\.(js|ts|gjs|gts)/));
const componentModule = [...pairComponent.clientImportedModules].find(
(cim) => cim.id.split('?')[0].match(/\/component\.(js|ts|gjs|gts)/),
);
if (componentModule) {
otherModules.push(componentModule);
}
Expand All @@ -56,7 +73,7 @@ export function hmr(enableViteHmrForModes: string[] = ['development']): Plugin {
if (resourcePath.includes('/-components/')) {
return source;
}
const name = require(`${process.cwd()}/package.json`).name;
const name = require(`${process.cwd()}/package.json`).name;
if (resourcePath.includes(`/assets/${name}.js`)) {
source += `\nimport.meta.hot.accept();`;
}
Expand All @@ -72,7 +89,10 @@ export function hmr(enableViteHmrForModes: string[] = ['development']): Plugin {
const dep = resultElement[1];
const resolved = await this.resolve(dep, resourcePath, {});
let id = resolved.id;
if (id.includes('rewritten-app') && !id.startsWith('embroider_virtual:')) {
if (
id.includes('rewritten-app') &&
!id.startsWith('embroider_virtual:')
) {
id = id.split('rewritten-app')[1];
}
if (id.startsWith('embroider_virtual:')) {
Expand All @@ -92,7 +112,6 @@ export function hmr(enableViteHmrForModes: string[] = ['development']): Plugin {
`import.meta.hot.accept('${id}'`,
);
}
return source;
}
if (
!supportedPaths.some((s) => resourcePath.includes(`/${s}/`)) &&
Expand Down
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.5",
"version": "1.4.6",
"keywords": [
"ember-addon"
],
Expand Down
18 changes: 10 additions & 8 deletions src/services/vite-hot-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getOwner } from '@ember/owner';
import RouterService from '@ember/routing/router-service';
import Router from '@ember/routing/route';
import Controller from '@ember/controller';
import ApplicationInstance from "@ember/application/instance";
import ApplicationInstance from '@ember/application/instance';

const ChangeMap = new WeakMap();

Expand Down Expand Up @@ -33,19 +33,21 @@ if (import.meta.hot) {
register(module: Module, dep: string, callback: Function) {
dep = dep.replace(new RegExp(`^${modulePrefix}/`), './');
this.moduleDepCallbacks[module.id]![dep] =
this.moduleDepCallbacks[module.id]![dep] || [] as Function[];
this.moduleDepCallbacks[module.id]![dep] || ([] as Function[]);
this.moduleDepCallbacks[module.id]![dep]!.push(callback);
},
loadNew(oldModule: Module, newModule: Module) {
this.versionMap[newModule.id] = newModule.version;
const entry = Object.values(requirejs.entries as Record<string, ReqJSEntry>).find(
const entry = Object.values(
requirejs.entries as Record<string, ReqJSEntry>,
).find(
(module) => module.module.exports.default === oldModule.exports.default,
);
if (!entry) return;
entry.module = {
id: newModule.id,
exports: newModule.exports,
version: newModule.version
version: newModule.version,
};
},

Expand All @@ -59,7 +61,7 @@ if (import.meta.hot) {
const module: Module = {
exports: m,
id: moduleUrl.split('?')[0]!,
version: 0
version: 0,
};
this._accepting -= 1;
if (this._accepting === 0) {
Expand Down Expand Up @@ -118,7 +120,7 @@ export default class ViteHotReloadService extends Service {
if (import.meta.hot) {
import.meta.hot.on('vite:beforeUpdate', (options) => {
options.updates = options.updates.filter(
(u: any) => !u.path.startsWith(`/assets/${modulePrefix}.js`),
(u: any) => !u.path.startsWith(`/assets/${modulePrefix}.js`),
);
});
}
Expand All @@ -131,7 +133,7 @@ export default class ViteHotReloadService extends Service {
this,
`${name}--hot-version--${window.emberHotReloadPlugin.version}`,
);
route.fullRouteName = route.fullRouteName.replace(
route.fullRouteName = `route:${name}`.replace(
/--hot-version--.*$/,
'',
);
Expand Down Expand Up @@ -214,4 +216,4 @@ declare module '@ember/service' {
interface Registry {
'hot-reload': ViteHotReloadService;
}
}
}

0 comments on commit 108e588

Please sign in to comment.