feat: prebundle code + restore from snapshot (#1604)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
_fresh/
|
||||
@@ -0,0 +1,135 @@
|
||||
---
|
||||
description: |
|
||||
Fresh optimize assets ahead of time, which makes pages load way quicker.
|
||||
---
|
||||
|
||||
Fresh enables you to pre-optimize frontend assets before the code is deployed.
|
||||
During that process the code for Islands will be compressed and optimized, so
|
||||
that Fresh can send as little code as possible to the browser. Depending on the
|
||||
amount of code an island needs, this process can take several seconds if done on
|
||||
the fly serverside.
|
||||
|
||||
Doing those optimizations ahead-of-time and deploying the already optimized
|
||||
assets alongside with your code, allows Fresh to treat them as like any other
|
||||
static file and can serve it immediately without any further processing. On
|
||||
pages with islands, having to do no processing greatly speeds up page load
|
||||
times.
|
||||
|
||||
## Creating an optimized build
|
||||
|
||||
To have Fresh optimize all the assets, run one of the following commands:
|
||||
|
||||
```sh
|
||||
# As a task in newer Fresh projects
|
||||
deno task build
|
||||
# or invoke it manually
|
||||
deno run -A dev.ts build
|
||||
```
|
||||
|
||||
This will create a `_fresh` folder in the project directory. That folder
|
||||
contains the optimized assets and a `snapshot.json` file which includes some
|
||||
metadata for Fresh.
|
||||
|
||||
> ℹ️ The `_fresh` folder should not be committed to the repository. Add an entry
|
||||
> in the `.gitignore` file to ensure that it is not committed. Create that file
|
||||
> at the root of your git repository if not present.
|
||||
>
|
||||
> ```gitignore .gitignore
|
||||
> # Ignore fresh build directory
|
||||
> _fresh/
|
||||
> ```
|
||||
|
||||
## Running Fresh with optimized assets
|
||||
|
||||
When Fresh is started in non-development mode (usually via `main.ts`), Fresh
|
||||
will automatically pick up optimized assets when a `_fresh` folder exists. If
|
||||
found, Fresh will print the following message to the terminal:
|
||||
|
||||
```sh Terminal output
|
||||
Using snapshot found at /path/to/project/_fresh
|
||||
```
|
||||
|
||||
## Deploying an optimized Fresh project
|
||||
|
||||
To generate optimized assets whenever you push changes to the `main` branch of
|
||||
your project, copy the following GitHub action and add it under
|
||||
`.github/workflows/deploy.yml` in your project.
|
||||
|
||||
```yml .github/workflows/deploy.yml
|
||||
name: Deploy
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: main
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
name: Deploy
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
id-token: write # Needed for auth with Deno Deploy
|
||||
contents: read # Needed to clone the repository
|
||||
|
||||
steps:
|
||||
- name: Clone repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Install Deno
|
||||
uses: denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: v1.x
|
||||
|
||||
- name: Build step
|
||||
run: "deno task build" # 📝 Update the build command(s) if necessary
|
||||
|
||||
- name: Upload to Deno Deploy
|
||||
uses: denoland/deployctl@v1
|
||||
with:
|
||||
project: "example-project" # 📝 Update the deploy project name if necessary
|
||||
entrypoint: "./main.ts" # 📝 Update the entrypoint if necessary
|
||||
```
|
||||
|
||||
> ℹ️ **Important:** The project name here must match the project name you've
|
||||
> picked in Deno Deploy.
|
||||
|
||||
On the Deno Deploy side change the GitHub integration mode to GitHub Actions.
|
||||
You need to unlink first, if you have an existing project that's linked with the
|
||||
"Automatic" mode.
|
||||
|
||||

|
||||
|
||||
Once this is set up you're ready for your next deployment. Whenever a new PR is
|
||||
merged into the `main` branch on GitHub the deploy action will be executed and
|
||||
deploy the optimized assets to Deno Deploy.
|
||||
|
||||
## Migrating existing projects with Plugins
|
||||
|
||||
If you're using Fresh plugins, extract them into a `fresh.config.ts` file, so
|
||||
that both the `dev.ts` and `main.ts` script have access to them.
|
||||
|
||||
```ts fresh.config.ts
|
||||
import { defineConfig } from "$fresh/server.ts";
|
||||
import twindPlugin from "$fresh/plugins/twind.ts";
|
||||
import twindConfig from "./twind.config.ts";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [twindPlugin(twindConfig)],
|
||||
});
|
||||
```
|
||||
|
||||
```ts main.ts
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, config);
|
||||
```
|
||||
|
||||
```ts dev.ts
|
||||
import dev from "$fresh/dev.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts", config);
|
||||
```
|
||||
@@ -56,6 +56,7 @@ const toc: RawTableOfContents = {
|
||||
["middleware", "Middlewares", "link:canary"],
|
||||
["error-pages", "Error pages", "link:latest"],
|
||||
["data-fetching", "Data fetching", "link:latest"],
|
||||
["ahead-of-time-builds", "Ahead-of-time Builds", "link:canary"],
|
||||
["deployment", "Deployment", "link:latest"],
|
||||
["plugins", "Plugins", "link:latest"],
|
||||
["updating", "Updating Fresh", "link:latest"],
|
||||
|
||||
@@ -105,6 +105,9 @@ const GITIGNORE = `# dotenv environment variable files
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# Fresh build directory
|
||||
_fresh/
|
||||
`;
|
||||
|
||||
await Deno.writeTextFile(
|
||||
@@ -498,6 +501,20 @@ try {
|
||||
// Skip this and be silent if there is a network issue.
|
||||
}
|
||||
|
||||
let FRESH_CONFIG_TS = `import { defineConfig } from "$fresh/server.ts";\n`;
|
||||
if (useTwind) {
|
||||
FRESH_CONFIG_TS += `import twindPlugin from "$fresh/plugins/twind.ts"
|
||||
import twindConfig from "./twind.config.ts";`;
|
||||
}
|
||||
|
||||
FRESH_CONFIG_TS += `
|
||||
export default defineConfig({${
|
||||
useTwind ? `\n plugins: [twindPlugin(twindConfig)]\n` : ""
|
||||
}});
|
||||
`;
|
||||
const CONFIG_TS_PATH = join(resolvedDirectory, "fresh.config.ts");
|
||||
await Deno.writeTextFile(CONFIG_TS_PATH, FRESH_CONFIG_TS);
|
||||
|
||||
let MAIN_TS = `/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
@@ -508,27 +525,20 @@ import "$std/dotenv/load.ts";
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
`;
|
||||
|
||||
if (useTwind) {
|
||||
MAIN_TS += `
|
||||
import twindPlugin from "$fresh/plugins/twind.ts";
|
||||
import twindConfig from "./twind.config.ts";
|
||||
`;
|
||||
}
|
||||
|
||||
MAIN_TS += `
|
||||
await start(manifest${
|
||||
useTwind ? ", { plugins: [twindPlugin(twindConfig)] }" : ""
|
||||
});\n`;
|
||||
await start(manifest, config);\n`;
|
||||
const MAIN_TS_PATH = join(resolvedDirectory, "main.ts");
|
||||
await Deno.writeTextFile(MAIN_TS_PATH, MAIN_TS);
|
||||
|
||||
const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts");
|
||||
await dev(import.meta.url, "./main.ts", config);
|
||||
`;
|
||||
const DEV_TS_PATH = join(resolvedDirectory, "dev.ts");
|
||||
await Deno.writeTextFile(DEV_TS_PATH, DEV_TS);
|
||||
@@ -544,6 +554,8 @@ const config = {
|
||||
check:
|
||||
"deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
|
||||
start: "deno run -A --watch=static/,routes/ dev.ts",
|
||||
build: "deno run -A dev.ts build",
|
||||
preview: "deno run -A main.ts",
|
||||
update: "deno run -A -r https://fresh.deno.dev/update .",
|
||||
},
|
||||
lint: {
|
||||
|
||||
@@ -163,7 +163,7 @@ export class EsbuildSnapshot implements BuildSnapshot {
|
||||
}
|
||||
|
||||
get paths(): string[] {
|
||||
return Object.keys(this.#files);
|
||||
return Array.from(this.#files.keys());
|
||||
}
|
||||
|
||||
read(path: string): Uint8Array | null {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { ServerContext } from "../server/context.ts";
|
||||
import { FreshOptions, Manifest } from "../server/mod.ts";
|
||||
import { dirname, fromFileUrl, join, toFileUrl } from "../server/deps.ts";
|
||||
import { fs } from "./deps.ts";
|
||||
|
||||
export async function build(
|
||||
manifestPath: string,
|
||||
opts: FreshOptions,
|
||||
) {
|
||||
const manifest = (await import(toFileUrl(manifestPath).href))
|
||||
.default as Manifest;
|
||||
|
||||
const outDir = join(dirname(fromFileUrl(manifest.baseUrl)), "_fresh");
|
||||
|
||||
// Ensure that build dir is empty
|
||||
await fs.emptyDir(outDir);
|
||||
|
||||
const ctx = await ServerContext.fromManifest(manifest, {
|
||||
...opts,
|
||||
skipSnapshot: false,
|
||||
});
|
||||
|
||||
// Bundle assets
|
||||
const snapshot = await ctx.buildSnapshot();
|
||||
|
||||
// Write output files to disk
|
||||
await Promise.all(snapshot.paths.map((fileName) => {
|
||||
const data = snapshot.read(fileName);
|
||||
if (data === null) return;
|
||||
|
||||
return Deno.writeFile(join(outDir, fileName), data);
|
||||
}));
|
||||
|
||||
// Write dependency snapshot file to disk
|
||||
const deps: Record<string, string[]> = {};
|
||||
for (const filePath of snapshot.paths) {
|
||||
const dependencies = snapshot.dependencies(filePath);
|
||||
deps[filePath] = dependencies;
|
||||
}
|
||||
|
||||
const snapshotPath = join(outDir, "snapshot.json");
|
||||
await Deno.writeTextFile(snapshotPath, JSON.stringify(deps, null, 2));
|
||||
}
|
||||
@@ -23,6 +23,7 @@ export { gte } from "https://deno.land/std@0.193.0/semver/mod.ts";
|
||||
export { existsSync } from "https://deno.land/std@0.193.0/fs/mod.ts";
|
||||
export * as semver from "https://deno.land/std@0.195.0/semver/mod.ts";
|
||||
export * as JSONC from "https://deno.land/std@0.195.0/jsonc/mod.ts";
|
||||
export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts";
|
||||
|
||||
// ts-morph
|
||||
export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts";
|
||||
|
||||
+12
-2
@@ -11,6 +11,8 @@ import {
|
||||
WalkEntry,
|
||||
} from "./deps.ts";
|
||||
import { error } from "./error.ts";
|
||||
import { FreshOptions } from "../server/mod.ts";
|
||||
import { build } from "./build.ts";
|
||||
|
||||
const MIN_DENO_VERSION = "1.31.0";
|
||||
|
||||
@@ -183,7 +185,11 @@ export default manifest;
|
||||
);
|
||||
}
|
||||
|
||||
export async function dev(base: string, entrypoint: string) {
|
||||
export async function dev(
|
||||
base: string,
|
||||
entrypoint: string,
|
||||
options: FreshOptions = {},
|
||||
) {
|
||||
ensureMinDenoVersion();
|
||||
|
||||
// Run update check in background
|
||||
@@ -209,7 +215,11 @@ export async function dev(base: string, entrypoint: string) {
|
||||
|
||||
if (manifestChanged) await generate(dir, newManifest);
|
||||
|
||||
await import(entrypoint);
|
||||
if (Deno.args.includes("build")) {
|
||||
await build(join(dir, "fresh.gen.ts"), options);
|
||||
} else {
|
||||
await import(entrypoint);
|
||||
}
|
||||
}
|
||||
|
||||
function arraysEqual<T>(a: T[], b: T[]): boolean {
|
||||
|
||||
+42
-4
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
colors,
|
||||
dirname,
|
||||
extname,
|
||||
fromFileUrl,
|
||||
@@ -51,6 +52,7 @@ import {
|
||||
Builder,
|
||||
BuildSnapshot,
|
||||
EsbuildBuilder,
|
||||
EsbuildSnapshot,
|
||||
JSXConfig,
|
||||
} from "../build/mod.ts";
|
||||
import { InternalRoute } from "./router.ts";
|
||||
@@ -117,6 +119,7 @@ export class ServerContext {
|
||||
jsxConfig: JSXConfig,
|
||||
dev: boolean = isDevMode(),
|
||||
routerOptions: RouterOptions,
|
||||
snapshot: BuildSnapshot | null = null,
|
||||
) {
|
||||
this.#routes = routes;
|
||||
this.#islands = islands;
|
||||
@@ -129,7 +132,7 @@ export class ServerContext {
|
||||
this.#error = error;
|
||||
this.#plugins = plugins;
|
||||
this.#dev = dev;
|
||||
this.#builder = new EsbuildBuilder({
|
||||
this.#builder = snapshot ?? new EsbuildBuilder({
|
||||
buildID: BUILD_ID,
|
||||
entrypoints: collectEntrypoints(this.#dev, this.#islands, this.#plugins),
|
||||
configPath,
|
||||
@@ -144,7 +147,7 @@ export class ServerContext {
|
||||
*/
|
||||
static async fromManifest(
|
||||
manifest: Manifest,
|
||||
opts: FreshOptions,
|
||||
opts: FreshOptions & { skipSnapshot?: boolean },
|
||||
): Promise<ServerContext> {
|
||||
// Get the manifest' base URL.
|
||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||
@@ -158,6 +161,40 @@ export class ServerContext {
|
||||
);
|
||||
}
|
||||
|
||||
// Restore snapshot if available
|
||||
let snapshot: BuildSnapshot | null = null;
|
||||
// Load from snapshot if not explicitly requested not to
|
||||
const loadFromSnapshot = !opts.skipSnapshot;
|
||||
if (loadFromSnapshot) {
|
||||
const snapshotDirPath = join(dirname(configPath), "_fresh");
|
||||
try {
|
||||
if ((await Deno.stat(snapshotDirPath)).isDirectory) {
|
||||
console.log(
|
||||
`Using snapshot found at ${colors.cyan(snapshotDirPath)}`,
|
||||
);
|
||||
|
||||
const snapshotPath = join(snapshotDirPath, "snapshot.json");
|
||||
const json = JSON.parse(await Deno.readTextFile(snapshotPath));
|
||||
const dependencies = new Map<string, string[]>(
|
||||
Object.entries(json),
|
||||
);
|
||||
|
||||
const files = new Map();
|
||||
const names = Object.keys(json);
|
||||
await Promise.all(names.map(async (name) => {
|
||||
const filePath = join(snapshotDirPath, name);
|
||||
files.set(name, await Deno.readFile(filePath));
|
||||
}));
|
||||
|
||||
snapshot = new EsbuildSnapshot(files, dependencies);
|
||||
}
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config.compilerOptions ??= {};
|
||||
|
||||
let jsx: "react" | "react-jsx";
|
||||
@@ -415,6 +452,7 @@ export class ServerContext {
|
||||
jsxConfig,
|
||||
dev,
|
||||
opts.router ?? DEFAULT_ROUTER_OPTIONS,
|
||||
snapshot,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -474,7 +512,7 @@ export class ServerContext {
|
||||
return this.#builder;
|
||||
}
|
||||
|
||||
async #buildSnapshot() {
|
||||
async buildSnapshot() {
|
||||
if ("build" in this.#builder) {
|
||||
const builder = this.#builder;
|
||||
this.#builder = builder.build();
|
||||
@@ -950,7 +988,7 @@ export class ServerContext {
|
||||
*/
|
||||
#bundleAssetRoute = (): router.MatchHandler => {
|
||||
return async (_req, _ctx, params) => {
|
||||
const snapshot = await this.#buildSnapshot();
|
||||
const snapshot = await this.buildSnapshot();
|
||||
const contents = snapshot.read(params.path);
|
||||
if (!contents) return new Response(null, { status: 404 });
|
||||
|
||||
|
||||
@@ -4,10 +4,15 @@ import {
|
||||
AsyncRoute,
|
||||
LayoutContext,
|
||||
RouteContext,
|
||||
StartOptions,
|
||||
} from "./types.ts";
|
||||
import { checkAsyncComponent } from "./render.ts";
|
||||
import { AppContext } from "$fresh/server.ts";
|
||||
|
||||
export function defineConfig(config: StartOptions): StartOptions {
|
||||
return config;
|
||||
}
|
||||
|
||||
// Route creation helpers
|
||||
export function defineRoute<
|
||||
T,
|
||||
|
||||
+7
-1
@@ -15,7 +15,12 @@ import {
|
||||
StartOptions,
|
||||
UnknownHandler,
|
||||
} from "./types.ts";
|
||||
export { defineApp, defineLayout, defineRoute } from "./defines.ts";
|
||||
export {
|
||||
defineApp,
|
||||
defineConfig,
|
||||
defineLayout,
|
||||
defineRoute,
|
||||
} from "./defines.ts";
|
||||
export type {
|
||||
AppContext,
|
||||
AppProps,
|
||||
@@ -78,6 +83,7 @@ export interface Manifest {
|
||||
export interface DenoConfig {
|
||||
imports?: Record<string, string>;
|
||||
importMap?: string;
|
||||
tasks?: Record<string, string>;
|
||||
compilerOptions?: {
|
||||
jsx?: string;
|
||||
jsxImportSource?: string;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import * as path from "$std/path/mod.ts";
|
||||
import { puppeteer } from "./deps.ts";
|
||||
import { assert } from "$std/_util/asserts.ts";
|
||||
import { startFreshServer, waitForText } from "$fresh/tests/test_utils.ts";
|
||||
|
||||
Deno.test("build snapshot and restore from it", async (t) => {
|
||||
const fixture = path.join(Deno.cwd(), "tests", "fixture_build");
|
||||
const outDir = path.join(fixture, "_fresh");
|
||||
|
||||
try {
|
||||
await t.step("build snapshot", async () => {
|
||||
await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(fixture, "dev.ts"),
|
||||
"build",
|
||||
],
|
||||
stdin: "null",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
}).output();
|
||||
|
||||
assert((await Deno.stat(outDir)).isDirectory, "Missing output directory");
|
||||
});
|
||||
|
||||
await t.step("check snapshot file", async () => {
|
||||
const snapshot = JSON.parse(
|
||||
await Deno.readTextFile(path.join(outDir, "snapshot.json")),
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["island-counter_default.js"]),
|
||||
"Island output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["main.js"]),
|
||||
"main.js output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["signals.js"]),
|
||||
"signals.js output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["deserializer.js"]),
|
||||
"deserializer.js output file not found in snapshot",
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("restore from snapshot", async () => {
|
||||
const { lines, serverProcess, address, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(fixture, "./main.ts"),
|
||||
],
|
||||
});
|
||||
|
||||
// Check if restore snapshot message was printed
|
||||
assert(
|
||||
output.find((line) => line.includes("Using snapshot found at")),
|
||||
"Did not print restoring from snapshot line",
|
||||
);
|
||||
|
||||
try {
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await page.goto(address);
|
||||
|
||||
await page.waitForSelector("button:not([disabled])");
|
||||
await page.click("button");
|
||||
|
||||
await waitForText(page, "p", "1");
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
} finally {
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
await Deno.remove(path.join(fixture, "_fresh"), { recursive: true });
|
||||
}
|
||||
});
|
||||
+23
-1
@@ -1,5 +1,6 @@
|
||||
import * as path from "$std/path/mod.ts";
|
||||
import { Status } from "../src/server/deps.ts";
|
||||
import { DenoConfig } from "$fresh/server.ts";
|
||||
import { JSONC, Status } from "../src/server/deps.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
@@ -49,6 +50,8 @@ Deno.test({
|
||||
|
||||
const files = [
|
||||
`/README.md`,
|
||||
`/.gitignore`,
|
||||
`/deno.json`,
|
||||
`/fresh.gen.ts`,
|
||||
`/components/Button.tsx`,
|
||||
`/islands/Counter.tsx`,
|
||||
@@ -79,6 +82,15 @@ Deno.test({
|
||||
assertEquals(code, 0);
|
||||
});
|
||||
|
||||
await t.step("check deno.json", async () => {
|
||||
const configPath = path.join(tmpDirName, "deno.json");
|
||||
const json = JSON.parse(await Deno.readTextFile(configPath));
|
||||
|
||||
assert(json.tasks.start, "Missing 'start' task");
|
||||
assert(json.tasks.build, "Missing 'build' task");
|
||||
assert(json.tasks.preview, "Missing 'preview' task");
|
||||
});
|
||||
|
||||
await t.step("start up the server and access the root page", async () => {
|
||||
const { serverProcess, lines, address } = await startFreshServer({
|
||||
args: ["run", "-A", "--check", "main.ts"],
|
||||
@@ -371,10 +383,20 @@ Deno.test("fresh-update", async function fn(t) {
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("check deno.json", async () => {
|
||||
const configPath = path.join(tmpDirName, "deno.json");
|
||||
const json = JSONC.parse(await Deno.readTextFile(configPath)) as DenoConfig;
|
||||
|
||||
assert(json.tasks?.start, "Missing 'start' task");
|
||||
assert(json.tasks?.build, "Missing 'build' task");
|
||||
assert(json.tasks?.preview, "Missing 'preview' task");
|
||||
});
|
||||
|
||||
const comment = "// This is a test comment";
|
||||
const regex = /("preact": "https:\/\/esm.sh\/preact@[\d.]+",\n)/;
|
||||
const originalName = `${tmpDirName}/deno.json`;
|
||||
const updatedName = `${originalName}c`;
|
||||
|
||||
await t.step("execute update command deno.jsonc support", async () => {
|
||||
try {
|
||||
Deno.renameSync(originalName, updatedName);
|
||||
|
||||
@@ -29,3 +29,4 @@ export {
|
||||
} from "https://esm.sh/linkedom@0.15.1";
|
||||
export { defineConfig, type Preset } from "https://esm.sh/@twind/core@1.1.3";
|
||||
export { default as presetTailwind } from "https://esm.sh/@twind/preset-tailwind@1.1.4";
|
||||
export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts";
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"lock": false,
|
||||
"imports": {
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "preact"
|
||||
}
|
||||
}
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts");
|
||||
@@ -0,0 +1,18 @@
|
||||
// DO NOT EDIT. This file is generated by Fresh.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/index.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $0,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { Signal } from "@preact/signals";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
|
||||
interface CounterProps {
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
|
||||
await start(routes);
|
||||
@@ -0,0 +1,11 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
|
||||
export default function Home() {
|
||||
const signal = useSignal(0);
|
||||
return (
|
||||
<div>
|
||||
<Counter id="counter" count={signal} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+5
-3
@@ -15,13 +15,13 @@ export function parseHtml(input: string) {
|
||||
}
|
||||
|
||||
export async function startFreshServer(options: Deno.CommandOptions) {
|
||||
const { serverProcess, lines, address } = await spawnServer(options);
|
||||
const { serverProcess, lines, address, output } = await spawnServer(options);
|
||||
|
||||
if (!address) {
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
return { serverProcess, lines, address };
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
export async function fetchHtml(url: string) {
|
||||
@@ -259,8 +259,10 @@ async function spawnServer(
|
||||
preventCancel: true,
|
||||
});
|
||||
|
||||
const output: string[] = [];
|
||||
let address = "";
|
||||
for await (const line of lines) {
|
||||
output.push(line);
|
||||
const match = line.match(/https?:\/\/localhost:\d+/g);
|
||||
if (match) {
|
||||
address = match[0];
|
||||
@@ -268,5 +270,5 @@ async function spawnServer(
|
||||
}
|
||||
}
|
||||
|
||||
return { serverProcess, lines, address };
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
@@ -88,6 +88,16 @@ if (!denoJson.lint.rules.tags.includes("recommended")) {
|
||||
denoJson.lint.rules.tags.push("recommended");
|
||||
}
|
||||
|
||||
if (!denoJson.tasks) {
|
||||
denoJson.tasks = {};
|
||||
}
|
||||
if (!denoJson.tasks.build) {
|
||||
denoJson.tasks.build = "deno run -A dev.ts build";
|
||||
}
|
||||
if (!denoJson.tasks.preview) {
|
||||
denoJson.tasks.preview = "deno run -A main.ts";
|
||||
}
|
||||
|
||||
freshImports(denoJson.imports);
|
||||
if (denoJson.imports["twind"]) {
|
||||
twindImports(denoJson.imports);
|
||||
|
||||
+3
-1
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"lock": false,
|
||||
"tasks": {
|
||||
"start": "deno run -A --watch=static/,routes/ dev.ts"
|
||||
"start": "deno run -A --watch=static/,routes/ dev.ts",
|
||||
"build": "deno run -A dev.ts build",
|
||||
"preview": "deno run -A main.ts"
|
||||
},
|
||||
"imports": {
|
||||
"$fresh/": "../",
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts");
|
||||
await dev(import.meta.url, "./main.ts", config);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import twindPlugin from "$fresh/plugins/twind.ts";
|
||||
import twindConfig from "./twind.config.ts";
|
||||
import { defineConfig } from "$fresh/server.ts";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [twindPlugin(twindConfig)],
|
||||
});
|
||||
+2
-3
@@ -5,9 +5,8 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import twindPlugin from "$fresh/plugins/twind.ts";
|
||||
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import twindConfig from "./twind.config.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
||||
await start(manifest, config);
|
||||
|
||||
@@ -77,6 +77,13 @@ ol.nested li:before {
|
||||
font-size: .8125rem;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.highlight-source-yml .atrule {
|
||||
color: var(--color-prettylights-syntax-entity);
|
||||
}
|
||||
.highlight-source-yml .string {
|
||||
color: var(--color-prettylights-syntax-string);
|
||||
}
|
||||
`;
|
||||
|
||||
export const handler: Handlers = {
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 172 KiB |
@@ -5,6 +5,7 @@ import "https://esm.sh/prismjs@1.29.0/components/prism-tsx.js?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-diff.js?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-json.js?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-bash.js?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-yaml.js?no-check";
|
||||
|
||||
export { extract as frontMatter } from "$std/front_matter/yaml.ts";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user