feat: a new "island" based architechture (#97)
This commit is contained in:
+51
-34
@@ -1,6 +1,6 @@
|
||||
import { join, resolve } from "./deps.ts";
|
||||
import { error } from "./error.ts";
|
||||
import { routes } from "./routes.ts";
|
||||
import { manifest } from "./manifest.ts";
|
||||
|
||||
const help = `fresh init
|
||||
|
||||
@@ -57,30 +57,50 @@ async function init(directory: string) {
|
||||
}
|
||||
}
|
||||
|
||||
await Deno.mkdir(join(directory, "pages", "api"), { recursive: true });
|
||||
const DEPS_TS = `export * from "${new URL(
|
||||
await Deno.mkdir(join(directory, "routes", "api"), { recursive: true });
|
||||
await Deno.mkdir(join(directory, "islands"), { recursive: true });
|
||||
|
||||
const CLIENT_DEPS_TS = `export * from "${new URL(
|
||||
"../../runtime.ts",
|
||||
import.meta.url,
|
||||
)}";\n`;
|
||||
await Deno.writeTextFile(join(directory, "deps.ts"), DEPS_TS);
|
||||
const PAGES_INDEX_TSX = `/** @jsx h */
|
||||
import { h, IS_BROWSER, PageConfig, useState } from "../deps.ts";
|
||||
await Deno.writeTextFile(join(directory, "client_deps.ts"), CLIENT_DEPS_TS);
|
||||
const SERVER_DEPS_TS = `export * from "${new URL(
|
||||
"../../server.ts",
|
||||
import.meta.url,
|
||||
)}";\n`;
|
||||
await Deno.writeTextFile(join(directory, "server_deps.ts"), SERVER_DEPS_TS);
|
||||
|
||||
const ROUTES_INDEX_TSX = `/** @jsx h */
|
||||
import { h } from "../client_deps.ts";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
Welcome to \`fresh\`. Try update this message in the ./pages/index.tsx
|
||||
Welcome to \`fresh\`. Try update this message in the ./routes/index.tsx
|
||||
file, and refresh.
|
||||
</p>
|
||||
<Counter />
|
||||
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
|
||||
<Counter start={3} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
join(directory, "routes", "index.tsx"),
|
||||
ROUTES_INDEX_TSX,
|
||||
);
|
||||
|
||||
function Counter() {
|
||||
const [count, setCount] = useState(0);
|
||||
const ISLANDS_COUNTER_TSX = `/** @jsx h */
|
||||
import { h, IS_BROWSER, useState } from "../client_deps.ts";
|
||||
|
||||
interface CounterProps {
|
||||
start: number;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div>
|
||||
<p>{count}</p>
|
||||
@@ -93,31 +113,26 @@ function Counter() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export const config: PageConfig = { runtimeJS: true };
|
||||
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
join(directory, "pages", "index.tsx"),
|
||||
PAGES_INDEX_TSX,
|
||||
join(directory, "islands", "Counter.tsx"),
|
||||
ISLANDS_COUNTER_TSX,
|
||||
);
|
||||
const PAGES_GREET_TSX = `/** @jsx h */
|
||||
import { h } from "../deps.ts";
|
||||
|
||||
interface Props {
|
||||
params: Record<string, string | string[]>;
|
||||
}
|
||||
const ROUTES_GREET_TSX = `/** @jsx h */
|
||||
import { h, PageProps } from "../client_deps.ts";
|
||||
|
||||
export default function Greet(props: Props) {
|
||||
export default function Greet(props: PageProps) {
|
||||
return <div>Hello {props.params.name}</div>;
|
||||
}
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
join(directory, "pages", "[name].tsx"),
|
||||
PAGES_GREET_TSX,
|
||||
join(directory, "routes", "[name].tsx"),
|
||||
ROUTES_GREET_TSX,
|
||||
);
|
||||
const serverUrl = new URL("../../server.ts", import.meta.url);
|
||||
const PAGES_API_JOKE_TS = `import { HandlerContext } from "${serverUrl}";
|
||||
|
||||
const ROUTES_API_JOKE_TS =
|
||||
`import { HandlerContext } from "../server_deps.ts";
|
||||
|
||||
// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/
|
||||
const JOKES = [
|
||||
@@ -140,19 +155,20 @@ export const handler = (_ctx: HandlerContext): Response => {
|
||||
};
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
join(directory, "pages", "api", "joke.ts"),
|
||||
PAGES_API_JOKE_TS,
|
||||
join(directory, "routes", "api", "joke.ts"),
|
||||
ROUTES_API_JOKE_TS,
|
||||
);
|
||||
|
||||
const MAIN_TS = `/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
/// <reference lib="deno.unstable" />
|
||||
|
||||
import { start } from "${serverUrl}";
|
||||
import routes from "./routes.gen.ts";
|
||||
import { start } from "./server_deps.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
|
||||
await start(routes);
|
||||
await start(manifest);
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
join(directory, "main.ts"),
|
||||
@@ -168,10 +184,11 @@ Start the project:
|
||||
deno run -A --watch main.ts
|
||||
\`\`\`
|
||||
|
||||
After adding, removing, or moving a page in the \`pages\` directory, run:
|
||||
After adding, removing, or moving a page in the \`routes\` or directory, or adding,
|
||||
removing, or moving an island in the \`islands\` directory, run:
|
||||
|
||||
\`\`\`
|
||||
fresh routes
|
||||
fresh manifest
|
||||
\`\`\`
|
||||
`;
|
||||
await Deno.writeTextFile(
|
||||
@@ -179,5 +196,5 @@ fresh routes
|
||||
README_MD,
|
||||
);
|
||||
|
||||
await routes(directory);
|
||||
await manifest(directory);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { join, resolve, toFileUrl, walk } from "./deps.ts";
|
||||
|
||||
const help = `fresh manifest
|
||||
|
||||
Regenerate the fresh.gen.ts manifest for your fresh project.
|
||||
|
||||
To regenerate the mapping in the current directory:
|
||||
fresh manifest
|
||||
|
||||
To regenerate the mapping in the './foobar' subdirectory:
|
||||
fresh manifest ./foobar
|
||||
|
||||
USAGE:
|
||||
fresh manifest [OPTIONS] [DIRECTORY]
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Prints help information
|
||||
`;
|
||||
|
||||
export interface Args {
|
||||
help: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
||||
const args: Args = {
|
||||
help: !!rawArgs.help,
|
||||
};
|
||||
const directory: string | null = typeof rawArgs._[0] === "string"
|
||||
? rawArgs._[0]
|
||||
: Deno.cwd();
|
||||
if (args.help) {
|
||||
console.log(help);
|
||||
Deno.exit(0);
|
||||
}
|
||||
await manifest(directory);
|
||||
}
|
||||
|
||||
export async function manifest(directory: string) {
|
||||
directory = resolve(directory);
|
||||
const routes = [];
|
||||
try {
|
||||
const routesDir = join(directory, "./routes");
|
||||
const routesUrl = toFileUrl(routesDir);
|
||||
// TODO(lucacasonato): remove the extranious Deno.readDir when
|
||||
// https://github.com/denoland/deno_std/issues/1310 is fixed.
|
||||
for await (const _ of Deno.readDir(routesDir)) {
|
||||
// do nothing
|
||||
}
|
||||
const routesFolder = walk(routesDir, {
|
||||
includeDirs: false,
|
||||
includeFiles: true,
|
||||
exts: ["tsx", "jsx", "ts", "js"],
|
||||
});
|
||||
for await (const entry of routesFolder) {
|
||||
if (entry.isFile) {
|
||||
const file = toFileUrl(entry.path).href.substring(
|
||||
routesUrl.href.length,
|
||||
);
|
||||
routes.push(file);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof Deno.errors.NotFound) {
|
||||
// Do nothing.
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const islands = [];
|
||||
try {
|
||||
const islandsDir = join(directory, "./islands");
|
||||
const islandsUrl = toFileUrl(islandsDir);
|
||||
// TODO(lucacasonato): remove the extranious Deno.readDir when
|
||||
// https://github.com/denoland/deno_std/issues/1310 is fixed.
|
||||
for await (const _ of Deno.readDir(islandsDir)) {
|
||||
// do nothing
|
||||
}
|
||||
const islandsFolder = walk(islandsDir, {
|
||||
includeDirs: false,
|
||||
includeFiles: true,
|
||||
exts: ["tsx", "jsx", "ts", "js"],
|
||||
});
|
||||
for await (const entry of islandsFolder) {
|
||||
if (entry.isFile) {
|
||||
const file = toFileUrl(entry.path).href.substring(
|
||||
islandsUrl.href.length,
|
||||
);
|
||||
islands.push(file);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof Deno.errors.NotFound) {
|
||||
// Do nothing.
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const output = `// DO NOT EDIT. This file is generated by \`fresh\`.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// To update this file, run \`fresh routes\`.
|
||||
|
||||
${
|
||||
routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join(
|
||||
"\n",
|
||||
)
|
||||
}
|
||||
${
|
||||
islands.map((file, i) => `import * as $$${i} from "./islands${file}";`)
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
${
|
||||
routes.map((file, i) => `${JSON.stringify(`./routes${file}`)}: $${i},`)
|
||||
.join("\n ")
|
||||
}
|
||||
},
|
||||
islands: {
|
||||
${
|
||||
islands.map((file, i) => `${JSON.stringify(`./islands${file}`)}: $$${i},`)
|
||||
.join("\n ")
|
||||
}
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
`;
|
||||
|
||||
const manifestPath = join(directory, "./fresh.gen.ts");
|
||||
await Deno.writeTextFile(manifestPath, output);
|
||||
console.log("Manifest generated and written to", manifestPath);
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
import { join, resolve, toFileUrl, walk } from "./deps.ts";
|
||||
|
||||
const help = `fresh routes
|
||||
|
||||
Regenerate the route mapping for your fresh project.
|
||||
|
||||
To regenerate the mapping in the current directory:
|
||||
fresh routes
|
||||
|
||||
To regenerate the mapping in the './foobar' subdirectory:
|
||||
fresh routes ./foobar
|
||||
|
||||
USAGE:
|
||||
fresh routes [OPTIONS] [DIRECTORY]
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Prints help information
|
||||
`;
|
||||
|
||||
export interface Args {
|
||||
help: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export async function routesSubcommand(rawArgs: Record<string, any>) {
|
||||
const args: Args = {
|
||||
help: !!rawArgs.help,
|
||||
};
|
||||
const directory: string | null = typeof rawArgs._[0] === "string"
|
||||
? rawArgs._[0]
|
||||
: Deno.cwd();
|
||||
if (args.help) {
|
||||
console.log(help);
|
||||
Deno.exit(0);
|
||||
}
|
||||
await routes(directory);
|
||||
}
|
||||
|
||||
export async function routes(directory: string) {
|
||||
directory = resolve(directory);
|
||||
const files = [];
|
||||
const pagesDir = join(directory, "./pages");
|
||||
const pagesUrl = toFileUrl(pagesDir);
|
||||
const folder = walk(pagesDir, {
|
||||
includeDirs: false,
|
||||
includeFiles: true,
|
||||
exts: ["tsx", "jsx", "ts", "js"],
|
||||
});
|
||||
for await (const entry of folder) {
|
||||
if (entry.isFile) {
|
||||
const file = toFileUrl(entry.path).href.substring(pagesUrl.href.length);
|
||||
files.push(file);
|
||||
}
|
||||
}
|
||||
|
||||
const output = `// DO NOT EDIT. This file is generated by \`fresh\`.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// To update this file, run \`fresh routes\`.
|
||||
|
||||
${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")}
|
||||
|
||||
const routes = {
|
||||
pages: {
|
||||
${
|
||||
files.map((file, i) => `${JSON.stringify(`./pages${file}`)}: $${i},`)
|
||||
.join("\n ")
|
||||
}
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default routes;
|
||||
`;
|
||||
|
||||
const routeManifestPath = join(directory, "./routes.gen.ts");
|
||||
await Deno.writeTextFile(routeManifestPath, output);
|
||||
console.log("Route manifest generated and written to", routeManifestPath);
|
||||
}
|
||||
Reference in New Issue
Block a user