Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support root src path("@/":"./src/") #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"test": "deno task test:unit && deno task test:fixture && deno task test:examples",
"test:unit": "deno test -A ./test/unit",
"test:fixture": "cd ./test/fixture && deno test -A",
"test:examples": "deno run -A ./tools/test-examples.ts basic with-csr with-unocss with-wouter",
"test:examples": "deno run -A ./tools/test-examples.ts basic with-csr with-unocss with-wouter with-react-router",
"version-bump:minor": "deno run -A tools/patch.ts --release=minor",
"version-bump:patch": "deno run -A tools/patch.ts --release=patch"
},
Expand Down
3 changes: 2 additions & 1 deletion examples/with-react-router/importMap.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"react-dom/client": "https://esm.sh/v106/[email protected]/client?dev",
"ultra/": "https://deno.land/x/[email protected]/",
"react-router-dom": "https://esm.sh/v106/[email protected]?external=react",
"react-router-dom/server": "https://esm.sh/v106/[email protected]/server?external=react"
"react-router-dom/server": "https://esm.sh/v106/[email protected]/server?external=react",
"@/": "./src/"
}
}
21 changes: 21 additions & 0 deletions examples/with-react-router/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import server from "./server.tsx";

/**
* This is here as an example of how to test your
* server/rendering.
*/
Deno.test("it works", async (t) => {
await t.step("it can render the AboutPage", async () => {
const response = await server.request("http://localhost/about");
const content = await response.text();

assertEquals(response.status, 200);
assertEquals(
response.headers.get("content-type"),
"text/html; charset=utf-8",
);

assertEquals(content.includes(`<div>About page</div>`), true);
});
});
6 changes: 5 additions & 1 deletion examples/with-react-router/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ server.get("*", async (context) => {
});
});

serve(server.fetch);
if (import.meta.main) {
serve(server.fetch);
}

export default server;
7 changes: 4 additions & 3 deletions examples/with-react-router/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { lazy, Suspense } from "react";
import { Route, Routes } from "react-router-dom";
import useServerContext from "ultra/hooks/use-server-context.js";
import { DefaultLayout } from "./layouts/DefaultLayout.tsx";
import { DefaultLayout } from "@/layouts/DefaultLayout.tsx";

const HomePage = lazy(() => import("./pages/Home.tsx"));
const AboutPage = lazy(() => import("./pages/About.tsx"));
const HomePage = lazy(() => import("@/pages/Home.tsx"));
const AboutPage = lazy(() => import("@/pages/About.tsx"));

function RouteNotFound() {
useServerContext((context) => {
Expand All @@ -26,6 +26,7 @@ export default function App() {
<Suspense fallback={<div>Page is Loading...</div>}>
<Routes>
<Route path="/" element={<DefaultLayout />}>
{/** @ts-ignore TS2590 [ERROR]: Expression produces a union type that is too complex to represent: https://github.com/microsoft/TypeScript/issues/42790 */}
<Route index element={<HomePage />} />
<Route path="about" element={<AboutPage />} />
<Route path="*" element={<RouteNotFound />} />
Expand Down
4 changes: 2 additions & 2 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function createServer(
cache: mode !== "development",
}));

// Serve anything else static at "/"
// Serve static at "/ultra/"
// deno-fmt-ignore
server.get("/ultra/*", (
context: Context,
Expand All @@ -81,7 +81,7 @@ export async function createServer(
})(context,next);
});

// Serve anything else static at "/"
// Serve anything else
// deno-fmt-ignore
server.get("*", serveStatic({
root: resolve(root, "./"),
Expand Down
4 changes: 3 additions & 1 deletion lib/ultra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class UltraServer extends Hono {
#importMapHandler(importMap: ImportMap | undefined) {
if (importMap?.imports) {
const ultraUrl = importMap.imports["ultra/"];
// Set importMap for ultra/ framework
//1. Set importMap for ultra/ framework
if (ultraUrl && !ultraUrl.startsWith("http")) {
if (ultraUrl.startsWith("/")) {
this.ultraDir = ultraUrl;
Expand All @@ -164,6 +164,8 @@ export class UltraServer extends Hono {
}
importMap.imports["ultra/"] = "/ultra/";
}
//2. @/ path
importMap.imports["@/"] = "/_ultra/compiler/src/";
}
}

Expand Down
16 changes: 15 additions & 1 deletion tools/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ type ImportMap = {
* would like to work on, generate a `deno.dev.json` and `importMap.dev.json`
* in that examples project directory, and run the ./server.tsx in dev mode.
*/
interface DenoConfig {
tasks: Record<string, string>;
lock?: boolean;
compilerOptions: Record<string, string>;
importMap: string;
}
export async function initExampleConfig(example: string) {
try {
const examplePath = join("examples", example);
const devConfigPath = join(examplePath, "deno.dev.json");
const devImportMapPath = join(examplePath, "importMap.dev.json");

const config: Record<string, string> = JSON.parse(
const config: DenoConfig = JSON.parse(
await readTextFile(join(examplePath, "deno.json")),
);

Expand All @@ -28,6 +34,14 @@ export async function initExampleConfig(example: string) {
importMap.imports["ultra/"] = `../../`;
config.importMap = "importMap.dev.json";

// Since `deno task -c deno.dev.json dev` doesn't pass `deno.dev.json` to cli by default
if (config.tasks.dev && !config.tasks.dev.includes("deno.dev.json")) {
config.tasks.dev = config.tasks.dev.replace(
/^deno run/,
"deno run -c deno.dev.json",
);
}

await Deno.writeTextFile(devConfigPath, JSON.stringify(config, null, 2));
await Deno.writeTextFile(
devImportMapPath,
Expand Down
2 changes: 1 addition & 1 deletion tools/test-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { initExampleConfig } from "./dev.ts";
async function testExample(example: string) {
try {
const examplePath = join("examples", example);
await initExampleConfig(example);
const cmd = [
Deno.execPath(),
"test",
Expand All @@ -13,6 +12,7 @@ async function testExample(example: string) {
"-A",
];
console.log("test ", examplePath, cmd);
await initExampleConfig(example);
const process = Deno.run({
cmd,
cwd: examplePath,
Expand Down