From ab1564542471f9b4575e46f25ca8d9e89f3685fc Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Tue, 7 May 2024 20:04:08 -0400 Subject: [PATCH] fix #898, CJS files were getting loaded as modules hence more cache issues --- .changeset/loud-rice-double.md | 5 +++++ src/server/determine-module-kind.ts | 16 ++++++++++++---- src/server/module-loader.ts | 16 +++++++--------- 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 .changeset/loud-rice-double.md diff --git a/.changeset/loud-rice-double.md b/.changeset/loud-rice-double.md new file mode 100644 index 00000000..806aa067 --- /dev/null +++ b/.changeset/loud-rice-double.md @@ -0,0 +1,5 @@ +--- +"counterfact": patch +--- + +fix #898 hot reloading broken due to cache issues diff --git a/src/server/determine-module-kind.ts b/src/server/determine-module-kind.ts index 554196d0..d6dac95b 100644 --- a/src/server/determine-module-kind.ts +++ b/src/server/determine-module-kind.ts @@ -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 @@ -34,5 +42,5 @@ export async function determineModuleKind(directory: string) { } } - return await determineModuleKind(path.dirname(directory)); + return await determineModuleKind(path.dirname(modulePath)); } diff --git a/src/server/module-loader.ts b/src/server/module-loader.ts index 1484acab..89f242a7 100644 --- a/src/server/module-loader.ts +++ b/src/server/module-loader.ts @@ -38,7 +38,7 @@ export class ModuleLoader extends EventTarget { private readonly dependencyGraph = new ModuleDependencyGraph(); - private uncachedImport: (moduleName: string) => Promise = + private readonly uncachedImport: (moduleName: string) => Promise = // eslint-disable-next-line @typescript-eslint/require-await async function (moduleName: string) { throw new Error(`uncachedImport not set up; importing ${moduleName}`); @@ -101,11 +101,6 @@ export class ModuleLoader extends EventTarget { } public async load(directory = ""): Promise { - const moduleKind = await determineModuleKind(this.basePath); - - this.uncachedImport = - moduleKind === "module" ? uncachedImport : uncachedRequire; - if ( !existsSync(nodePath.join(this.basePath, directory).replaceAll("\\", "/")) ) { @@ -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"));