Skip to content

Commit

Permalink
feat: support root src path("@/":"/_ultra/compiler/src/")
Browse files Browse the repository at this point in the history
1. Support root src path like `import "@/config.ts"`
2. Add an example and test case about root src path in **examples/with-react/router**
  • Loading branch information
ahuigo committed Feb 7, 2023
1 parent 105a471 commit 34f05af
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 11 deletions.
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",
"test:examples": "deno run -A ./tools/test-examples.ts basic with-csr with-unocss 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
5 changes: 3 additions & 2 deletions examples/with-react-router/importMap.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"react-dom": "https://esm.sh/[email protected]",
"react-dom/server": "https://esm.sh/[email protected]/server?dev",
"react-dom/client": "https://esm.sh/[email protected]/client?dev",
"ultra/": "https://deno.land/x/[email protected]/",
"react-router-dom": "https://esm.sh/[email protected]?external=react",
"react-router-dom/server": "https://esm.sh/[email protected]/server?external=react"
"react-router-dom/server": "https://esm.sh/[email protected]/server?external=react",
"ultra/": "https://deno.land/x/[email protected]/",
"@/": "./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

0 comments on commit 34f05af

Please sign in to comment.