From 80598d07fd4eed735ea7ee3eb9728d7f5c9b3aa4 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 23 May 2022 19:35:15 +0200 Subject: [PATCH] feat: use import maps instead of deps files (#209) This commit updates fresh to use import maps instead of deps files in generated projects. --- .github/workflows/ci.yml | 12 +++-- .vscode/import_map.json | 8 ++++ .vscode/settings.json | 3 +- deno.jsonc | 5 +- docs/concepts/middleware.md | 2 +- docs/concepts/routes.md | 8 ++-- docs/getting-started/adding-interactivity.md | 4 +- docs/getting-started/create-a-project.md | 26 +++++------ docs/getting-started/create-a-route.md | 2 +- docs/getting-started/custom-handlers.md | 4 +- docs/getting-started/dynamic-routes.md | 2 +- docs/getting-started/fetching-data.md | 4 +- docs/getting-started/form-submissions.md | 4 +- examples/counter/deno.json | 3 +- examples/counter/deps.client.ts | 1 - examples/counter/deps.server.ts | 1 - examples/counter/import_map.json | 5 ++ examples/counter/islands/Counter.tsx | 2 +- examples/counter/main.ts | 2 +- examples/counter/routes/index.tsx | 2 +- src/cli/init.ts | 46 ++++++++++--------- src/runtime/deps.ts | 1 + src/server/bundle.ts | 6 ++- src/server/context.ts | 5 +- src/server/deps.ts | 2 +- tests/cli_test.ts | 5 +- tests/deps.ts | 6 +++ tests/fixture/deps.client.ts | 7 --- tests/fixture/deps.server.ts | 1 - tests/fixture/import_map.json | 5 ++ tests/fixture/islands/Test.tsx | 2 +- tests/fixture/main.ts | 8 +++- tests/fixture/routes/[name].tsx | 2 +- tests/fixture/routes/_404.tsx | 2 +- tests/fixture/routes/_500.tsx | 2 +- tests/fixture/routes/_app.tsx | 2 +- tests/fixture/routes/_middleware.ts | 2 +- tests/fixture/routes/assetsCaching/index.tsx | 2 +- tests/fixture/routes/books/[id].tsx | 2 +- tests/fixture/routes/index.tsx | 2 +- tests/fixture/routes/intercept.tsx | 2 +- tests/fixture/routes/intercept_args.tsx | 2 +- .../fixture/routes/layeredMdw/_middleware.ts | 2 +- .../routes/layeredMdw/layer2/_middleware.ts | 2 +- .../layeredMdw/layer2/layer3/_middleware.ts | 2 +- tests/fixture/routes/params.tsx | 2 +- tests/fixture/routes/props/[id].tsx | 2 +- tests/fixture/routes/static.tsx | 2 +- tests/fixture/routes/wildcard.tsx | 2 +- tests/fixture_error/deps.client.ts | 1 - tests/fixture_error/deps.server.ts | 1 - tests/fixture_error/import_map.json | 5 ++ tests/fixture_error/main.ts | 2 +- tests/fixture_error/routes/index.tsx | 2 +- www/components/Button.tsx | 3 +- www/components/DocsSidebar.tsx | 4 +- www/components/Footer.tsx | 3 +- www/components/Icons.tsx | 3 +- www/components/NavigationBar.tsx | 3 +- www/components/WarningBanner.tsx | 3 +- www/deno.json | 6 +++ www/import_map.json | 7 +++ www/islands/Counter.tsx | 3 +- www/islands/LemonDrop.tsx | 15 ++---- www/main.ts | 2 +- www/routes/_render.ts | 5 +- www/routes/docs/[...slug].tsx | 15 ++---- www/routes/gfm.css.ts | 3 +- www/routes/index.tsx | 4 +- www/{deps.server.ts => utils/markdown.ts} | 4 -- www/{deps.client.ts => utils/twind.ts} | 7 ++- 71 files changed, 188 insertions(+), 141 deletions(-) create mode 100644 .vscode/import_map.json delete mode 100644 examples/counter/deps.client.ts delete mode 100644 examples/counter/deps.server.ts create mode 100644 examples/counter/import_map.json delete mode 100644 tests/fixture/deps.client.ts delete mode 100644 tests/fixture/deps.server.ts create mode 100644 tests/fixture/import_map.json delete mode 100644 tests/fixture_error/deps.client.ts delete mode 100644 tests/fixture_error/deps.server.ts create mode 100644 tests/fixture_error/import_map.json create mode 100644 www/deno.json create mode 100644 www/import_map.json rename www/{deps.server.ts => utils/markdown.ts} (78%) rename www/{deps.client.ts => utils/twind.ts} (59%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42069129..f411625c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,13 +74,17 @@ jobs: run: deno check --remote cli.ts - name: Type check website - run: deno check --remote www/main.ts + run: deno check --remote main.ts + working-directory: www/ - name: Type check examples/counter - run: deno check --remote examples/counter/main.ts + run: deno check --remote main.ts + working-directory: examples/counter - name: Type check tests/fixture - run: deno check tests/fixture/main.ts + run: deno check main.ts + working-directory: tests/fixture/ - name: Type check tests/fixture_error - run: deno check tests/fixture_error/main.ts + run: deno check main.ts + working-directory: tests/fixture_error/ diff --git a/.vscode/import_map.json b/.vscode/import_map.json new file mode 100644 index 00000000..a4b4ed22 --- /dev/null +++ b/.vscode/import_map.json @@ -0,0 +1,8 @@ +{ + ".": "THIS FILE EXISTS ONLY FOR VSCODE! IT IS NOT USED AT RUNTIME.", + "imports": { + "$fresh/": "../", + "$twind": "https://esm.sh/twind@0.16.16", + "$twind/": "https://esm.sh/twind@0.16.16/" + } +} diff --git a/.vscode/settings.json b/.vscode/settings.json index e1533c2b..61e6398e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "deno.enable": true, - "deno.lint": true + "deno.lint": true, + "deno.importMap": "./.vscode/import_map.json" } diff --git a/deno.jsonc b/deno.jsonc index 3f12b288..046c60a4 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,7 +1,8 @@ { "tasks": { - "test": "deno test -A --no-check=remote && deno check www/main.ts && deno check cli.ts", + "test": "deno test -A --no-check=remote && deno check --config=www/deno.json www/main.ts && deno check cli.ts", "fixture": "deno run -A --watch ./tests/fixture/main.ts & deno run -A ./cli.ts manifest --watch ./tests/fixture", "www": "deno run -A --watch=www/static,docs/ ./www/main.ts & deno run -A ./cli.ts manifest --watch ./www" - } + }, + "importMap": "./tests/fixture/import_map.json" } diff --git a/docs/concepts/middleware.md b/docs/concepts/middleware.md index 196d64e3..3aa74ce5 100644 --- a/docs/concepts/middleware.md +++ b/docs/concepts/middleware.md @@ -15,7 +15,7 @@ used to pass arbituary data to downstream (or upstream) handlers. ```ts // routes/_middleware.ts -import { MiddlewareHandlerContext } from "../deps.server.ts"; +import { MiddlewareHandlerContext } from "$fresh/server.ts"; interface State { data: string; diff --git a/docs/concepts/routes.md b/docs/concepts/routes.md index fb54c0bf..addec3c4 100644 --- a/docs/concepts/routes.md +++ b/docs/concepts/routes.md @@ -22,7 +22,7 @@ Let's look at a basic route that returns a plain text string: ```tsx // routes/plain.tsx -import { HandlerContext, Handlers } from "../deps.server.ts"; +import { HandlerContext, Handlers } from "$fresh/server.ts"; export const handler: Handlers = { GET(ctx: HandlerContext) { @@ -43,7 +43,7 @@ Now, let's render some HTML using the route component. Another example: // routes/html.tsx /** @jsx h */ -import { h, PageProps } from "../deps.client.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; export default function Page(props: PageProps) { return
You are on the page '{props.url.href}'.
; @@ -65,8 +65,8 @@ response after rendering the page component. // routes/html.tsx /** @jsx h */ -import { h, PageProps } from "../deps.client.ts"; -import { HandlerContext, Handlers } from "../deps.server.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; +import { HandlerContext, Handlers } from "$fresh/server.ts"; export const handler: Handlers = { GET(ctx: HandlerContext) { diff --git a/docs/getting-started/adding-interactivity.md b/docs/getting-started/adding-interactivity.md index ac07edbc..3c6b5bba 100644 --- a/docs/getting-started/adding-interactivity.md +++ b/docs/getting-started/adding-interactivity.md @@ -31,7 +31,7 @@ Here is an example of an island component that counts down to a specific time. // islands/Countdown.tsx /** @jsx h */ -import { h, useEffect, useMemo, useState } from "../deps.client.ts"; +import { h, useEffect, useMemo, useState } from "$fresh/runtime.ts"; const timeFmt = new Intl.RelativeTimeFormat("en-US"); @@ -73,7 +73,7 @@ client with the correct props: // routes/countdown.tsx /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; import { Countdown } from "../islands/Countdown.tsx"; export default function Page() { diff --git a/docs/getting-started/create-a-project.md b/docs/getting-started/create-a-project.md index e0a401e3..b438e046 100644 --- a/docs/getting-started/create-a-project.md +++ b/docs/getting-started/create-a-project.md @@ -14,8 +14,8 @@ $ fresh init my-project Manifest generated and written to ./my-project/fresh.gen.ts ``` -This will create a directory containing a bunch of files. Out of all of the -files, only two are strictly necessary for a fresh project: +This will create a directory containing some files and directories. There are 3 +files that are strictly necessary to run a fresh project: - **`main.ts`**: This is the entrypoint for your project. It is the file that will you run to start your fresh application. This file doesn't actually need @@ -25,19 +25,17 @@ files, only two are strictly necessary for a fresh project: your routes and islands. This file is automatically generated based on your `routes/` and `islands/` folder by the `fresh` CLI when running the `fresh manifest` command. +- **`import_map.json`**: This is an [import map][import-map] that is used to + manage dependencies for the project. This allows for easy importing and + updating of dependencies. -In addition to these two files, two other files are created that manage -dependencies: +A **`deno.json`** file is also created in the project directory. This file does +two things: -- **`deps.client.ts`**: This file contains dependencies that you need on the - client, and possibly also on the server. It should be assumed that all of the - dependencies referenced by this file will be shipped to the client. By - default, it just re-exports the `fresh` runtime. -- **`deps.server.ts`**: This file contains dependencies that are only used on - the server. These are often things like database drivers, API clients, or - other code that should only be executed on the server. This file should never - be referenced by modules that might be shipped to the client. By default this - just re-exports the `fresh` server libraries. +- It tells Deno about the location of the import map, so that it can be loaded + automatically. +- It registers a "start" [task][task-runner] to run the project without having + to type a long `deno run` command. Two important folders are also created that contain your routes and islands respectively: @@ -54,4 +52,6 @@ respectively: Finally a **`static/`** folder is created that contains static files that are automatically served "as is". [Learn more about static files][static-files]. +[import-map]: https://deno.land/manual/linking_to_external_code/import_maps +[task-runner]: https://deno.land/manual/tools/task_runner [static-files]: ../concepts/static-files diff --git a/docs/getting-started/create-a-route.md b/docs/getting-started/create-a-route.md index 809c09f6..d0db8e47 100644 --- a/docs/getting-started/create-a-route.md +++ b/docs/getting-started/create-a-route.md @@ -38,7 +38,7 @@ This is done with JSX. // routes/about.tsx /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; export default function AboutPage() { return ( diff --git a/docs/getting-started/custom-handlers.md b/docs/getting-started/custom-handlers.md index fc946bb7..86ba81a4 100644 --- a/docs/getting-started/custom-handlers.md +++ b/docs/getting-started/custom-handlers.md @@ -31,7 +31,7 @@ then adds a custom header to the response before returning it: // routes/about.tsx /** @jsx h */ -import { h, Handlers } from "../deps.client.ts"; +import { h, Handlers } from "$fresh/runtime.ts"; export const handler: Handlers = { GET(req, ctx) { @@ -58,7 +58,7 @@ response: ```ts // routes/api/random-uuid.ts -import { Handlers } from "../deps.client.ts"; +import { Handlers } from "$fresh/runtime.ts"; export const handler: Handlers = { GET(req) { diff --git a/docs/getting-started/dynamic-routes.md b/docs/getting-started/dynamic-routes.md index 97293021..d93391dc 100644 --- a/docs/getting-started/dynamic-routes.md +++ b/docs/getting-started/dynamic-routes.md @@ -28,7 +28,7 @@ as arguments in it's `props` object though. // routes/greet/[name].tsx /** @jsx h */ -import { h, PageProps } from "../../deps.client.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; export default function AboutPage(props: PageProps) { const { name } = props.params; diff --git a/docs/getting-started/fetching-data.md b/docs/getting-started/fetching-data.md index cda87c7e..71d0cc82 100644 --- a/docs/getting-started/fetching-data.md +++ b/docs/getting-started/fetching-data.md @@ -24,8 +24,8 @@ renders it in a page component. // routes/github/[username].tsx /** @jsx h */ -import { h, PageProps } from "../../deps.client.ts"; -import { Handlers } from "../../deps.server.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; +import { Handlers } from "$fresh/server.ts"; interface User { login: string; diff --git a/docs/getting-started/form-submissions.md b/docs/getting-started/form-submissions.md index 7b05b94b..9deec3da 100644 --- a/docs/getting-started/form-submissions.md +++ b/docs/getting-started/form-submissions.md @@ -33,8 +33,8 @@ server side: // routes/search.tsx /** @jsx h */ -import { h, PageProps } from "../../deps.client.ts"; -import { Handlers } from "../../deps.server.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; +import { Handlers } from "$fresh/server.ts"; const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"]; diff --git a/examples/counter/deno.json b/examples/counter/deno.json index abc9a70f..88b96c5b 100644 --- a/examples/counter/deno.json +++ b/examples/counter/deno.json @@ -1,5 +1,6 @@ { "tasks": { "start": "deno run -A --watch main.ts & deno run -A ../../cli.ts manifest --watch" - } + }, + "importMap": "./import_map.json" } diff --git a/examples/counter/deps.client.ts b/examples/counter/deps.client.ts deleted file mode 100644 index 3ccecce5..00000000 --- a/examples/counter/deps.client.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../../runtime.ts"; diff --git a/examples/counter/deps.server.ts b/examples/counter/deps.server.ts deleted file mode 100644 index 0318a0be..00000000 --- a/examples/counter/deps.server.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../../server.ts"; diff --git a/examples/counter/import_map.json b/examples/counter/import_map.json new file mode 100644 index 00000000..7bdcd08d --- /dev/null +++ b/examples/counter/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "$fresh/": "../../" + } +} diff --git a/examples/counter/islands/Counter.tsx b/examples/counter/islands/Counter.tsx index 41f19e26..ce4e644e 100644 --- a/examples/counter/islands/Counter.tsx +++ b/examples/counter/islands/Counter.tsx @@ -1,5 +1,5 @@ /** @jsx h */ -import { h, IS_BROWSER, useState } from "../deps.client.ts"; +import { h, IS_BROWSER, useState } from "$fresh/runtime.ts"; interface CounterProps { start: number; diff --git a/examples/counter/main.ts b/examples/counter/main.ts index 7652910e..0f15e8de 100644 --- a/examples/counter/main.ts +++ b/examples/counter/main.ts @@ -4,7 +4,7 @@ /// /// -import { start } from "./deps.server.ts"; +import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; await start(manifest); diff --git a/examples/counter/routes/index.tsx b/examples/counter/routes/index.tsx index 43a8cd2b..37050b81 100644 --- a/examples/counter/routes/index.tsx +++ b/examples/counter/routes/index.tsx @@ -1,5 +1,5 @@ /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; import Counter from "../islands/Counter.tsx"; export default function Home() { diff --git a/src/cli/init.ts b/src/cli/init.ts index e00c8dc5..633a4e0f 100644 --- a/src/cli/init.ts +++ b/src/cli/init.ts @@ -66,19 +66,19 @@ async function init(directory: string) { await Deno.mkdir(join(directory, "islands"), { recursive: true }); await Deno.mkdir(join(directory, "static"), { recursive: true }); - const DEPS_CLIENT_TS = `export * from "${new URL( - "../../runtime.ts", - import.meta.url, - )}";\n`; - await Deno.writeTextFile(join(directory, "deps.client.ts"), DEPS_CLIENT_TS); - const DEPS_SERVER_TS = `export * from "${new URL( - "../../server.ts", - import.meta.url, - )}";\n`; - await Deno.writeTextFile(join(directory, "deps.server.ts"), DEPS_SERVER_TS); + const IMPORT_MAP_JSON = JSON.stringify( + { + "imports": { + "$fresh/": new URL("../../", import.meta.url).href, + }, + }, + null, + 2, + ); + await Deno.writeTextFile(join(directory, "import_map.json"), IMPORT_MAP_JSON); const ROUTES_INDEX_TSX = `/** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; import Counter from "../islands/Counter.tsx"; export default function Home() { @@ -100,7 +100,7 @@ export default function Home() { ); const ISLANDS_COUNTER_TSX = `/** @jsx h */ -import { h, IS_BROWSER, useState } from "../deps.client.ts"; +import { h, IS_BROWSER, useState } from "$fresh/runtime.ts"; interface CounterProps { start: number; @@ -127,7 +127,7 @@ export default function Counter(props: CounterProps) { ); const ROUTES_GREET_TSX = `/** @jsx h */ -import { h, PageProps } from "../deps.client.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; export default function Greet(props: PageProps) { return
Hello {props.params.name}
; @@ -138,8 +138,7 @@ export default function Greet(props: PageProps) { ROUTES_GREET_TSX, ); - const ROUTES_API_JOKE_TS = - `import { HandlerContext } from "../../deps.server.ts"; + const ROUTES_API_JOKE_TS = `import { HandlerContext } from "$fresh/server.ts"; // Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ const JOKES = [ @@ -188,7 +187,7 @@ export const handler = (_req: Request, _ctx: HandlerContext): Response => { /// /// -import { start } from "./deps.server.ts"; +import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; await start(manifest); @@ -201,11 +200,16 @@ await start(manifest); // this throws on windows } - const DENO_CONFIG = `{ - "tasks": { - "start": "deno run -A --watch --no-check main.ts" - } -}`; + const DENO_CONFIG = JSON.stringify( + { + tasks: { + start: "deno run -A --watch --no-check main.ts", + }, + importMap: "./import_map.json", + }, + null, + 2, + ); await Deno.writeTextFile(join(directory, "deno.json"), DENO_CONFIG); diff --git a/src/runtime/deps.ts b/src/runtime/deps.ts index 81972964..a7bad34e 100644 --- a/src/runtime/deps.ts +++ b/src/runtime/deps.ts @@ -1,4 +1,5 @@ export * from "https://esm.sh/preact@10.6.6"; +export { jsx, jsxDEV, jsxs } from "https://esm.sh/preact@10.6.6/jsx-runtime"; export { useCallback, useContext, diff --git a/src/server/bundle.ts b/src/server/bundle.ts index 1353a563..3d45c5ae 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -21,11 +21,13 @@ async function ensureEsbuildInialized() { } export class Bundler { + #importMapURL: URL; #islands: Island[]; #cache: Map | Promise | undefined = undefined; - constructor(islands: Island[]) { + constructor(islands: Island[], importMapURL: URL) { this.#islands = islands; + this.#importMapURL = importMapURL; } async bundle() { @@ -52,7 +54,7 @@ export class Bundler { absWorkingDir, outfile: "", platform: "neutral", - plugins: [denoPlugin()], + plugins: [denoPlugin({ importMapURL: this.#importMapURL })], splitting: true, target: ["chrome96", "firefox95", "safari14"], treeShaking: true, diff --git a/src/server/context.ts b/src/server/context.ts index 965b9e91..ed57c35f 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -69,6 +69,7 @@ export class ServerContext { app: AppModule, notFound: UnknownPage, error: ErrorPage, + importMapURL: URL, ) { this.#pages = pages; this.#islands = islands; @@ -78,7 +79,7 @@ export class ServerContext { this.#app = app; this.#notFound = notFound; this.#error = error; - this.#bundler = new Bundler(this.#islands); + this.#bundler = new Bundler(this.#islands, importMapURL); this.#dev = typeof Deno.env.get("DENO_DEPLOYMENT_ID") !== "string"; // Env var is only set in prod (on Deploy). } @@ -88,6 +89,7 @@ export class ServerContext { static async fromManifest(manifest: Manifest): Promise { // Get the manifest' base URL. const baseUrl = new URL("./", manifest.baseUrl).href; + const importMapURL = new URL("./import_map.json", manifest.baseUrl); // Extract all routes, and prepare them into the `Page` structure. const pages: Page[] = []; @@ -254,6 +256,7 @@ export class ServerContext { app, notFound, error, + importMapURL, ); } diff --git a/src/server/deps.ts b/src/server/deps.ts index 288af666..0e6c0135 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -30,4 +30,4 @@ const esbuild: typeof esbuildWasm = Deno.run === undefined ? esbuildWasm : esbuildNative; export { esbuild, esbuildWasm as esbuildTypes }; -export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.4.3/mod.ts"; +export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.5.0/mod.ts"; diff --git a/tests/cli_test.ts b/tests/cli_test.ts index c2bb7ded..ce2681c4 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -61,7 +61,7 @@ Deno.test({ "name": tmpDirName, "contents": [ { "type": "file", "name": "README.md" }, - { "type": "file", "name": "deps.client.ts" }, + { "type": "file", "name": "import_map.json" }, { "type": "file", "name": "fresh.gen.ts" }, { "type": "directory", @@ -86,7 +86,6 @@ Deno.test({ { "type": "file", "name": "index.tsx" }, ], }, - { "type": "file", "name": "deps.server.ts" }, { "type": "directory", "name": "static", @@ -96,7 +95,7 @@ Deno.test({ }, ], }, - { "type": "report", "directories": 3, "files": 9 }, + { "type": "report", "directories": 3, "files": 8 }, ]; await t.step("check generated files", async () => { await assertFileExistence(targetFileTree); diff --git a/tests/deps.ts b/tests/deps.ts index c5656331..cf62d537 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -1,3 +1,9 @@ +/// +/// +/// +/// +/// + export { assert, assertEquals, diff --git a/tests/fixture/deps.client.ts b/tests/fixture/deps.client.ts deleted file mode 100644 index e6f2e642..00000000 --- a/tests/fixture/deps.client.ts +++ /dev/null @@ -1,7 +0,0 @@ -/// -/// -/// -/// -/// - -export * from "../../runtime.ts"; diff --git a/tests/fixture/deps.server.ts b/tests/fixture/deps.server.ts deleted file mode 100644 index 0318a0be..00000000 --- a/tests/fixture/deps.server.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../../server.ts"; diff --git a/tests/fixture/import_map.json b/tests/fixture/import_map.json new file mode 100644 index 00000000..7bdcd08d --- /dev/null +++ b/tests/fixture/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "$fresh/": "../../" + } +} diff --git a/tests/fixture/islands/Test.tsx b/tests/fixture/islands/Test.tsx index 7151be6b..a3045518 100644 --- a/tests/fixture/islands/Test.tsx +++ b/tests/fixture/islands/Test.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; export default function Test(props: { message: string }) { return ( diff --git a/tests/fixture/main.ts b/tests/fixture/main.ts index cb5a0320..31dfab87 100644 --- a/tests/fixture/main.ts +++ b/tests/fixture/main.ts @@ -1,4 +1,10 @@ -import { start } from "./deps.server.ts"; +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; import routes from "./fresh.gen.ts"; await start(routes); diff --git a/tests/fixture/routes/[name].tsx b/tests/fixture/routes/[name].tsx index 3b5c430e..9a0573cb 100644 --- a/tests/fixture/routes/[name].tsx +++ b/tests/fixture/routes/[name].tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; interface Props { params: Record; diff --git a/tests/fixture/routes/_404.tsx b/tests/fixture/routes/_404.tsx index b953c529..5be390c6 100644 --- a/tests/fixture/routes/_404.tsx +++ b/tests/fixture/routes/_404.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, UnknownPageProps } from "../deps.client.ts"; +import { h, UnknownPageProps } from "$fresh/runtime.ts"; export default function NotFoundPage({ url }: UnknownPageProps) { return

404 not found: {url.pathname}

; diff --git a/tests/fixture/routes/_500.tsx b/tests/fixture/routes/_500.tsx index 72960cd1..0f4e4b61 100644 --- a/tests/fixture/routes/_500.tsx +++ b/tests/fixture/routes/_500.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { ErrorPageProps, h } from "../deps.client.ts"; +import { ErrorPageProps, h } from "$fresh/runtime.ts"; export default function Error500Page({ error }: ErrorPageProps) { return

500 internal error: {(error as Error).message}

; diff --git a/tests/fixture/routes/_app.tsx b/tests/fixture/routes/_app.tsx index 6d2a7789..72c78a99 100644 --- a/tests/fixture/routes/_app.tsx +++ b/tests/fixture/routes/_app.tsx @@ -1,7 +1,7 @@ /** @jsx h */ /** @jsxFrag Fragment */ -import { AppProps, Fragment, h, Head } from "../deps.client.ts"; +import { AppProps, Fragment, h, Head } from "$fresh/runtime.ts"; export default function App(props: AppProps) { return ( diff --git a/tests/fixture/routes/_middleware.ts b/tests/fixture/routes/_middleware.ts index 19a34c2d..2ccb360a 100644 --- a/tests/fixture/routes/_middleware.ts +++ b/tests/fixture/routes/_middleware.ts @@ -1,4 +1,4 @@ -import { MiddlewareHandlerContext } from "../deps.server.ts"; +import { MiddlewareHandlerContext } from "$fresh/server.ts"; export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { ctx.state.root = "root_mw"; diff --git a/tests/fixture/routes/assetsCaching/index.tsx b/tests/fixture/routes/assetsCaching/index.tsx index b5c314c9..06691095 100644 --- a/tests/fixture/routes/assetsCaching/index.tsx +++ b/tests/fixture/routes/assetsCaching/index.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { asset, h } from "../../deps.client.ts"; +import { asset, h } from "$fresh/runtime.ts"; import Test from "../../islands/Test.tsx"; export default function Home() { diff --git a/tests/fixture/routes/books/[id].tsx b/tests/fixture/routes/books/[id].tsx index 7ca12de1..21e59ce2 100644 --- a/tests/fixture/routes/books/[id].tsx +++ b/tests/fixture/routes/books/[id].tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, PageProps, RouteConfig } from "../../deps.client.ts"; +import { h, PageProps, RouteConfig } from "$fresh/runtime.ts"; export default function Page(props: PageProps) { return
Book {props.params.id}
; diff --git a/tests/fixture/routes/index.tsx b/tests/fixture/routes/index.tsx index dd5efc46..60710835 100644 --- a/tests/fixture/routes/index.tsx +++ b/tests/fixture/routes/index.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, IS_BROWSER } from "../deps.client.ts"; +import { h, IS_BROWSER } from "$fresh/runtime.ts"; import Test from "../islands/Test.tsx"; export default function Home() { diff --git a/tests/fixture/routes/intercept.tsx b/tests/fixture/routes/intercept.tsx index a42396b8..8b862f35 100644 --- a/tests/fixture/routes/intercept.tsx +++ b/tests/fixture/routes/intercept.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; import { HandlerContext } from "../../../server.ts"; export default function Page() { diff --git a/tests/fixture/routes/intercept_args.tsx b/tests/fixture/routes/intercept_args.tsx index d69b6ffd..2eec8a72 100644 --- a/tests/fixture/routes/intercept_args.tsx +++ b/tests/fixture/routes/intercept_args.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, PageProps } from "../deps.client.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; import { HandlerContext, Handlers } from "../../../server.ts"; interface Data extends Record { diff --git a/tests/fixture/routes/layeredMdw/_middleware.ts b/tests/fixture/routes/layeredMdw/_middleware.ts index 867bbe43..709093e3 100644 --- a/tests/fixture/routes/layeredMdw/_middleware.ts +++ b/tests/fixture/routes/layeredMdw/_middleware.ts @@ -1,4 +1,4 @@ -import { MiddlewareHandlerContext } from "../../deps.server.ts"; +import { MiddlewareHandlerContext } from "$fresh/server.ts"; export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { ctx.state.layer1 = "layer1_mw"; diff --git a/tests/fixture/routes/layeredMdw/layer2/_middleware.ts b/tests/fixture/routes/layeredMdw/layer2/_middleware.ts index af2ddc62..15af4050 100644 --- a/tests/fixture/routes/layeredMdw/layer2/_middleware.ts +++ b/tests/fixture/routes/layeredMdw/layer2/_middleware.ts @@ -1,4 +1,4 @@ -import { MiddlewareHandlerContext } from "../../../deps.server.ts"; +import { MiddlewareHandlerContext } from "$fresh/server.ts"; interface State { root: string; diff --git a/tests/fixture/routes/layeredMdw/layer2/layer3/_middleware.ts b/tests/fixture/routes/layeredMdw/layer2/layer3/_middleware.ts index a63bb20c..82e58fd3 100644 --- a/tests/fixture/routes/layeredMdw/layer2/layer3/_middleware.ts +++ b/tests/fixture/routes/layeredMdw/layer2/layer3/_middleware.ts @@ -1,4 +1,4 @@ -import { MiddlewareHandlerContext } from "../../../../deps.server.ts"; +import { MiddlewareHandlerContext } from "$fresh/server.ts"; export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { ctx.state.layer3 = "layer3_mw"; diff --git a/tests/fixture/routes/params.tsx b/tests/fixture/routes/params.tsx index 0cc12442..30c6fc29 100644 --- a/tests/fixture/routes/params.tsx +++ b/tests/fixture/routes/params.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, RouteConfig } from "../deps.client.ts"; +import { h, RouteConfig } from "$fresh/runtime.ts"; import { HandlerContext } from "../../../server.ts"; export const handler = { diff --git a/tests/fixture/routes/props/[id].tsx b/tests/fixture/routes/props/[id].tsx index a7798824..c18a557d 100644 --- a/tests/fixture/routes/props/[id].tsx +++ b/tests/fixture/routes/props/[id].tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, PageProps } from "../../deps.client.ts"; +import { h, PageProps } from "$fresh/runtime.ts"; export default function Home(props: PageProps) { return
{JSON.stringify(props)}
; diff --git a/tests/fixture/routes/static.tsx b/tests/fixture/routes/static.tsx index 9b3859d2..4c15e2a0 100644 --- a/tests/fixture/routes/static.tsx +++ b/tests/fixture/routes/static.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { asset, h } from "../deps.client.ts"; +import { asset, h } from "$fresh/runtime.ts"; export default function StaticPage() { return ( diff --git a/tests/fixture/routes/wildcard.tsx b/tests/fixture/routes/wildcard.tsx index c81720d0..256e4cbb 100644 --- a/tests/fixture/routes/wildcard.tsx +++ b/tests/fixture/routes/wildcard.tsx @@ -1,6 +1,6 @@ /** @jsx h */ -import { h, PageProps, RouteConfig } from "../deps.client.ts"; +import { h, PageProps, RouteConfig } from "$fresh/runtime.ts"; export default function WildcardPage({ params }: PageProps) { if (typeof params.path === "string") { diff --git a/tests/fixture_error/deps.client.ts b/tests/fixture_error/deps.client.ts deleted file mode 100644 index 3ccecce5..00000000 --- a/tests/fixture_error/deps.client.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../../runtime.ts"; diff --git a/tests/fixture_error/deps.server.ts b/tests/fixture_error/deps.server.ts deleted file mode 100644 index 0318a0be..00000000 --- a/tests/fixture_error/deps.server.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../../server.ts"; diff --git a/tests/fixture_error/import_map.json b/tests/fixture_error/import_map.json new file mode 100644 index 00000000..7bdcd08d --- /dev/null +++ b/tests/fixture_error/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "$fresh/": "../../" + } +} diff --git a/tests/fixture_error/main.ts b/tests/fixture_error/main.ts index 7652910e..0f15e8de 100644 --- a/tests/fixture_error/main.ts +++ b/tests/fixture_error/main.ts @@ -4,7 +4,7 @@ /// /// -import { start } from "./deps.server.ts"; +import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; await start(manifest); diff --git a/tests/fixture_error/routes/index.tsx b/tests/fixture_error/routes/index.tsx index bac1343a..5979ffcc 100644 --- a/tests/fixture_error/routes/index.tsx +++ b/tests/fixture_error/routes/index.tsx @@ -1,5 +1,5 @@ /** @jsx h */ -import { h } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; export default function Home(): null { throw new Error("boom!"); diff --git a/www/components/Button.tsx b/www/components/Button.tsx index f23e2fd6..dc2a2ae1 100644 --- a/www/components/Button.tsx +++ b/www/components/Button.tsx @@ -1,6 +1,7 @@ /** @jsx h */ -import { h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; export function RoundedButton(props: h.JSX.HTMLAttributes) { return ( diff --git a/www/components/DocsSidebar.tsx b/www/components/DocsSidebar.tsx index 5ae19ead..1bb876d2 100644 --- a/www/components/DocsSidebar.tsx +++ b/www/components/DocsSidebar.tsx @@ -1,6 +1,8 @@ /** @jsx h */ -import { apply, h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { apply, tw } from "../utils/twind.ts"; + import { CATEGORIES, TableOfContentsCategory, diff --git a/www/components/Footer.tsx b/www/components/Footer.tsx index a4b36e68..1b4c2630 100644 --- a/www/components/Footer.tsx +++ b/www/components/Footer.tsx @@ -1,6 +1,7 @@ /** @jsx h */ -import { h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; const LINKS = [ { diff --git a/www/components/Icons.tsx b/www/components/Icons.tsx index 03e34e40..dd88c91b 100644 --- a/www/components/Icons.tsx +++ b/www/components/Icons.tsx @@ -1,6 +1,7 @@ /** @jsx h */ -import { h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; export function IconMinus() { return ( diff --git a/www/components/NavigationBar.tsx b/www/components/NavigationBar.tsx index 1ac93679..fc65ee76 100644 --- a/www/components/NavigationBar.tsx +++ b/www/components/NavigationBar.tsx @@ -1,6 +1,7 @@ /** @jsx h */ -import { h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; export default function NavigationBar(props: { active: string }) { const items = [ diff --git a/www/components/WarningBanner.tsx b/www/components/WarningBanner.tsx index 636acd5e..12de6b96 100644 --- a/www/components/WarningBanner.tsx +++ b/www/components/WarningBanner.tsx @@ -1,6 +1,7 @@ /** @jsx h */ -import { h, tw } from "../deps.client.ts"; +import { h } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; export default function WarningBanner() { const warning = tw`bg-yellow-100 border(1 yellow-200) p-4 mb-4`; diff --git a/www/deno.json b/www/deno.json new file mode 100644 index 00000000..c07ad937 --- /dev/null +++ b/www/deno.json @@ -0,0 +1,6 @@ +{ + "tasks": { + "start": "deno run -A --watch main.ts & deno run -A ../cli.ts manifest --watch" + }, + "importMap": "./import_map.json" +} diff --git a/www/import_map.json b/www/import_map.json new file mode 100644 index 00000000..237a3514 --- /dev/null +++ b/www/import_map.json @@ -0,0 +1,7 @@ +{ + "imports": { + "$fresh/": "../", + "$twind": "https://esm.sh/twind@0.16.16", + "$twind/": "https://esm.sh/twind@0.16.16/" + } +} diff --git a/www/islands/Counter.tsx b/www/islands/Counter.tsx index f02f59d2..f7dc878c 100644 --- a/www/islands/Counter.tsx +++ b/www/islands/Counter.tsx @@ -1,5 +1,6 @@ /** @jsx h */ -import { h, IS_BROWSER, tw, useState } from "../deps.client.ts"; +import { h, IS_BROWSER, useState } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; import { RoundedButton } from "../components/Button.tsx"; import { IconMinus, IconPlus } from "../components/Icons.tsx"; diff --git a/www/islands/LemonDrop.tsx b/www/islands/LemonDrop.tsx index 326f2087..52609fc1 100644 --- a/www/islands/LemonDrop.tsx +++ b/www/islands/LemonDrop.tsx @@ -1,12 +1,7 @@ /** @jsx h */ -import { - Fragment, - h, - tw, - useEffect, - useRef, - useState, -} from "../deps.client.ts"; +/** @jsxFrag Fragment */ +import { Fragment, h, useEffect, useRef, useState } from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; import { Spring, WaveTank } from "../components/WaveTank.ts"; function easeInCirc(x: number) { @@ -99,7 +94,7 @@ function LemonDrop() { }, []); return ( - + <> - + ); } diff --git a/www/main.ts b/www/main.ts index 3f6e7a59..31dfab87 100644 --- a/www/main.ts +++ b/www/main.ts @@ -4,7 +4,7 @@ /// /// -import { start } from "./deps.server.ts"; +import { start } from "$fresh/server.ts"; import routes from "./fresh.gen.ts"; await start(routes); diff --git a/www/routes/_render.ts b/www/routes/_render.ts index 59d9a846..02bec8c3 100644 --- a/www/routes/_render.ts +++ b/www/routes/_render.ts @@ -1,7 +1,8 @@ // This module adds twind support. -import { setup, theme } from "../deps.client.ts"; -import { RenderContext, RenderFn, virtualSheet } from "../deps.server.ts"; +import { RenderContext, RenderFn } from "$fresh/server.ts"; +import { setup, theme } from "../utils/twind.ts"; +import { virtualSheet } from "$twind/sheets"; const sheet = virtualSheet(); sheet.reset(); diff --git a/www/routes/docs/[...slug].tsx b/www/routes/docs/[...slug].tsx index 56296ef7..a8c659be 100644 --- a/www/routes/docs/[...slug].tsx +++ b/www/routes/docs/[...slug].tsx @@ -1,16 +1,11 @@ /** @jsx h */ /** @jsxFrag Fragment */ -import { - apply, - asset, - Fragment, - h, - Head, - PageProps, - tw, -} from "../../deps.client.ts"; -import { frontMatter, gfm, Handlers } from "../../deps.server.ts"; +import { Fragment, h, Head, PageProps } from "$fresh/runtime.ts"; +import { Handlers } from "$fresh/server.ts"; +import { apply, tw } from "../../utils/twind.ts"; +import { frontMatter, gfm } from "../../utils/markdown.ts"; + import DocsSidebar from "../../components/DocsSidebar.tsx"; import Footer from "../../components/Footer.tsx"; import NavigationBar from "../../components/NavigationBar.tsx"; diff --git a/www/routes/gfm.css.ts b/www/routes/gfm.css.ts index 147757cd..24818465 100644 --- a/www/routes/gfm.css.ts +++ b/www/routes/gfm.css.ts @@ -1,4 +1,5 @@ -import { gfm, Handlers } from "../deps.server.ts"; +import { Handlers } from "$fresh/server.ts"; +import { gfm } from "../utils/markdown.ts"; // TODO(lucacasonato): hash the file and use the hash as the filename, and serve // with high-cacheability headers. diff --git a/www/routes/index.tsx b/www/routes/index.tsx index c64e2ebe..3cbd0943 100644 --- a/www/routes/index.tsx +++ b/www/routes/index.tsx @@ -8,8 +8,8 @@ import { h, Head, PageProps, - tw, -} from "../deps.client.ts"; +} from "$fresh/runtime.ts"; +import { tw } from "../utils/twind.ts"; import Counter from "../islands/Counter.tsx"; import LemonDrop from "../islands/LemonDrop.tsx"; import Footer from "../components/Footer.tsx"; diff --git a/www/deps.server.ts b/www/utils/markdown.ts similarity index 78% rename from www/deps.server.ts rename to www/utils/markdown.ts index b5fb5019..769d9e43 100644 --- a/www/deps.server.ts +++ b/www/utils/markdown.ts @@ -1,7 +1,3 @@ -export * from "../server.ts"; - -export { virtualSheet } from "https://esm.sh/twind@0.16.16/sheets"; - export * as gfm from "https://deno.land/x/gfm@0.1.19/mod.ts"; import "https://esm.sh/prismjs@1.27.0/components/prism-jsx.js?no-check"; import "https://esm.sh/prismjs@1.27.0/components/prism-typescript.js?no-check"; diff --git a/www/deps.client.ts b/www/utils/twind.ts similarity index 59% rename from www/deps.client.ts rename to www/utils/twind.ts index f55b3620..1e40b3a4 100644 --- a/www/deps.client.ts +++ b/www/utils/twind.ts @@ -1,7 +1,6 @@ -export * from "../runtime.ts"; -import { IS_BROWSER } from "../runtime.ts"; -import { apply, setup, tw } from "https://esm.sh/twind@0.16.16"; -import * as colors from "https://esm.sh/twind@0.16.16/colors"; +import { IS_BROWSER } from "$fresh/runtime.ts"; +import { apply, setup, tw } from "$twind"; +import * as colors from "$twind/colors"; export { apply, setup, tw }; export const theme = { colors: {