Skip to content

Commit

Permalink
prevent import.meta.resolve() from being converted to an
Browse files Browse the repository at this point in the history
  `await import()`
  • Loading branch information
jollytoad committed Sep 16, 2024
1 parent e8a57bc commit da195df
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ and this project adheres to

This changelog will need to be split between individual packages

## [0.23.1]

### Fixed

- [@http/generate] prevent `import.meta.resolve()` from being converted to an
`await import()`

## [0.23.0]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@http/discovery": "jsr:@http/discovery@^0.23.0",
"@http/examples": "jsr:@http/examples@^0.23.0",
"@http/fs": "jsr:@http/fs@^0.23.0",
"@http/generate": "jsr:@http/generate@^0.23.0",
"@http/generate": "jsr:@http/generate@^0.23.1",
"@http/host-bun-local": "jsr:@http/host-bun-local@^0.23.0",
"@http/host-cloudflare-worker": "jsr:@http/host-cloudflare-worker@^0.23.0",
"@http/host-deno-deploy": "jsr:@http/host-deno-deploy@^0.23.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ import * as route_methods_1 from "./routes/user/:name/index.ts";
export default cascade(
byPattern("/user/:name{/}?", byMethod(route_methods_1)),
byPattern("/raw", byMethod(route_methods_2)),
byPattern("/page", pageHandler(page_body_3)),
byPattern(
"/page",
pageHandler(page_body_3, import.meta.resolve("./routes/page.ts")),
),
byPattern("/about", byMethod(route_methods_4)),
byPattern("/", route_5),
);
Expand All @@ -105,7 +108,12 @@ export default cascade(
),
byPattern(
"/page",
lazy(async () => pageHandler((await import("./routes/page.ts")).body)),
lazy(async () =>
pageHandler(
(await import("./routes/page.ts")).body,
import.meta.resolve("./routes/page.ts"),
)
),
),
byPattern(
"/about",
Expand Down
3 changes: 2 additions & 1 deletion packages/generate/_test/page_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { html } from "@http/response/html";
*/
export function pageHandler(
body: (req: Request) => string,
modulePath?: string
): (req: Request) => Response {
return (req) =>
html(`
<!DOCTYPE html>
<html>
<body>
<body data-module="${modulePath}">
${body(req)}
</body>
</html>
Expand Down
5 changes: 4 additions & 1 deletion packages/generate/_test/page_handler_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Code, GeneratorOptions, RouteModule } from "@http/generate/types";
import {
asFn,
importNamed,
importResolve,
staticImport,
} from "@http/generate/code-builder";
import { hasBodyFunction } from "$test/generate/page_handler_mapper.ts";
Expand All @@ -25,10 +26,12 @@ export function generate(
"pageHandler",
)));

const modulePath = importResolve(module);

return pageHandler(importNamed(
module,
"body",
`page_body_${i}`,
));
), modulePath);
}
}
7 changes: 6 additions & 1 deletion packages/generate/_test/routes_dynamic_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default cascade(
),
byPattern(
"/page",
lazy(async () => pageHandler((await import("./routes/page.ts")).body)),
lazy(async () =>
pageHandler(
(await import("./routes/page.ts")).body,
import.meta.resolve("./routes/page.ts"),
)
),
),
byPattern(
"/about",
Expand Down
5 changes: 4 additions & 1 deletion packages/generate/_test/routes_static_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import * as route_methods_1 from "./routes/user/:name/index.ts";
export default cascade(
byPattern("/user/:name{/}?", byMethod(route_methods_1)),
byPattern("/raw", byMethod(route_methods_2)),
byPattern("/page", pageHandler(page_body_3)),
byPattern(
"/page",
pageHandler(page_body_3, import.meta.resolve("./routes/page.ts")),
),
byPattern("/about", byMethod(route_methods_4)),
byPattern("/", route_5),
);
2 changes: 1 addition & 1 deletion packages/generate/code-builder/_import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function importResolve(
* Convert an import to an inline dynamic import
*/
export function dynamicImport(imp: Import): Import {
if (imp.inline === false) return imp;
if (imp.inline !== undefined) return imp;

return Object.assign<Import, Partial<Import>>(
imp,
Expand Down
2 changes: 1 addition & 1 deletion packages/generate/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/generate",
"version": "0.23.0",
"version": "0.23.1",
"exports": {
"./code-builder/generate": "./code-builder/generate.ts",
"./code-builder": "./code-builder/mod.ts",
Expand Down

0 comments on commit da195df

Please sign in to comment.