Skip to content

Commit

Permalink
with loadEndpoint() taking one argument I think this will work now
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Apr 30, 2024
1 parent b2431f0 commit e8b9e7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
29 changes: 17 additions & 12 deletions src/server/module-loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { once } from "node:events";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import nodePath, { basename } from "node:path";
import nodePath, { basename, dirname } from "node:path";

import { type FSWatcher, watch } from "chokidar";
import createDebug from "debug";
Expand Down Expand Up @@ -90,7 +90,7 @@ export class ModuleLoader extends EventTarget {

dependencies.add(pathName);
for (const dependency of dependencies) {
void this.loadEndpoint(dependency, parts.dir, url);
void this.loadEndpoint(dependency);
}
},
);
Expand Down Expand Up @@ -139,23 +139,28 @@ export class ModuleLoader extends EventTarget {
.join(this.basePath, directory, file.name)
.replaceAll("\\", "/");

const url = `/${nodePath.join(
directory,
nodePath.parse(basename(fullPath)).name,
)}`
.replaceAll("\\", "/")
.replaceAll(/\/+/gu, "/");

await this.loadEndpoint(fullPath, directory, url);
await this.loadEndpoint(fullPath);
});

await Promise.all(imports);
}

// eslint-disable-next-line max-statements
private async loadEndpoint(pathName: string, directory: string, url: string) {
private async loadEndpoint(pathName: string) {
debug("importing module: %s", pathName);

const directory = dirname(pathName.slice(this.basePath.length)).replaceAll(
"\\",
"/",
);

const url = `/${nodePath.join(
directory,
nodePath.parse(basename(pathName)).name,
)}`
.replaceAll("\\", "/")
.replaceAll(/\/+/gu, "/");

this.dependencyGraph.load(pathName);

try {
Expand All @@ -169,7 +174,7 @@ export class ModuleLoader extends EventTarget {
if (basename(pathName).startsWith("_.context")) {
if (isContextModule(endpoint)) {
this.contextRegistry.update(
`/${directory.replaceAll("\\", "/")}`.replaceAll(/\/+/gu, "/"),
directory,

// @ts-expect-error TS says Context has no constructable signatures but that's not true?
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Expand Down
24 changes: 1 addition & 23 deletions test/server/module-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,6 @@ describe("a module loader", () => {
expect(registry.exists("GET", "/a/b/c")).toBe(true);
});
});
it("updates the registry when a file is added", async () => {
await usingTemporaryFiles(async ($) => {
await $.add("package.json", '{ "type": "module" }');
const registry: Registry = new Registry();
const loader: ModuleLoader = new ModuleLoader($.path("."), registry);

await loader.load();
await loader.watch();

expect(registry.exists("GET", "/late/addition")).toBe(false);

await $.add(
"late/addition.js",
'export function GET() { return { body: "I\'m here now!" }; }',
);
await once(loader, "add");

expect(registry.exists("GET", "/late/addition")).toBe(true);

await loader.stopWatching();
});
});

it("updates the registry when a file is deleted", async () => {
await usingTemporaryFiles(async ($) => {
await $.add(
Expand Down Expand Up @@ -193,6 +170,7 @@ describe("a module loader", () => {

const loader: ModuleLoader = new ModuleLoader(
$.path("."),

registry,
contextRegistry,
);
Expand Down

0 comments on commit e8b9e7a

Please sign in to comment.