diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f411625c..3d8ce41b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: run: deno lint - name: Cache dependencies - run: deno cache --no-check src/cli/deps.ts src/runtime/deps.ts src/server/deps.ts + run: deno cache --no-check src/dev/deps.ts src/runtime/deps.ts src/server/deps.ts - name: Cache Chrome (Linux) if: startsWith(matrix.os, 'ubuntu') @@ -70,21 +70,21 @@ jobs: - name: Run tests run: deno test -A - - name: Type check CLI - run: deno check --remote cli.ts + - name: Type check init script + run: deno check --remote init.ts - name: Type check website - run: deno check --remote main.ts + run: deno check --remote main.ts dev.ts working-directory: www/ - name: Type check examples/counter - run: deno check --remote main.ts + run: deno check --remote main.ts dev.ts working-directory: examples/counter - name: Type check tests/fixture - run: deno check main.ts + run: deno check main.ts dev.ts working-directory: tests/fixture/ - name: Type check tests/fixture_error - run: deno check main.ts + run: deno check main.ts dev.ts working-directory: tests/fixture_error/ diff --git a/README.md b/README.md index 1223f945..65231cdc 100644 --- a/README.md +++ b/README.md @@ -29,22 +29,13 @@ the framework. The [documentation](https://fresh.deno.dev/docs/) is available on [fresh.deno.dev](https://fresh.deno.dev/). -## Install - -To install, run the following command. This will add `fresh` CLI to your PATH. -Make sure to have Deno 1.12.0 or later installed. - -```sh -deno install -A -f --no-check -n fresh -r https://raw.githubusercontent.com/lucacasonato/fresh/main/cli.ts -``` - ## Getting started -The `fresh` CLI can scaffold a new project for you. To scaffold a project in the -`myproject` folder, run the following: +You can scaffold a new project by running the Fresh init script. To scaffold a +project in the `myproject` folder, run the following: ```sh -fresh init my-project +deno run -A --no-check https://raw.githubusercontent.com/lucacasonato/fresh/main/init.ts my-project ``` To now start the project, use `deno task`: @@ -54,9 +45,9 @@ deno task start ``` To deploy the script to [Deno Deploy](https://deno.com/deploy), push your -project to GitHub, create a `fresh` project, and link it to `main.ts` file in +project to GitHub, create a Fresh project, and link it to **`main.ts`** file in the created repository. For a more in-depth getting started guide, visit the -[Getting Started](https://fresh.deno.dev/docs/getting-started) page in the -`fresh` docs. +[Getting Started](https://fresh.deno.dev/docs/getting-started) page in the Fresh +docs. diff --git a/cli.ts b/cli.ts deleted file mode 100644 index d2b35871..00000000 --- a/cli.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { parseArgs } from "./src/cli/deps.ts"; -import { initSubcommand } from "./src/cli/init.ts"; -import { manifestSubcommand } from "./src/cli/manifest.ts"; - -const VERSION = "0.1.0"; - -const help = `fresh ${VERSION} -The next-gen web framework. - -To initalize a new project: - fresh init ./myproject - -To (re-)generate the manifest file: - fresh manifest - -SUBCOMMANDS: - init Initalize a fresh project - manifest (Re-)generate the manifest file -`; - -const args = parseArgs(Deno.args, { - alias: { - "help": "h", - "version": "V", - "watch": "w", - }, - boolean: [ - "help", - "version", - "watch", - ], -}); - -const subcommand = args._.shift(); -switch (subcommand) { - case "init": - await initSubcommand(args); - break; - case "manifest": - await manifestSubcommand(args); - break; - default: - if (args.version) { - console.log(`fresh ${VERSION}`); - Deno.exit(0); - } - if (args.help) { - console.log(help); - Deno.exit(0); - } - console.error(help); - Deno.exit(1); -} diff --git a/deno.jsonc b/deno.jsonc index 046c60a4..3954c382 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,8 +1,8 @@ { "tasks": { - "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" + "test": "deno test -A --no-check=remote && deno check --config=www/deno.json www/main.ts www/dev.ts && deno check init.ts", + "fixture": "deno run -A --watch=static/,routes/,islands/ tests/fixture/dev.ts", + "www": "deno run -A --watch=static/,routes/,islands/ www/dev.ts" }, "importMap": "./tests/fixture/import_map.json" } diff --git a/dev.ts b/dev.ts new file mode 100644 index 00000000..39539a26 --- /dev/null +++ b/dev.ts @@ -0,0 +1,2 @@ +import { dev } from "./src/dev/mod.ts"; +export default dev; diff --git a/docs/data-fetching.md b/docs/data-fetching.md deleted file mode 100644 index 2927733b..00000000 --- a/docs/data-fetching.md +++ /dev/null @@ -1,62 +0,0 @@ -# Data fetching - -Not all pages can be server rendered synchronously - some pages are dynamic and -require fetching some data from an external source. This data fetching is often -asynchronous. - -Fresh supports asynchronous data fetching in handlers. This is not done during -rendering, but before rendering in the route -handler. - -## Fetching data in a `handler` - -To fetch data, you must first define a custom `handler` function. This function -is called when the route is requested. In there you can do some handling, and -then return a `Response` object. If you want to render the page, you can call -the `ctx.render` function. This returns a `Promise` that you can -return from the handler. - -The `ctx.render` function takes a single argument, which is some data that you -want to pass to the page template. The data can be accessed in the template via -the `props.data` property. - -Here is a usage example of a `handler` fetches a URL as JSON and provides the -resulting data to the template: - -```tsx -import { h, Handlers, PageProps, useData } from "../deps.ts"; - -interface ModuleInfo { - latest: string; - versions: string[]; -} - -export const handler: Handlers = { - async GET(ctx) { - const resp = await fetch("https://cdn.deno.land/std/meta/versions.json"); - let data = null; - if (resp.status === 200) data = await resp.json(); - return ctx.render(data); - }, -}; - -export default function Page(props: PageProps) { - const data = props.data; - - if (data === null) { - return
Data not available.
; - } - - const { latest, versions } = data; - return ( -

- The Deno standard library has {versions.length} versions, with {latest} - {" "} - being the most recent. -

- ); -} -``` - -If you want to fetch data client side, inside of an island, you can use -libraries like `"swr"`, or raw `useEffect` and `useState` as you usually would. diff --git a/docs/file-system-routing.md b/docs/file-system-routing.md deleted file mode 100644 index 32d9e216..00000000 --- a/docs/file-system-routing.md +++ /dev/null @@ -1,28 +0,0 @@ -# File system routing - -The naming and location of files in the `routes` folder is important because -these are used to specify the route where a specific API endpoint or HTML page -is available. This concept is called file system routing. - -As an example, the HTML page described by `index.tsx` would be served at `/`, -while the `about.tsx` page would be served at `/about`. File system routing is -also capable of dynamic path segments: `/posts/[post].jsx` would be used for -requests to `/posts/foo`, `/posts/bar`, or `/posts/baz`, but not for -`/posts/foo/comments` (that would be matched by `/posts/[post]/comments.tsx`). -Multi-level dynamic segments are also possible: `/[owner]/[repo]/[...path]` -would match `/denoland/deno/README.md` and `/lucacasonato/fresh/cli/build.rs`. - -Multi level dynamic segments can only be used as the last path segment. - -## Comparison with path-to-regex - -If you are more familiar with path-to-regex style routes, here is a simple -translation table to get you up to speed. - -| `path-to-regex` | File system routing | -| ----------------------- | ------------------------------------------------------------------ | -| `/` | `/index.tsx` | -| `/about` | `/about.tsx` or `/about/index.tsx` | -| `/posts/:name` | `/posts/[name].tsx` or `/posts/[name]/index.tsx` | -| `/posts/:name/comments` | `/posts/[name]/comments.tsx` or `/posts/[name]/comments/index.tsx` | -| `/x/:module/:path*` | `/x/[module]/[...path].tsx` | diff --git a/docs/getting-started.md b/docs/getting-started.md deleted file mode 100644 index 89019af2..00000000 --- a/docs/getting-started.md +++ /dev/null @@ -1,85 +0,0 @@ -# Getting Started - -Welcome to the `fresh` documentation. - -## Requirements - -The documentation assumes you have Deno 1.20.1 or later installed. - -To install Deno, follow the installation instructions in the manual: -https://deno.land/manual/getting_started/installation - -## Setup - -`fresh` comes with a CLI tool, conveniently also called `fresh`. It is used to -scaffold new projects and generate the route manifest. You will only need this -tool if you are creating a new project, or are creating or moving pages around -inside an existing project. The `fresh` CLI is not required for most development -or in CI, as `fresh` does not have a build step. To install `fresh`: - -``` -deno install -A -f --no-check -n fresh -r https://raw.githubusercontent.com/lucacasonato/fresh/main/cli.ts -``` - -## Creating a new project - -Creating a new project can be done as follows: - -``` -fresh init my-project -``` - -The first argument passed to `fresh init` (in the above case `my-project`), is -the path to a folder you would like to initalize a `fresh` project in. To -initalize a project in the current working directory, pass `.` as the first -argument. - -Now move into the directory you just created / initalized (for example -`cd my-project`). - -To learn more about how projects are structured, take a look at the -[Project Structure](./project-structure.md) documentation. - -## Running the project locally - -You can now run your new project locally using the `deno` CLI: - -``` -deno task start -``` - -The `--watch` option in `start` task will cause your script to be reloaded on -any changes to the source. If you do not want this you can omit the `--watch` -option in `deno.json`. - -After you have started the project, you can view it in your browser at -http://localhost:8000. You will first get served a JIT server rendered page, -which will get hydrated using client side JS after a few moments. - -## Deploying the project to Deno Deploy - -To deploy your project you will need to push the source code to either a web -server, or GitHub.com. Fresh currently only supports public GitHub repositories. -For this guide we will use GitHub.com and the Deno Deploy GitHub integration. - -First create a new repository on GitHub.com with the same account you signed up -to Deno Deploy with (or an organization you can access from that account). Then -follow the setup instructions for GitHub on your machine, and push your code -into the repository. - -Visit your project on dash.deno.com, go to the "Git" tab in the settings, select -your organization and repository, select your production branch (usually -`main`), and finally select your entrypoint module (`main.ts`. You might need to -grant the Deno Deploy app permission to the repository at this point (follow -prompts on the Deno Deploy dashboard). - -Your project is now linked to Deno Deploy and will automatically deployed on -every push. Your project now has a URL you can visit to view your site live. -Example: https://fresh.deno.dev - -## Next steps - -Now that the project is deployed, it is time to make some changes to the default -project. For that read the guides on -[project structure](./project-structure.md), [data fetching](./data-fetching.md) -and [file system routing](./file-system-routing.md) diff --git a/docs/getting-started/adding-interactivity.md b/docs/getting-started/adding-interactivity.md index ae9240b9..0b39fd04 100644 --- a/docs/getting-started/adding-interactivity.md +++ b/docs/getting-started/adding-interactivity.md @@ -17,10 +17,10 @@ static page might need a little bit of JavaScript to power a image carousel or [islands architecture][islands-architecture]. This refers to a page having little "islands" of interactivity, in a sea of otherwise static content. -`fresh` embraces this model. All pages are rendered server side, but you can +Fresh embraces this model. All pages are rendered server side, but you can create "island components" that are _also_ rendered client side. To do this, -`fresh` projects have a special `islands/` folder. The modules in this folder -each encapsulate a single island component. The name of the module should be the +Fresh projects have a special `islands/` folder. The modules in this folder each +encapsulate a single island component. The name of the module should be the [pascal case][pascal-case] name of the island component. For example a counter component would be defined in the file `islands/Counter.tsx`. A buy now button would be defined in the file `islands/BuyNowButton.tsx`. @@ -87,11 +87,7 @@ export default function Page() { } ``` -Before trying this out, one must run `fresh manifest` again to update the -`fresh.gen.ts` file with the new island component. Just like routes, island -components need to be registered in the manifest file. This means that -`fresh manifest` needs to be run every time a new island component is added, -removed, or renamed. +The page that is rendered on the client now has an interactive countdown. [islands-architecture]: https://jasonformat.com/islands-architecture [pascal-case]: https://en.wiktionary.org/wiki/Pascal_case diff --git a/docs/getting-started/create-a-project.md b/docs/getting-started/create-a-project.md index b438e046..1f376e18 100644 --- a/docs/getting-started/create-a-project.md +++ b/docs/getting-started/create-a-project.md @@ -1,30 +1,34 @@ --- description: | - Create a new fresh project by running `fresh init`. This scaffolds out the - various files and folders a fresh project needs. + Create a new fresh project by running the fresh project creation tool. This + scaffolds out the various files and folders a fresh project needs. --- -New `fresh` projects can be created with the `fresh` CLI tool. It will scaffold -out a new project with some example files to get you started. +New Fresh projects can be created with using the fresh project creation tool. It +will scaffold out a new project with some example files to get you started. To create a new project in the directory `"my-project"`, run: ``` -$ fresh init my-project -Manifest generated and written to ./my-project/fresh.gen.ts +$ deno run -A --no-check https://raw.githubusercontent.com/lucacasonato/fresh/main/init.ts my-project +The manifest has been generated for 3 routes and 1 islands. + +Project created! +Run `deno task start` in the project directory to get started. ``` -This will create a directory containing some files and directories. There are 3 +This will create a directory containing some files and directories. There are 4 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 - to be called `main.ts`, but it's best practice to call the entrypoint - `main.ts`. +- **`dev.ts`**: This is the development entrypoint for your project. This is the + file that you run to start your project. This file doesn't need to be called + `dev.ts`, but this is the convention. +- **`main.ts`**: This is the production entrypoint for your project. It is the + file that you link to Deno Deploy. This file doesn't actually need to be + `main.ts`, but this is the convention. - **`fresh.gen.ts`**: This is the manifest file that contains information about - 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. + your routes and islands. This file is automatically generated in development + based on your `routes/` and `islands/` folders. - **`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. diff --git a/docs/getting-started/create-a-route.md b/docs/getting-started/create-a-route.md index d0db8e47..50a44872 100644 --- a/docs/getting-started/create-a-route.md +++ b/docs/getting-started/create-a-route.md @@ -30,7 +30,7 @@ declare a component that should be rendered every time a user visits the page. This is done with JSX. > ℹ️ To learn more about JSX, you can read [this article][jsx] in the React -> documentation. Beware that `fresh` does not use React, but rather +> documentation. Beware that Fresh does not use React, but rather > [Preact][preact], a lighter weight virtual dom library that works similar to > React. @@ -54,24 +54,7 @@ export default function AboutPage() { > element function. These are just boilerplate. You don't need to know exactly > what they do - they just ensure that JSX get's rendered correctly. -This alone is not yet enough to actually view the page in the browser though. If -one tries to visit `http://localhost:8000/about`, the server will return a 404 -code. - -Before `fresh` can render the page, the `fresh.gen.ts` manifest needs to be -updated to reflect this new route. To do this, one can run the `fresh manifest` -command: - -``` -$ fresh manifest -Manifest generated and written to ./my-project/fresh.gen.ts -``` - -> ℹ️ Updating the manifest needs to be done every time you add, remove, or -> rename a page. It does not need to be done if just the contents of a page are -> edited. - -Now the page will be visible at `http://localhost:8000/about`. +The new page will be visible at `http://localhost:8000/about`.