Skip to content

Commit 631d537

Browse files
committed
Fix loader build
1 parent 5150d5d commit 631d537

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

server/build.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ export async function build(serverEntry?: string) {
6060
moduleLoaders.length > 0 &&
6161
`import { globToRegExp } from "https://deno.land/[email protected]/path/mod.ts";const moduleLoaders = []; globalThis["__ALEPH_MODULE_LOADERS"] = moduleLoaders;`,
6262
moduleLoaders.length > 0 &&
63-
moduleLoaders.map(({ meta }, idx) => `
64-
import loader_${idx} from ${JSON.stringify(meta.src)};
65-
const reg = globToRegExp(${JSON.stringify(meta.glob)});
66-
moduleLoaders.push({
67-
meta: ${JSON.stringify(meta)},
68-
test: (pathname) => {
69-
return reg.test(pathname) && loader_${idx}.test(pathname);
70-
},
71-
load: (pathname, env) => loader_${idx}.load(pathname, env),
72-
})
73-
`).join("\n"),
63+
moduleLoaders.map((loader, idx) => {
64+
const meta = Reflect.get(loader, "meta");
65+
return `
66+
import loader$${idx} from ${JSON.stringify(meta.src)};
67+
{
68+
const reg = globToRegExp(${JSON.stringify(meta.glob)});
69+
let loader = loader$${idx};
70+
if (typeof loader === "function") {
71+
loader = new loader();
72+
}
73+
moduleLoaders.push({
74+
meta: ${JSON.stringify(meta)},
75+
test: (pathname) => {
76+
return reg.test(pathname) && loader.test(pathname);
77+
},
78+
load: (pathname, env) => loader.load(pathname, env),
79+
})
80+
}
81+
`;
82+
}).join("\n"),
7483
...routeFiles.map(([filename, exportNames], idx) => {
7584
const hasDefaultExport = exportNames.includes("default");
7685
const hasDataExport = exportNames.includes("data");
@@ -145,8 +154,13 @@ export async function build(serverEntry?: string) {
145154
name: "aleph-esbuild-plugin",
146155
setup(build) {
147156
build.onResolve({ filter: /.*/ }, (args) => {
148-
const isRemote = util.isLikelyHttpURL(args.path);
149-
const [path] = util.splitBy(isRemote ? args.path : util.trimPrefix(args.path, "file://"), "#");
157+
let importUrl = args.path;
158+
if (importUrl in importMap.imports) {
159+
importUrl = importMap.imports[importUrl];
160+
}
161+
162+
const isRemote = util.isLikelyHttpURL(importUrl);
163+
const [path] = util.splitBy(isRemote ? importUrl : util.trimPrefix(importUrl, "file://"), "#");
150164

151165
if (args.kind === "dynamic-import") {
152166
return { path, external: true };

server/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ export async function initModuleLoaders(importMap: ImportMap): Promise<ModuleLoa
182182
) {
183183
const glob = "/**/" + key;
184184
const reg = globToRegExp(glob);
185-
loaders.push({
185+
const Loader = {
186186
meta: { src, glob },
187187
test: (pathname: string) => {
188188
return reg.test(pathname) && loader.test(pathname);
189189
},
190190
load: (pathname: string, env: Record<string, unknown>) => loader.load(pathname, env),
191-
});
191+
};
192+
loaders.push(Loader);
192193
}
193194
}
194195
}

server/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export type ImportMap = {
4848
};
4949

5050
export type ModuleLoader = {
51-
readonly meta: { src: string; glob: string };
5251
test(pathname: string): boolean;
5352
load(pathname: string, env: ModuleLoaderEnv): Promise<ModuleLoaderContent> | ModuleLoaderContent;
5453
};

0 commit comments

Comments
 (0)