Skip to content

Commit

Permalink
Merge pull request #899 from pmcelhaney/context-reload-issues
Browse files Browse the repository at this point in the history
fix #898, CJS files were getting loaded as modules hence more cache issues
  • Loading branch information
pmcelhaney authored May 8, 2024
2 parents 7141200 + ab15645 commit 6f5599d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-rice-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fix #898 hot reloading broken due to cache issues
16 changes: 12 additions & 4 deletions src/server/determine-module-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ interface PackageJsonWithType {
type?: string;
}
// eslint-disable-next-line max-statements
export async function determineModuleKind(directory: string) {
const packageJsonPath = path.join(directory, "package.json");
export async function determineModuleKind(modulePath: string) {
if (modulePath.endsWith(".cjs")) {
return "commonjs";
}

if (modulePath.endsWith(".mjs")) {
return "module";
}

if (directory === path.parse(directory).root) {
if (modulePath === path.parse(modulePath).root) {
return DEFAULT_MODULE_KIND;
}

const packageJsonPath = path.join(modulePath, "package.json");

if (existsSync(packageJsonPath)) {
try {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
Expand All @@ -34,5 +42,5 @@ export async function determineModuleKind(directory: string) {
}
}

return await determineModuleKind(path.dirname(directory));
return await determineModuleKind(path.dirname(modulePath));
}
16 changes: 7 additions & 9 deletions src/server/module-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ModuleLoader extends EventTarget {

private readonly dependencyGraph = new ModuleDependencyGraph();

private uncachedImport: (moduleName: string) => Promise<unknown> =
private readonly uncachedImport: (moduleName: string) => Promise<unknown> =
// eslint-disable-next-line @typescript-eslint/require-await
async function (moduleName: string) {
throw new Error(`uncachedImport not set up; importing ${moduleName}`);
Expand Down Expand Up @@ -101,11 +101,6 @@ export class ModuleLoader extends EventTarget {
}

public async load(directory = ""): Promise<void> {
const moduleKind = await determineModuleKind(this.basePath);

this.uncachedImport =
moduleKind === "module" ? uncachedImport : uncachedRequire;

if (
!existsSync(nodePath.join(this.basePath, directory).replaceAll("\\", "/"))
) {
Expand Down Expand Up @@ -163,10 +158,13 @@ export class ModuleLoader extends EventTarget {
this.dependencyGraph.load(pathName);

try {
const doImport =
(await determineModuleKind(pathName)) === "commonjs"
? uncachedRequire
: uncachedImport;

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const endpoint = (await this.uncachedImport(pathName)) as
| ContextModule
| Module;
const endpoint = (await doImport(pathName)) as ContextModule | Module;

this.dispatchEvent(new Event("add"));

Expand Down

0 comments on commit 6f5599d

Please sign in to comment.