major: build islands + scan routeDir without importing user code (#3122)

This fixes a bunch of long standing issues in our tracker where a build
wouldn't finish because of importing user code. It's common for backend
apps to spawn some open ended tasks like subscribing to Deno KV or other
things. This addresses another problem of having to set up ENV vars just
for the build, even though they are technically not required to build
islands.

This also gives a roughly ~30% performance boost on cold starts (at
least on `deno.com`). Unfortunately, this does require some breaking
changes. But I believe the performance improvements + removing the
footgun of loading user code during build is well worth it. This work is
also a precursor in case we want to exploring moving to vite at some
point in the future.

## Breaking changes

1. Build options have moved to the builder.
```diff
  // main.ts
- const app = new App({ build: { outDir: "dist/" } })
+ const app = new App()

  // dev.ts
- const builder = new Builder();
+ const builder = new Builder({ outDir: "dist/" });
```

2. Another breaking change is how file based routes are loaded. Since
they're prepared and loaded by the builder the API changed.

```diff
  // main.ts
  const app = new App()
    .use(staticFiles())
  
- await fsRoutes(app, {
-   loadIsland: (path) => import(`./islands/${path}`),
-   loadRoute: (path) => import(`./routes/${path}`),
- }); 
+ app.fsRoutes()
```
You can optionally change the `islandDir` or `routeDir` by passing that
to the `Builder`:

```ts
// dev.ts
const builder = new Builder({
  islandDir: "/path/to/islands", 
  routeDir: "/path/to/routes"
});
```

3. Import `app` lazily in `dev.ts`. Passing `app` to `builder.build()`
is not necessary anymore.

```diff
- import { app } from "./main.ts";

  const builder = new Builder({ target: "safari12" });
  
  if (Deno.args.includes("build")) {
-    await builder.build(app);
+    await builder.build();
  } else {
-   await builder.listen(app);
+   await builder.listen(() => import("./main.ts"));
  }
```

4. The production run command has changed. It's now using `deno serve`
```diff
  // deno.json
  {
    "tasks": {
      "dev": "deno run -A --watch=static/,routes/ dev.ts",
      "dev": "deno run -A dev.ts build",
-     "start": "deno run -A main.ts",
+     "start": "deno serve -A _fresh/server.js",
    }
  }
```
Remove this bit in `main.ts`:

```diff
  // main.ts
  
- if (import.meta.main) {
-   await app.listen()
- }
```

Fixes https://github.com/denoland/fresh/issues/2240
Fixes https://github.com/denoland/fresh/issues/2640
Fixes https://github.com/denoland/fresh/issues/2592
Fixes https://github.com/denoland/fresh/issues/1843
This commit is contained in:
Marvin Hagemeister
2025-07-24 15:04:05 +02:00
committed by GitHub
parent 69af00ef65
commit 118242eaa6
54 changed files with 1911 additions and 1770 deletions
+1 -2
View File
@@ -22,7 +22,6 @@ jobs:
uses: denoland/setup-deno@v2
with:
cache: true
deno-version: rc
- name: Build step
working-directory: ./www
@@ -32,5 +31,5 @@ jobs:
uses: denoland/deployctl@v1
with:
project: "fresh"
entrypoint: "./www/main.ts"
entrypoint: "./www/_fresh/server.js"
root: "."
+2 -1
View File
@@ -14,7 +14,8 @@
".": "./src/mod.ts",
"./runtime": "./src/runtime/shared.ts",
"./dev": "./src/dev/mod.ts",
"./compat": "./src/compat.ts"
"./compat": "./src/compat.ts",
"./do-not-use": "./src/internals.ts"
},
"tasks": {
"test": "deno test -A --parallel",
+1 -1
View File
@@ -29,7 +29,7 @@ To run Fresh in production mode, run the `start` task:
```sh Terminal
deno task start
# or
deno run -A main.ts
deno serve -A _fresh/server.js
```
Fresh will automatically pick up the optimized assets in the `_fresh` directory.
+9 -19
View File
@@ -53,21 +53,20 @@ The full `dev.ts` file for newly generated Fresh 2 projects looks like this:
```ts
import { Builder } from "fresh/dev";
import { tailwind } from "@fresh/plugin-tailwind";
import { app } from "./main.ts";
// Pass development only configuration here
const builder = new Builder({ target: "safari12" });
// Example: Enabling the tailwind plugin for Fresh
tailwind(builder, app);
tailwind(builder);
// Create optimized assets for the browser when
// running `deno run -A dev.ts build`
if (Deno.args.includes("build")) {
await builder.build(app);
await builder.build();
} else {
// ...otherwise start the development server
await builder.listen(app);
await builder.listen(() => import("./main.ts"));
}
```
@@ -82,18 +81,9 @@ import { App, fsRoutes, staticFiles } from "fresh";
export const app = new App()
// Add static file serving middleware
.use(staticFiles());
// Enable file-system based routing
await fsRoutes(app, {
loadIsland: (path) => import(`./islands/${path}`),
loadRoute: (path) => import(`./routes/${path}`),
});
// If this module is called directly, start the server
if (import.meta.main) {
await app.listen();
}
.use(staticFiles())
// Enable file-system based routing
.fsRoutes();
```
## Merging error pages
@@ -226,9 +216,9 @@ Same is true for handlers:
All the various context interfaces have been consolidated and simplified:
| Fresh 1.x | Fresh 2.x |
| --------------------------------------------- | -------------- |
| `AppContext`, `LayoutContext`, `RouteContext` | `FreshContext` |
| Fresh 1.x | Fresh 2.x |
| --------------------------------------------- | ------------------------------------------ |
| `AppContext`, `LayoutContext`, `RouteContext` | [`Context`](/docs/canary/concepts/context) |
### Context methods
-10
View File
@@ -15,16 +15,6 @@ import { DemoIsland } from "jsr:@fresh/examples/island";
export const app = new App({ root: import.meta.url })
.use(staticFiles());
// Register the island
app.island(
// Module specifier for esbuild, could also be a file path
"jsr:@fresh/examples/island",
// Name of the island
"DemoIsland",
// Island component function
DemoIsland,
);
// Use the island somewhere in your components
app.get("/", (ctx) => ctx.render(<DemoIsland />));
+9 -16
View File
@@ -158,11 +158,11 @@ ENV DENO_DEPLOYMENT_ID=\${GIT_REVISION}
WORKDIR /app
COPY . .
RUN deno cache main.ts
RUN deno cache _fresh/server.js
EXPOSE 8000
CMD ["run", "-A", "main.ts"]
CMD ["serve", "-A", "_fresh/server.js"]
`;
await writeFile("Dockerfile", DOCKERFILE_TEXT);
@@ -349,7 +349,7 @@ ${GRADIENT_CSS}`;
// Skip this and be silent if there is a network issue.
}
const MAIN_TS = `import { App, fsRoutes, staticFiles } from "fresh";
const MAIN_TS = `import { App, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
export const app = new App<State>();
@@ -371,14 +371,8 @@ const exampleLoggerMiddleware = define.middleware((ctx) => {
});
app.use(exampleLoggerMiddleware);
await fsRoutes(app, {
loadIsland: (path) => import(\`./islands/\${path}\`),
loadRoute: (path) => import(\`./routes/\${path}\`),
});
if (import.meta.main) {
await app.listen();
}`;
// Include file-system based routes here
app.fsRoutes();`;
await writeFile("main.ts", MAIN_TS);
const COMPONENTS_BUTTON_TSX =
@@ -489,14 +483,13 @@ export default function Counter(props: CounterProps) {
const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/
${useTailwind ? `import { tailwind } from "@fresh/plugin-tailwind";\n` : ""}
import { Builder } from "fresh/dev";
import { app } from "./main.ts";
const builder = new Builder();
${useTailwind ? "tailwind(builder, app);" : ""}
${useTailwind ? "tailwind(builder);" : ""}
if (Deno.args.includes("build")) {
await builder.build(app);
await builder.build();
} else {
await builder.listen(app);
await builder.listen(() => import("./main.ts"));
}`;
await writeFile("dev.ts", DEV_TS);
@@ -506,7 +499,7 @@ if (Deno.args.includes("build")) {
check: "deno fmt --check . && deno lint . && deno check",
dev: "deno run -A --watch=static/,routes/ dev.ts",
build: "deno run -A dev.ts build",
start: "deno run -A main.ts",
start: "deno serve -A _fresh/server.js",
update: "deno run -A -r jsr:@fresh/update .",
},
lint: {
+23 -20
View File
@@ -139,28 +139,31 @@ Deno.test({
},
});
Deno.test("init with tailwind - fmt, lint, and type check project", async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
Deno.test(
"init with tailwind - fmt, lint, and type check project",
async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
await patchProject(dir);
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
});
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
},
);
Deno.test("init - can start dev server", async () => {
await using tmp = await withTmpDir();
@@ -240,5 +243,5 @@ Deno.test("init - errors on missing build cache in prod", async () => {
const { stderr } = getStdOutput(cp);
expect(cp.code).toEqual(1);
expect(stderr).toMatch(/Found 1 islands, but did not/);
expect(stderr).toMatch(/Module not found/);
});
+2 -4
View File
@@ -1,5 +1,4 @@
import type { Builder } from "fresh/dev";
import type { App } from "fresh";
import twPostcss from "@tailwindcss/postcss";
import postcss from "postcss";
import type { TailwindPluginOptions } from "./types.ts";
@@ -7,14 +6,13 @@ import type { TailwindPluginOptions } from "./types.ts";
// Re-export types for public API
export type { TailwindPluginOptions } from "./types.ts";
export function tailwind<T>(
export function tailwind(
builder: Builder,
app: App<T>,
options: TailwindPluginOptions = {},
): void {
const { exclude, ...tailwindOptions } = options;
const instance = postcss(twPostcss({
optimize: app.config.mode === "production",
optimize: builder.config.mode === "production",
...tailwindOptions,
}));
+101 -221
View File
@@ -1,35 +1,30 @@
import { type ComponentType, h } from "preact";
import { renderToString } from "preact-render-to-string";
import { trace } from "@opentelemetry/api";
import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
import { Context, type ServerIslandRegistry } from "./context.ts";
import {
mergePath,
type Method,
type Router,
UrlPatternRouter,
} from "./router.ts";
import {
type FreshConfig,
normalizeConfig,
type ResolvedFreshConfig,
} from "./config.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
import { Context } from "./context.ts";
import { mergePath, type Method, UrlPatternRouter } from "./router.ts";
import type { FreshConfig, ResolvedFreshConfig } from "./config.ts";
import type { BuildCache } from "./build_cache.ts";
import { HttpError } from "./error.ts";
import { pathToExportName } from "./utils.ts";
import type { LayoutConfig, Route } from "./types.ts";
import type { RouteComponent } from "./segments.ts";
import {
getOrCreateSegment,
newSegment,
renderRoute,
type RouteComponent,
segmentToMiddlewares,
} from "./segments.ts";
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
applyCommands,
type Command,
CommandType,
DEFAULT_NOT_ALLOWED_METHOD,
DEFAULT_NOT_FOUND,
newAppCmd,
newErrorCmd,
newHandlerCmd,
newLayoutCmd,
newMiddlewareCmd,
newNotFoundCmd,
newRouteCmd,
} from "./commands.ts";
import { MockBuildCache } from "./test_utils.ts";
// TODO: Completed type clashes in older Deno versions
// deno-lint-ignore no-explicit-any
@@ -38,16 +33,6 @@ export const DEFAULT_CONN_INFO: any = {
remoteAddr: { transport: "tcp", hostname: "localhost", port: 1234 },
};
const DEFAULT_RENDER = <State>(): Promise<PageResponse<State>> =>
// deno-lint-ignore no-explicit-any
Promise.resolve({ data: {} as any });
const DEFAULT_NOT_FOUND = (): Promise<Response> => {
throw new HttpError(404);
};
const DEFAULT_NOT_ALLOWED_METHOD = (): Promise<Response> => {
throw new HttpError(405);
};
const defaultOptionsHandler = (methods: string[]): () => Promise<Response> => {
return () =>
Promise.resolve(
@@ -159,35 +144,26 @@ async function listenOnFreePort(
throw firstError;
}
// deno-lint-ignore no-explicit-any
export let getIslandRegistry: (app: App<any>) => ServerIslandRegistry;
// deno-lint-ignore no-explicit-any
export let getBuildCache: (app: App<any>) => BuildCache | null;
// deno-lint-ignore no-explicit-any
export let setBuildCache: (app: App<any>, cache: BuildCache | null) => void;
export let getBuildCache: <State>(app: App<State>) => BuildCache<State> | null;
export let setBuildCache: <State>(
app: App<State>,
cache: BuildCache<State>,
) => void;
/**
* Create an application instance that passes the incoming `Request`
* instance through middlewares and routes.
*/
export class App<State> {
#router: Router<MiddlewareFn<State>> = new UrlPatternRouter<
MiddlewareFn<State>
>();
#islandRegistry: ServerIslandRegistry = new Map();
#buildCache: BuildCache | null = null;
#islandNames = new Set<string>();
#root = newSegment<State>("", null);
#routeDefs: {
method: Method | "ALL";
pattern: string;
fns: MiddlewareFn<State>[];
}[] = [];
#getBuildCache: () => BuildCache<State> | null = () => null;
#commands: Command<State>[] = [];
static {
getIslandRegistry = (app) => app.#islandRegistry;
getBuildCache = (app) => app.#buildCache;
setBuildCache = (app, cache) => app.#buildCache = cache;
getBuildCache = (app) => app.#getBuildCache();
setBuildCache = (app, cache) => {
app.config.root = cache.root;
app.#getBuildCache = () => cache;
};
}
/**
@@ -196,33 +172,11 @@ export class App<State> {
config: ResolvedFreshConfig;
constructor(config: FreshConfig = {}) {
this.config = normalizeConfig(config);
}
island(
filePathOrUrl: string | URL,
exportName: string,
// deno-lint-ignore no-explicit-any
fn: ComponentType<any>,
): this {
const filePath = filePathOrUrl instanceof URL
? filePathOrUrl.href
: filePathOrUrl;
// Create unique island name
let name = exportName === "default"
? pathToExportName(filePath)
: exportName;
if (this.#islandNames.has(name)) {
let i = 0;
while (this.#islandNames.has(`${name}_${i}`)) {
i++;
}
name = `${name}_${i}`;
}
this.#islandRegistry.set(fn, { fn, exportName, name, file: filePathOrUrl });
return this;
this.config = {
root: Deno.cwd(),
basePath: config.basePath ?? "",
mode: "production",
};
}
/**
@@ -234,19 +188,18 @@ export class App<State> {
pathOrMiddleware: string | MiddlewareFn<State>,
...middlewares: MiddlewareFn<State>[]
): this {
let path: string;
let pattern: string;
let fns: MiddlewareFn<State>[];
if (typeof pathOrMiddleware === "string") {
path = pathOrMiddleware;
pattern = pathOrMiddleware;
fns = middlewares!;
} else {
path = "*";
pattern = "*";
middlewares.unshift(pathOrMiddleware);
fns = middlewares;
}
const segment = getOrCreateSegment(this.#root, path, true);
segment.middlewares.push(...fns);
this.#commands.push(newMiddlewareCmd(pattern, fns, true));
return this;
}
@@ -255,12 +208,7 @@ export class App<State> {
* Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode MiddlewareFn}.
*/
notFound(routeOrMiddleware: Route<State> | MiddlewareFn<State>): this {
const route = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(route);
this.#root.notFound = (ctx) => renderRoute(ctx, route);
this.#commands.push(newNotFoundCmd(routeOrMiddleware));
return this;
}
@@ -268,19 +216,13 @@ export class App<State> {
path: string,
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
): this {
const segment = getOrCreateSegment(this.#root, path, true);
segment.errorRoute = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(segment.errorRoute);
this.#commands.push(newErrorCmd(path, routeOrMiddleware, true));
return this;
}
appWrapper(component: RouteComponent<State>) {
const segment = getOrCreateSegment<State>(this.#root, "", false);
segment.app = component;
appWrapper(component: RouteComponent<State>): this {
this.#commands.push(newAppCmd(component));
return this;
}
layout(
@@ -288,32 +230,12 @@ export class App<State> {
component: RouteComponent<State>,
config?: LayoutConfig,
): this {
const segment = getOrCreateSegment<State>(this.#root, path, true);
segment.layout = { component, config: config ?? null };
this.#commands.push(newLayoutCmd(path, component, config, true));
return this;
}
route(path: string, route: Route<State>): this {
const segment = getOrCreateSegment<State>(this.#root, path, false);
const middlewares = segmentToMiddlewares(segment);
ensureHandler(route);
middlewares.push((ctx) => renderRoute(ctx, route));
const routePath = mergePath(
this.config.basePath,
route.config?.routeOverride ?? path,
);
if (typeof route.handler === "function") {
this.#addRoute("ALL", routePath, middlewares);
} else if (isHandlerByMethod(route.handler!)) {
for (const method of Object.keys(route.handler)) {
this.#addRoute(method as Method, routePath, middlewares);
}
}
this.#commands.push(newRouteCmd(path, route, false));
return this;
}
@@ -321,42 +243,42 @@ export class App<State> {
* Add middlewares for GET requests at the specified path.
*/
get(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("GET", path, middlewares);
this.#commands.push(newHandlerCmd("GET", path, middlewares, false));
return this;
}
/**
* Add middlewares for POST requests at the specified path.
*/
post(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("POST", path, middlewares);
this.#commands.push(newHandlerCmd("POST", path, middlewares, false));
return this;
}
/**
* Add middlewares for PATCH requests at the specified path.
*/
patch(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("PATCH", path, middlewares);
this.#commands.push(newHandlerCmd("PATCH", path, middlewares, false));
return this;
}
/**
* Add middlewares for PUT requests at the specified path.
*/
put(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("PUT", path, middlewares);
this.#commands.push(newHandlerCmd("PUT", path, middlewares, false));
return this;
}
/**
* Add middlewares for DELETE requests at the specified path.
*/
delete(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("DELETE", path, middlewares);
this.#commands.push(newHandlerCmd("DELETE", path, middlewares, false));
return this;
}
/**
* Add middlewares for HEAD requests at the specified path.
*/
head(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("HEAD", path, middlewares);
this.#commands.push(newHandlerCmd("HEAD", path, middlewares, false));
return this;
}
@@ -364,30 +286,22 @@ export class App<State> {
* Add middlewares for all HTTP verbs at the specified path.
*/
all(path: string, ...middlewares: MiddlewareFn<State>[]): this {
this.#addMiddleware("ALL", path, middlewares);
this.#commands.push(newHandlerCmd("ALL", path, middlewares, false));
return this;
}
#addMiddleware(
method: Method | "ALL",
path: string,
fns: MiddlewareFn<State>[],
) {
const segment = getOrCreateSegment<State>(this.#root, path, false);
const result = segmentToMiddlewares(segment);
result.push(...fns);
const resPath = mergePath(this.config.basePath, path);
this.#addRoute(method, resPath, result);
}
#addRoute(
method: Method | "ALL",
path: string,
fns: MiddlewareFn<State>[],
) {
this.#routeDefs.push({ method, pattern: path, fns });
fsRoutes(pattern = "*"): this {
this.#commands.push({
type: CommandType.FsRoute,
pattern,
getItems: () => {
const buildCache = this.#getBuildCache();
if (buildCache === null) return [];
return buildCache.getFsRoutes();
},
includeLastSegment: false,
});
return this;
}
/**
@@ -395,26 +309,25 @@ export class App<State> {
* specified path.
*/
mountApp(path: string, app: App<State>): this {
const segmentPath = mergePath("", path);
const segment = getOrCreateSegment(this.#root, segmentPath, true);
const fns = segmentToMiddlewares(segment);
for (let i = 0; i < app.#commands.length; i++) {
const cmd = app.#commands[i];
segment.middlewares.push(...app.#root.middlewares);
if (cmd.type !== CommandType.App && cmd.type !== CommandType.NotFound) {
const clone = {
...cmd,
pattern: mergePath(path, cmd.pattern),
includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
};
this.#commands.push(clone);
continue;
}
const routes = app.#routeDefs;
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
const merged = mergePath(path, route.pattern);
const mergedFns = [...fns, ...route.fns];
this.#addRoute(route.method, merged, mergedFns);
this.#commands.push(cmd);
}
app.#islandRegistry.forEach((value, key) => {
this.#islandRegistry.set(key, value);
});
app.#root.notFound = this.#root.notFound;
// deno-lint-ignore no-this-alias
const self = this;
app.#getBuildCache = () => self.#getBuildCache();
return this;
}
@@ -427,36 +340,27 @@ export class App<State> {
request: Request,
info?: Deno.ServeHandlerInfo,
) => Promise<Response> {
if (this.#buildCache === null) {
this.#buildCache = ProdBuildCache.fromSnapshot(
this.config,
this.#islandRegistry.size,
);
}
if (
!this.#buildCache.hasSnapshot && this.config.mode === "production" &&
DENO_DEPLOYMENT_ID !== undefined
) {
return missingBuildHandler;
}
for (let i = 0; i < this.#routeDefs.length; i++) {
const route = this.#routeDefs[i];
if (route.method === "ALL") {
this.#router.add("GET", route.pattern, route.fns);
this.#router.add("DELETE", route.pattern, route.fns);
this.#router.add("HEAD", route.pattern, route.fns);
this.#router.add("OPTIONS", route.pattern, route.fns);
this.#router.add("PATCH", route.pattern, route.fns);
this.#router.add("POST", route.pattern, route.fns);
this.#router.add("PUT", route.pattern, route.fns);
let buildCache = this.#getBuildCache();
if (buildCache === null) {
if (
this.config.mode === "production" &&
DENO_DEPLOYMENT_ID !== undefined
) {
throw new Error(
`Could not find _fresh directory. Maybe you forgot to run "deno task build"?`,
);
} else {
this.#router.add(route.method, route.pattern, route.fns);
buildCache = new MockBuildCache([]);
}
}
const rootMiddlewares = segmentToMiddlewares(this.#root);
const router = new UrlPatternRouter<MiddlewareFn<State>>();
const { rootMiddlewares } = applyCommands(
router,
this.#commands,
this.config.basePath,
);
return async (
req: Request,
@@ -467,7 +371,7 @@ export class App<State> {
url.pathname = url.pathname.replace(/\/+/g, "/");
const method = req.method.toUpperCase() as Method;
const matched = this.#router.match(method, url);
const matched = router.match(method, url);
let { params, pattern, handlers, methodMatch } = matched;
const span = trace.getActiveSpan();
@@ -484,7 +388,7 @@ export class App<State> {
if (matched.pattern !== null && !methodMatch) {
if (method === "OPTIONS") {
const allowed = this.#router.getAllowedMethods(matched.pattern);
const allowed = router.getAllowedMethods(matched.pattern);
next = defaultOptionsHandler(allowed);
} else {
next = DEFAULT_NOT_ALLOWED_METHOD;
@@ -501,8 +405,7 @@ export class App<State> {
params,
this.config,
next,
this.#islandRegistry,
this.#buildCache!,
buildCache!,
);
try {
@@ -540,26 +443,3 @@ export class App<State> {
await listenOnFreePort(options, handler);
}
}
// deno-lint-ignore require-await
const missingBuildHandler = async (): Promise<Response> => {
const headers = new Headers();
headers.set("Content-Type", "text/html; charset=utf-8");
const html = DENO_DEPLOYMENT_ID
? renderToString(h(FinishSetup, null))
: renderToString(h(ForgotBuild, null));
return new Response(html, { headers, status: 500 });
};
function ensureHandler<State>(route: Route<State>) {
if (route.handler === undefined) {
route.handler = route.component !== undefined
? DEFAULT_RENDER
: DEFAULT_NOT_FOUND;
} else if (isHandlerByMethod(route.handler)) {
if (route.component !== undefined && !route.handler.GET) {
route.handler.GET = DEFAULT_RENDER;
}
}
}
+22 -57
View File
@@ -1,7 +1,6 @@
import { expect } from "@std/expect";
import { App, getIslandRegistry, setBuildCache } from "./app.ts";
import { App } from "./app.ts";
import { FakeServer } from "./test_utils.ts";
import { ProdBuildCache } from "./build_cache.ts";
import { HttpError } from "./error.ts";
Deno.test("App - .use()", async () => {
@@ -261,6 +260,27 @@ Deno.test("App - .mountApp() compose apps", async () => {
expect(await res.text()).toEqual("A");
});
Deno.test("App - .mountApp() compose apps with .route()", async () => {
const innerApp = new App<{ text: string }>()
.use((ctx) => {
ctx.state.text = "A";
return ctx.next();
})
.route("/", { handler: (ctx) => new Response(ctx.state.text) });
const app = new App<{ text: string }>()
.get("/", () => new Response("ok"))
.mountApp("/foo", innerApp);
const server = new FakeServer(app.handler());
let res = await server.get("/");
expect(await res.text()).toEqual("ok");
res = await server.get("/foo");
expect(await res.text()).toEqual("A");
});
Deno.test("App - .mountApp() self mount, no middleware", async () => {
const innerApp = new App<{ text: string }>()
.use((ctx) => {
@@ -458,30 +478,6 @@ Deno.test("App - catches errors", async () => {
expect(thrownErr).toBeInstanceOf(Error);
});
// TODO: Find a better way to test this
Deno.test.ignore("App - finish setup", async () => {
const app = new App<{ text: string }>()
.get("/", (ctx) => {
return ctx.render(<div>ok</div>);
});
setBuildCache(
app,
await ProdBuildCache.fromSnapshot({
...app.config,
build: {
outDir: "foo",
},
}, getIslandRegistry(app).size),
);
const server = new FakeServer(app.handler());
const res = await server.get("/");
const text = await res.text();
expect(text).toContain("Finish setting up");
expect(res.status).toEqual(500);
});
Deno.test("App - sets error on context", async () => {
const thrown: [unknown, unknown][] = [];
const app = new App()
@@ -545,37 +541,6 @@ Deno.test("App - throw when middleware returns no response", async () => {
expect(text).toContain("Internal server error");
});
Deno.test("App - adding Island should convert to valid export names", () => {
const app = new App();
const islands = getIslandRegistry(app);
const component1 = () => <>OK</>;
const component2 = () => <>OK</>;
const component3 = () => <>OK</>;
app.island("/islands/foo.v2.tsx", "default", component1);
app.island("/islands/_bar-baz-...-$.tsx", "default", component2);
app.island("/islands/1_hello.tsx", "default", component3);
expect(islands.get(component1)!).toEqual({
file: "/islands/foo.v2.tsx",
name: "foo_v2",
exportName: "default",
fn: component1,
});
expect(islands.get(component2)!).toEqual({
file: "/islands/_bar-baz-...-$.tsx",
name: "_bar_baz_$",
exportName: "default",
fn: component2,
});
expect(islands.get(component3)!).toEqual({
file: "/islands/1_hello.tsx",
name: "_hello",
exportName: "default",
fn: component3,
});
});
Deno.test("App - overwrite default 404 handler", async () => {
const app = new App()
.notFound(() => new Response("bar", { status: 404 }))
+55 -92
View File
@@ -1,18 +1,22 @@
import * as path from "@std/path";
import { getSnapshotPath, type ResolvedFreshConfig } from "./config.ts";
import { DENO_DEPLOYMENT_ID, setBuildId } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
import { setBuildId } from "./runtime/build_id.ts";
import type { Command } from "./commands.ts";
import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts";
import type { ServerIslandRegistry } from "./context.ts";
import type { AnyComponent } from "preact";
import { UniqueNamer } from "./utils.ts";
export interface FileSnapshot {
generated: boolean;
name: string;
filePath: string;
hash: string | null;
}
export interface BuildSnapshot {
version: number;
buildId: string;
staticFiles: Record<string, FileSnapshot>;
islands: Record<string, string>;
export interface BuildSnapshot<State> {
version: string;
fsRoutes: FsRouteFile<State>[];
staticFiles: Map<string, FileSnapshot>;
islands: ServerIslandRegistry;
}
export interface StaticFile {
@@ -22,98 +26,35 @@ export interface StaticFile {
close(): void;
}
export interface BuildCache {
hasSnapshot: boolean;
// deno-lint-ignore no-explicit-any
export interface BuildCache<State = any> {
root: string;
islandRegistry: ServerIslandRegistry;
getFsRoutes(): Command<State>[];
readFile(pathname: string): Promise<StaticFile | null>;
getIslandChunkName(islandName: string): string | null;
}
export class ProdBuildCache implements BuildCache {
static fromSnapshot(config: ResolvedFreshConfig, islandCount: number) {
const snapshotPath = getSnapshotPath(config);
export class ProdBuildCache<State> implements BuildCache<State> {
#snapshot: BuildSnapshot<State>;
islandRegistry: ServerIslandRegistry;
const staticFiles = new Map<string, FileSnapshot>();
const islandToChunk = new Map<string, string>();
let hasSnapshot = false;
try {
const content = Deno.readTextFileSync(snapshotPath);
const snapshot = JSON.parse(content) as BuildSnapshot;
hasSnapshot = true;
setBuildId(snapshot.buildId);
const files = Object.keys(snapshot.staticFiles);
for (let i = 0; i < files.length; i++) {
const pathname = files[i];
const info = snapshot.staticFiles[pathname];
staticFiles.set(pathname, info);
}
const islands = Object.keys(snapshot.islands);
for (let i = 0; i < islands.length; i++) {
const pathname = islands[i];
islandToChunk.set(pathname, snapshot.islands[pathname]);
}
if (!DENO_DEPLOYMENT_ID) {
// deno-lint-ignore no-console
console.log(
`Found snapshot at ${colors.cyan(snapshotPath)}`,
);
}
} catch (err) {
if ((err instanceof Deno.errors.NotFound)) {
if (islandCount > 0) {
throw new Error(
`Found ${
colors.green(`${islandCount} islands`)
}, but did not find build snapshot at:\n${
colors.red(snapshotPath)
}.\n\nMaybe your forgot to run ${
colors.cyan("deno task build")
} before starting the production server\nor maybe you wanted to run ${
colors.cyan("deno task dev")
} to spin up a development server instead?\n`,
);
}
} else {
throw err;
}
}
return new ProdBuildCache(config, islandToChunk, staticFiles, hasSnapshot);
constructor(public root: string, snapshot: BuildSnapshot<State>) {
setBuildId(snapshot.version);
this.#snapshot = snapshot;
this.islandRegistry = snapshot.islands;
}
#islands: Map<string, string>;
#fileInfo: Map<string, FileSnapshot>;
#config: ResolvedFreshConfig;
constructor(
config: ResolvedFreshConfig,
islands: Map<string, string>,
files: Map<string, FileSnapshot>,
public hasSnapshot: boolean,
) {
this.#islands = islands;
this.#fileInfo = files;
this.#config = config;
getFsRoutes(): Command<State>[] {
return fsItemsToCommands(this.#snapshot.fsRoutes);
}
async readFile(pathname: string): Promise<StaticFile | null> {
const info = this.#fileInfo.get(pathname);
const { staticFiles } = this.#snapshot;
const info = staticFiles.get(pathname);
if (info === undefined) return null;
const base = info.generated
? this.#config.build.outDir
: this.#config.staticDir;
const filePath = info.generated
? path.join(base, "static", pathname)
: path.join(base, pathname);
// Check if path resolves outside of intended directory.
if (path.relative(base, filePath).startsWith("..")) {
return null;
}
const filePath = path.join(this.root, info.filePath);
const [stat, file] = await Promise.all([
Deno.stat(filePath),
@@ -127,8 +68,30 @@ export class ProdBuildCache implements BuildCache {
close: () => file.close(),
};
}
}
getIslandChunkName(islandName: string): string | null {
return this.#islands.get(islandName) ?? null;
export class IslandPreparer {
#namer = new UniqueNamer();
prepare(
registry: ServerIslandRegistry,
mod: Record<string, unknown>,
chunkName: string,
modName: string,
) {
for (const [name, value] of Object.entries(mod)) {
if (typeof value !== "function") continue;
const islandName = name === "default" ? modName : name;
const uniqueName = this.#namer.getUniqueName(islandName);
const fn = value as AnyComponent;
registry.set(fn, {
exportName: name,
file: chunkName,
fn,
name: uniqueName,
});
}
}
}
-80
View File
@@ -1,80 +0,0 @@
import { expect } from "@std/expect";
import * as path from "@std/path";
import { ProdBuildCache, type StaticFile } from "./build_cache.ts";
import { withTmpDir } from "./test_utils.ts";
import type { ResolvedFreshConfig } from "./mod.ts";
async function getContent(readResult: Promise<StaticFile | null>) {
const res = await readResult;
if (res === null) return null;
if (res.readable instanceof Uint8Array) throw new Error("not implemented");
return new Response(res.readable).text();
}
Deno.test({
name: "ProdBuildCache - should error if reading outside of staticDir",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const config: ResolvedFreshConfig = {
root: tmp,
mode: "production",
basePath: "/",
staticDir: path.join(tmp, "static"),
build: {
outDir: path.join(tmp, "dist"),
},
};
await Deno.mkdir(path.join(tmp, "static", ".well-known"), {
recursive: true,
});
await Deno.mkdir(path.join(tmp, "dist", "static"), {
recursive: true,
});
await Promise.all([
Deno.writeTextFile(
path.join(tmp, "dist", "secret-styles.css"),
"SECRET!",
),
Deno.writeTextFile(path.join(tmp, "SECRETS.txt"), "SECRET!"),
Deno.writeTextFile(path.join(tmp, "dist", "static", "styles.css"), "OK"),
Deno.writeTextFile(
path.join(tmp, "static", ".well-known", "foo.txt"),
"OK",
),
]);
const buildCache = new ProdBuildCache(
config,
new Map(),
new Map([
["../secret-styles.css", { generated: true, hash: "SECRET!" }],
["../SECRETS.txt", { generated: false, hash: "SECRET!" }],
["./../secret-styles.css", { generated: true, hash: "SECRET!" }],
["./../SECRETS.txt", { generated: false, hash: "SECRET!" }],
["styles.css", { generated: true, hash: "OK" }],
[".well-known/foo.txt", { generated: false, hash: "OK" }],
["./styles.css", { generated: true, hash: "OK" }],
["./.well-known/foo.txt", { generated: false, hash: "OK" }],
]),
true,
);
const secret1 = getContent(buildCache.readFile("../styles.css"));
const secret2 = getContent(buildCache.readFile("../SECRETS.txt"));
const secret3 = getContent(buildCache.readFile("./../styles.css"));
const secret4 = getContent(buildCache.readFile("./../SECRETS.txt"));
const public1 = getContent(buildCache.readFile("styles.css"));
const public2 = getContent(buildCache.readFile(".well-known/foo.txt"));
const public3 = getContent(buildCache.readFile("./styles.css"));
const public4 = getContent(buildCache.readFile("./.well-known/foo.txt"));
await expect(secret1).resolves.toBe(null);
await expect(secret2).resolves.toBe(null);
await expect(secret3).resolves.toBe(null);
await expect(secret4).resolves.toBe(null);
await expect(public1).resolves.toBe("OK");
await expect(public2).resolves.toBe("OK");
await expect(public3).resolves.toBe("OK");
await expect(public4).resolves.toBe("OK");
},
});
+309
View File
@@ -0,0 +1,309 @@
import { HttpError } from "./error.ts";
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
import type { MiddlewareFn } from "./middlewares/mod.ts";
import { mergePath, type Method, type Router } from "./router.ts";
import {
getOrCreateSegment,
newSegment,
renderRoute,
type RouteComponent,
type Segment,
segmentToMiddlewares,
} from "./segments.ts";
import type { LayoutConfig, Route } from "./types.ts";
export const DEFAULT_NOT_FOUND = (): Promise<Response> => {
throw new HttpError(404);
};
export const DEFAULT_NOT_ALLOWED_METHOD = (): Promise<Response> => {
throw new HttpError(405);
};
const DEFAULT_RENDER = <State>(): Promise<PageResponse<State>> =>
// deno-lint-ignore no-explicit-any
Promise.resolve({ data: {} as any });
function ensureHandler<State>(route: Route<State>) {
if (route.handler === undefined) {
route.handler = route.component !== undefined
? DEFAULT_RENDER
: DEFAULT_NOT_FOUND;
} else if (isHandlerByMethod(route.handler)) {
if (route.component !== undefined && !route.handler.GET) {
route.handler.GET = DEFAULT_RENDER;
}
}
}
export const enum CommandType {
Middleware = "middleware",
Layout = "layout",
App = "app",
Route = "route",
Error = "error",
NotFound = "notFound",
Handler = "handler",
FsRoute = "fsRoute",
}
export interface ErrorCmd<State> {
type: CommandType.Error;
pattern: string;
item: Route<State>;
includeLastSegment: boolean;
}
export function newErrorCmd<State>(
pattern: string,
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
includeLastSegment: boolean,
): ErrorCmd<State> {
const route = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(route);
return { type: CommandType.Error, pattern, item: route, includeLastSegment };
}
export interface AppCommand<State> {
type: CommandType.App;
component: RouteComponent<State>;
}
export function newAppCmd<State>(
component: RouteComponent<State>,
): AppCommand<State> {
return { type: CommandType.App, component };
}
export interface LayoutCommand<State> {
type: CommandType.Layout;
pattern: string;
component: RouteComponent<State>;
config?: LayoutConfig;
includeLastSegment: boolean;
}
export function newLayoutCmd<State>(
pattern: string,
component: RouteComponent<State>,
config: LayoutConfig | undefined,
includeLastSegment: boolean,
): LayoutCommand<State> {
return {
type: CommandType.Layout,
pattern,
component,
config,
includeLastSegment,
};
}
export interface MiddlewareCmd<State> {
type: CommandType.Middleware;
pattern: string;
fns: MiddlewareFn<State>[];
includeLastSegment: boolean;
}
export function newMiddlewareCmd<State>(
pattern: string,
fns: MiddlewareFn<State>[],
includeLastSegment: boolean,
): MiddlewareCmd<State> {
return { type: CommandType.Middleware, pattern, fns, includeLastSegment };
}
export interface NotFoundCmd<State> {
type: CommandType.NotFound;
fn: MiddlewareFn<State>;
}
export function newNotFoundCmd<State>(
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
): NotFoundCmd<State> {
const route = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(route);
return { type: CommandType.NotFound, fn: (ctx) => renderRoute(ctx, route) };
}
export interface RouteCommand<State> {
type: CommandType.Route;
pattern: string;
route: Route<State>;
includeLastSegment: boolean;
}
export function newRouteCmd<State>(
pattern: string,
route: Route<State>,
includeLastSegment: boolean,
): RouteCommand<State> {
ensureHandler(route);
return { type: CommandType.Route, pattern, route, includeLastSegment };
}
export interface HandlerCommand<State> {
type: CommandType.Handler;
pattern: string;
method: Method | "ALL";
fns: MiddlewareFn<State>[];
includeLastSegment: boolean;
}
export function newHandlerCmd<State>(
method: Method | "ALL",
pattern: string,
fns: MiddlewareFn<State>[],
includeLastSegment: boolean,
): HandlerCommand<State> {
return {
type: CommandType.Handler,
pattern,
method,
fns,
includeLastSegment,
};
}
export interface FsRouteCommand<State> {
type: CommandType.FsRoute;
pattern: string;
getItems: () => Command<State>[];
includeLastSegment: boolean;
}
export type Command<State> =
| ErrorCmd<State>
| AppCommand<State>
| LayoutCommand<State>
| NotFoundCmd<State>
| MiddlewareCmd<State>
| RouteCommand<State>
| HandlerCommand<State>
| FsRouteCommand<State>;
export function applyCommands<State>(
router: Router<MiddlewareFn<State>>,
commands: Command<State>[],
basePath: string,
): { rootMiddlewares: MiddlewareFn<State>[] } {
const root = newSegment<State>("", null);
applyCommandsInner(root, router, commands, basePath);
return { rootMiddlewares: segmentToMiddlewares(root) };
}
function applyCommandsInner<State>(
root: Segment<State>,
router: Router<MiddlewareFn<State>>,
commands: Command<State>[],
basePath: string,
) {
for (let i = 0; i < commands.length; i++) {
const cmd = commands[i];
switch (cmd.type) {
case CommandType.Middleware: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
cmd.includeLastSegment,
);
segment.middlewares.push(...cmd.fns);
break;
}
case CommandType.NotFound: {
root.notFound = cmd.fn;
break;
}
case CommandType.Error: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
cmd.includeLastSegment,
);
segment.errorRoute = cmd.item;
break;
}
case CommandType.App: {
root.app = cmd.component;
break;
}
case CommandType.Layout: {
const segment = getOrCreateSegment(
root,
cmd.pattern,
cmd.includeLastSegment,
);
segment.layout = {
component: cmd.component,
config: cmd.config ?? null,
};
break;
}
case CommandType.Route: {
const { pattern, route } = cmd;
const segment = getOrCreateSegment(
root,
pattern,
cmd.includeLastSegment,
);
const fns = segmentToMiddlewares(segment);
fns.push((ctx) => renderRoute(ctx, route));
const routePath = mergePath(
basePath,
route.config?.routeOverride ?? pattern,
);
if (typeof route.handler === "function") {
router.add("GET", routePath, fns);
router.add("DELETE", routePath, fns);
router.add("HEAD", routePath, fns);
router.add("OPTIONS", routePath, fns);
router.add("PATCH", routePath, fns);
router.add("POST", routePath, fns);
router.add("PUT", routePath, fns);
} else if (isHandlerByMethod(route.handler!)) {
for (const method of Object.keys(route.handler)) {
router.add(method as Method, routePath, fns);
}
}
break;
}
case CommandType.Handler: {
const { pattern, fns, method } = cmd;
const segment = getOrCreateSegment(
root,
pattern,
cmd.includeLastSegment,
);
const result = segmentToMiddlewares(segment);
result.push(...fns);
const resPath = mergePath(basePath, pattern);
if (method === "ALL") {
router.add("GET", resPath, result);
router.add("DELETE", resPath, result);
router.add("HEAD", resPath, result);
router.add("OPTIONS", resPath, result);
router.add("PATCH", resPath, result);
router.add("POST", resPath, result);
router.add("PUT", resPath, result);
} else {
router.add(method, resPath, result);
}
break;
}
case CommandType.FsRoute: {
const items = cmd.getItems();
applyCommandsInner(root, router, items, basePath);
break;
}
default:
throw new Error(`Unknown command: ${JSON.stringify(cmd)}`);
}
}
}
+1 -64
View File
@@ -1,38 +1,12 @@
import * as path from "@std/path";
export interface FreshConfig {
/**
* The root directory of the Fresh project.
*
* Other paths, such as `build.outDir`, `staticDir`, and `fsRoutes()`
* are resolved relative to this directory.
* @default Deno.cwd()
*/
root?: string;
build?: {
/**
* The directory to write generated files to when `dev.ts build` is run.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "_fresh"
*/
outDir?: string;
};
/**
* Serve fresh from a base path instead of from the root.
* "/foo/bar" -> http://localhost:8000/foo/bar
* @default undefined
*/
basePath?: string;
/**
* The directory to serve static files from.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "static"
*/
staticDir?: string;
}
/**
@@ -40,29 +14,20 @@ export interface FreshConfig {
*/
export interface ResolvedFreshConfig {
root: string;
build: {
outDir: string;
};
/**
* Serve fresh from a base path instead of from the root.
* "/foo/bar" -> http://localhost:8000/foo/bar
*/
basePath: string;
staticDir: string;
/**
* The mode Fresh can run in.
*/
mode: "development" | "production";
}
export function parseRootPath(root: string, cwd: string): string {
return parseDirPath(root, cwd, true);
}
function parseDirPath(
export function parseDirPath(
dirPath: string,
root: string,
fileToDir = false,
): string {
if (dirPath.startsWith("file://")) {
dirPath = path.fromFileUrl(dirPath);
@@ -70,37 +35,9 @@ function parseDirPath(
dirPath = path.join(root, dirPath);
}
if (fileToDir) {
const ext = path.extname(dirPath);
if (
ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx" ||
ext === ".mjs"
) {
dirPath = path.dirname(dirPath);
}
}
if (Deno.build.os === "windows") {
dirPath = dirPath.replaceAll("\\", "/");
}
return dirPath;
}
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
const root = parseRootPath(options.root ?? ".", Deno.cwd());
return {
root,
build: {
outDir: parseDirPath(options.build?.outDir ?? "_fresh", root),
},
basePath: options.basePath ?? "",
staticDir: parseDirPath(options.staticDir ?? "static", root),
mode: "production",
};
}
export function getSnapshotPath(config: ResolvedFreshConfig): string {
return path.join(config.build.outDir, "snapshot.json");
}
+7 -94
View File
@@ -1,108 +1,21 @@
import { expect } from "@std/expect";
import { normalizeConfig, parseRootPath } from "./config.ts";
import type { FreshConfig } from "./mod.ts";
import { parseDirPath } from "./config.ts";
Deno.test("parseRootPath", () => {
Deno.test("parseDirPath", () => {
const cwd = Deno.cwd().replaceAll("\\", "/");
// File paths
expect(parseRootPath("file:///foo/bar", cwd)).toEqual("/foo/bar");
expect(parseRootPath("file:///foo/bar.ts", cwd)).toEqual("/foo");
expect(parseDirPath("file:///foo/bar", cwd)).toEqual("/foo/bar");
if (Deno.build.os === "windows") {
expect(parseRootPath("file:///C:/foo/bar", cwd)).toEqual("C:/foo/bar");
expect(parseRootPath("file:///C:/foo/bar.ts", cwd)).toEqual("C:/foo");
expect(parseDirPath("file:///C:/foo/bar", cwd)).toEqual("C:/foo/bar");
}
// Relative paths
expect(parseRootPath("./foo/bar", cwd)).toEqual(`${cwd}/foo/bar`);
expect(parseRootPath("./foo/bar.ts", cwd)).toEqual(`${cwd}/foo`);
expect(parseDirPath("./foo/bar", cwd)).toEqual(`${cwd}/foo/bar`);
// Absolute paths
expect(parseRootPath("/foo/bar", cwd)).toEqual("/foo/bar");
expect(parseRootPath("/foo/bar.ts", cwd)).toEqual("/foo");
expect(parseRootPath("/foo/bar.tsx", cwd)).toEqual("/foo");
expect(parseRootPath("/foo/bar.js", cwd)).toEqual("/foo");
expect(parseRootPath("/foo/bar.jsx", cwd)).toEqual("/foo");
expect(parseRootPath("/foo/bar.mjs", cwd)).toEqual("/foo");
expect(parseDirPath("/foo/bar", cwd)).toEqual("/foo/bar");
if (Deno.build.os === "windows") {
expect(parseRootPath("C:/foo/bar", cwd)).toEqual("C:/foo/bar");
expect(parseRootPath("C:/foo/bar.ts", cwd)).toEqual("C:/foo");
expect(parseDirPath("C:/foo/bar", cwd)).toEqual("C:/foo/bar");
}
});
Deno.test("normalizeConfig - root", () => {
const cwd = Deno.cwd().replaceAll("\\", "/");
const configRoot = (root?: string) => normalizeConfig({ root }).root;
expect(configRoot()).toEqual(cwd);
expect(configRoot("/foo/bar")).toEqual("/foo/bar");
expect(configRoot("/foo/bar.ts")).toEqual("/foo");
expect(configRoot("file:///foo/bar")).toEqual("/foo/bar");
expect(configRoot("./foo/bar")).toEqual(`${cwd}/foo/bar`);
expect(configRoot("./foo/bar.ts")).toEqual(`${cwd}/foo`);
if (Deno.build.os === "windows") {
expect(configRoot("C:/foo/bar.ts")).toEqual("C:/foo");
expect(configRoot("file:///C:/foo/bar")).toEqual("C:/foo/bar");
}
});
Deno.test("normalizeConfig - build.outDir", () => {
const cwd = Deno.cwd().replaceAll("\\", "/");
const outDir = (options: FreshConfig) =>
normalizeConfig(options).build.outDir;
// Default outDir
expect(outDir({ root: "./src" })).toEqual(`${cwd}/src/_fresh`);
expect(outDir({ root: "/src" })).toEqual("/src/_fresh");
expect(outDir({ root: "file:///src" })).toEqual("/src/_fresh");
// Relative outDir
expect(outDir({ root: "/src", build: { outDir: "dist" } })).toEqual(
"/src/dist",
);
expect(outDir({ root: "/src", build: { outDir: "./dist" } })).toEqual(
"/src/dist",
);
// Absolute outDir
expect(outDir({ root: "/src", build: { outDir: "/dist" } })).toEqual(
"/dist",
);
expect(outDir({ root: "/src", build: { outDir: "/dist/fresh" } })).toEqual(
"/dist/fresh",
);
expect(outDir({ root: "/src", build: { outDir: "file:///dist" } })).toEqual(
"/dist",
);
});
Deno.test("normalizeConfig - staticDir", () => {
const cwd = Deno.cwd().replaceAll("\\", "/");
const staticDir = (options: FreshConfig) =>
normalizeConfig(options).staticDir;
// Default staticDir
expect(staticDir({ root: "./src" })).toEqual(`${cwd}/src/static`);
expect(staticDir({ root: "/src" })).toEqual("/src/static");
expect(staticDir({ root: "file:///src" })).toEqual("/src/static");
// Relative staticDir
expect(staticDir({ root: "/src", staticDir: "public" })).toEqual(
"/src/public",
);
expect(staticDir({ root: "/src", staticDir: "./public" })).toEqual(
"/src/public",
);
// Absolute staticDir
expect(staticDir({ root: "/src", staticDir: "/public" })).toEqual(
"/public",
);
expect(staticDir({ root: "/src", staticDir: "/public/assets" })).toEqual(
"/public/assets",
);
expect(staticDir({ root: "/src", staticDir: "file:///public" })).toEqual(
"/public",
);
});
+6 -11
View File
@@ -23,7 +23,7 @@ import {
} from "./render.ts";
export interface Island {
file: string | URL;
file: string;
name: string;
exportName: string;
fn: ComponentType;
@@ -43,7 +43,7 @@ export interface UiTree<Data, State> {
*/
export type FreshContext<State = unknown> = Context<State>;
export let getBuildCache: <T>(ctx: Context<T>) => BuildCache;
export let getBuildCache: <T>(ctx: Context<T>) => BuildCache<T>;
export let getInternals: <T>(ctx: Context<T>) => UiTree<unknown, T>;
/**
@@ -108,16 +108,15 @@ export class Context<State> {
*/
next: () => Promise<Response>;
#islandRegistry: ServerIslandRegistry;
#buildCache: BuildCache;
#buildCache: BuildCache<State>;
// FIXME: remove after switching to <Slot />
Component!: FunctionComponent;
static {
// deno-lint-ignore no-explicit-any
getInternals = (ctx) => (ctx as Context<unknown>).#internal as any;
getBuildCache = (ctx) => (ctx as Context<unknown>).#buildCache;
getInternals = <T>(ctx: Context<T>) => ctx.#internal as any;
getBuildCache = <T>(ctx: Context<T>) => ctx.#buildCache;
}
constructor(
@@ -128,8 +127,7 @@ export class Context<State> {
params: Record<string, string>,
config: ResolvedFreshConfig,
next: () => Promise<Response>,
islandRegistry: ServerIslandRegistry,
buildCache: BuildCache,
buildCache: BuildCache<State>,
) {
this.url = url;
this.req = req;
@@ -139,7 +137,6 @@ export class Context<State> {
this.config = config;
this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM);
this.next = next;
this.#islandRegistry = islandRegistry;
this.#buildCache = buildCache;
}
@@ -265,7 +262,6 @@ export class Context<State> {
span.setAttribute("fresh.span_type", "render");
const state = new RenderState(
this,
this.#islandRegistry,
this.#buildCache,
partialId,
);
@@ -277,7 +273,6 @@ export class Context<State> {
vnode ?? h(Fragment, null),
this,
state,
this.#buildCache,
headers,
);
} catch (err) {
+271 -143
View File
@@ -1,29 +1,31 @@
import {
App,
getBuildCache,
getIslandRegistry,
type ListenOptions,
setBuildCache,
} from "../app.ts";
import { App, type ListenOptions, setBuildCache } from "../app.ts";
import { fsAdapter } from "../fs.ts";
import * as path from "@std/path";
import * as colors from "@std/fmt/colors";
import { bundleJs } from "./esbuild.ts";
import * as JSONC from "@std/jsonc";
import { liveReload } from "./middlewares/live_reload.ts";
import {
cssAssetHash,
FreshFileTransformer,
FileTransformer,
type OnTransformOptions,
} from "./file_transformer.ts";
import type { TransformFn } from "./file_transformer.ts";
import { DiskBuildCache, MemoryBuildCache } from "./dev_build_cache.ts";
import type { Island } from "../context.ts";
import {
type DevBuildCache,
DiskBuildCache,
type FsRoute,
MemoryBuildCache,
} from "./dev_build_cache.ts";
import { BUILD_ID } from "../runtime/build_id.ts";
import { updateCheck } from "./update_check.ts";
import { DAY } from "@std/datetime";
import { devErrorOverlay } from "./middlewares/error_overlay/middleware.tsx";
import { automaticWorkspaceFolders } from "./middlewares/automatic_workspace_folders.ts";
import { parseDirPath } from "../config.ts";
import { pathToExportName, UniqueNamer } from "../utils.ts";
import { checkDenoCompilerOptions } from "./check.ts";
import { crawlRouteDir, walkDir } from "./fs_crawl.ts";
export interface BuildOptions {
/**
@@ -33,20 +35,99 @@ export interface BuildOptions {
* @default {"es2022"}
*/
target?: string | string[];
/**
* The root directory of the Fresh project.
*
* Other paths, such as `build.outDir`, `staticDir`, and `fsRoutes()`
* are resolved relative to this directory.
* @default Deno.cwd()
*/
root?: string;
/**
* The directory to write generated files to when `dev.ts build` is run.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "_fresh"
*/
outDir?: string;
/**
* The directory to serve static files from.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "static"
*/
staticDir?: string;
/**
* The directory which contains islands.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "islands"
*/
islandDir?: string;
/**
* The directory which contains routes.
*
* This can be an absolute path, a file URL or a relative path.
* Relative paths are resolved against the `root` option.
* @default "routes"
*/
routeDir?: string;
/**
* File paths which should be ignored when crawling the file system.
*/
ignore?: RegExp[];
}
export class Builder {
#transformer = new FreshFileTransformer(fsAdapter);
/**
* The final resolved Builder configuration.
*/
export type ResolvedBuildConfig = Required<BuildOptions> & {
mode: "development" | "production";
buildId: string;
};
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
// deno-lint-ignore no-explicit-any
export class Builder<State = any> {
#transformer: FileTransformer;
#addedInternalTransforms = false;
#options: Required<BuildOptions>;
#chunksReady = Promise.withResolvers<void>();
config: ResolvedBuildConfig;
#islandSpecifiers = new Set<string>();
#fsRoutes: FsRoute<State>;
#ready = Promise.withResolvers<void>();
constructor(options?: BuildOptions) {
this.#options = {
const root = parseDirPath(options?.root ?? ".", Deno.cwd());
const outDir = parseDirPath(options?.outDir ?? "_fresh", root);
const staticDir = parseDirPath(options?.staticDir ?? "static", root);
const islandDir = parseDirPath(options?.islandDir ?? "islands", root);
const routeDir = parseDirPath(options?.routeDir ?? "routes", root);
this.#fsRoutes = { dir: routeDir, files: [], id: "default" };
this.#transformer = new FileTransformer(fsAdapter, root);
this.config = {
target: options?.target ?? ["chrome99", "firefox99", "safari15"],
root,
outDir,
staticDir,
islandDir,
routeDir,
ignore: options?.ignore ?? [TEST_FILE_PATTERN],
mode: "production",
buildId: BUILD_ID,
};
}
registerIsland(specifier: string): void {
this.#islandSpecifiers.add(specifier);
}
onTransformStaticFile(
options: OnTransformOptions,
callback: TransformFn,
@@ -54,75 +135,144 @@ export class Builder {
this.#transformer.onTransform(options, callback);
}
async listen<T>(app: App<T>, options: ListenOptions = {}): Promise<void> {
async listen(
importApp: () => Promise<{ app: App<State> } | App<State>>,
options: ListenOptions = {},
): Promise<void> {
// Run update check in background
updateCheck(DAY).catch(() => {});
const devApp = new App<T>(app.config)
this.config.mode = "development";
await this.#crawlFsItems();
let app = await importApp();
if (!(app instanceof App) && "app" in app) {
app = app.app;
}
const buildCache = new MemoryBuildCache<State>(
this.config,
this.#fsRoutes,
this.#transformer,
);
await buildCache.prepare();
const devApp = new App<State>(app.config)
.use(liveReload())
.use(devErrorOverlay())
.use(automaticWorkspaceFolders(app.config.root))
// Wait for island chunks to be ready before attempting to serve them
.use(automaticWorkspaceFolders(this.config.root))
// Wait for islands to be ready
.use(async (ctx) => {
await this.#chunksReady.promise;
return await ctx.next();
await this.#ready.promise;
return ctx.next();
})
.mountApp("/*", app);
devApp.config.root = this.config.root;
devApp.config.mode = "development";
setBuildCache(
devApp,
new MemoryBuildCache(
devApp.config,
BUILD_ID,
this.#transformer,
this.#options.target,
),
);
setBuildCache(devApp, buildCache);
// Boot in parallel to spin up the server quicker. We'll hold
// requests until the required assets are processed.
await Promise.all([
devApp.listen(options),
this.#build(devApp, true),
this.#build(buildCache, true),
]);
return;
}
async build<T>(app: App<T>): Promise<void> {
setBuildCache(
app,
new DiskBuildCache(
app.config,
BUILD_ID,
this.#transformer,
this.#options.target,
),
);
/**
* Build optimized assets for your app. By default this will create
* a production build.
*
* This can also be used for testing to apply a snapshot to a particular
* {@linkcode App} instance.
*
* @example
* ```ts
* const builder = new Builder();
* const applySnapshot = await builder.build({ snapshot: "memory" });
*
* Deno.test("My Test", () => {
* const app = new App()
* .get("/", () => new Response("hello"))
*
* applySnapshot(app)
*
* // ... your usual testing
* })
* ```
* @param options
* @returns Apply a snapshot to a particular {@linkcode App} instance.
*/
async build(
options?: {
mode?: ResolvedBuildConfig["mode"];
snapshot?: "disk" | "memory";
},
): Promise<(app: App<State>) => void> {
this.config.mode = options?.mode ?? "production";
return await this.#build(app, false);
await this.#crawlFsItems();
const buildCache = options?.snapshot === "memory"
? new MemoryBuildCache(
this.config,
this.#fsRoutes,
this.#transformer,
)
: new DiskBuildCache(
this.config,
this.#fsRoutes,
this.#transformer,
);
await this.#build(buildCache, this.config.mode === "development");
await buildCache.prepare();
return (app) => {
setBuildCache(app, buildCache);
};
}
async #build<T>(app: App<T>, dev: boolean): Promise<void> {
const { build } = app.config;
const staticOutDir = path.join(build.outDir, "static");
async #crawlFsItems() {
await Promise.all([
walkDir(
fsAdapter,
this.config.islandDir,
(entry) => this.registerIsland(entry.path),
this.config.ignore,
),
crawlRouteDir(
fsAdapter,
this.config.routeDir,
this.config.ignore,
(entry) => this.registerIsland(entry),
this.#fsRoutes.files,
),
]);
}
async #build<T>(buildCache: DevBuildCache<T>, dev: boolean): Promise<void> {
const { target, outDir, root } = this.config;
const staticOutDir = path.join(outDir, "static");
const { denoJson, jsxImportSource } = await checkDenoCompilerOptions(root);
if (!this.#addedInternalTransforms) {
this.#addedInternalTransforms = true;
cssAssetHash(this.#transformer);
}
const target = this.#options.target;
try {
await Deno.remove(staticOutDir);
} catch {
// Ignore
}
const buildCache = getBuildCache(app)! as
| MemoryBuildCache
| DiskBuildCache;
const runtimePath = dev
? "../runtime/client/dev.ts"
: "../runtime/client/mod.tsx";
@@ -130,130 +280,108 @@ export class Builder {
const entryPoints: Record<string, string> = {
"fresh-runtime": new URL(runtimePath, import.meta.url).href,
};
const seenEntries = new Map<string, Island>();
const mapIslandToEntry = new Map<Island, string>();
const islandRegistry = getIslandRegistry(app);
for (const island of islandRegistry.values()) {
const filePath = island.file instanceof URL
? island.file.href
: island.file;
const seen = seenEntries.get(filePath);
if (seen !== undefined) {
mapIslandToEntry.set(island, seen.name);
} else {
entryPoints[island.name] = filePath;
seenEntries.set(filePath, island);
mapIslandToEntry.set(island, island.name);
}
}
const namer = new UniqueNamer();
for (const spec of this.#islandSpecifiers) {
const specName = specToName(spec);
const name = namer.getUniqueName(specName);
const denoJson = await findNearestDenoConfigWithCompilerOptions(
app.config.root,
);
entryPoints[name] = spec;
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
if (jsxImportSource === undefined) {
throw new Error(
`Option compilerOptions > jsxImportSource not set in: ${denoJson.filePath}`,
);
}
// Check precompile option
if (denoJson.config.compilerOptions?.jsx === "precompile") {
const expected = ["a", "img", "source", "body", "html", "head"];
const skipped = denoJson.config.compilerOptions.jsxPrecompileSkipElements;
if (!skipped || expected.some((name) => !skipped.includes(name))) {
throw new Error(
`Expected option compilerOptions > jsxPrecompileSkipElements to contain ${
expected.map((name) => `"${name}"`).join(", ")
}`,
);
}
buildCache.islandModNameToChunk.set(name, {
name,
server: spec,
browser: null,
});
}
const output = await bundleJs({
cwd: app.config.root,
cwd: root,
outDir: staticOutDir,
dev: dev ?? false,
target,
buildId: BUILD_ID,
entryPoints,
jsxImportSource,
denoJsonPath: denoJson.filePath,
denoJsonPath: denoJson,
});
const prefix = `/_fresh/js/${BUILD_ID}/`;
for (const name of buildCache.islandModNameToChunk.keys()) {
const chunkName = output.entryToChunk.get(name);
if (chunkName === undefined) {
throw new Error(`Could not find chunk for island ${name}`);
}
const pathname = `${prefix}${chunkName}`;
buildCache.islandModNameToChunk.get(name)!.browser = pathname;
}
for (let i = 0; i < output.files.length; i++) {
const file = output.files[i];
const pathname = `${prefix}${file.path}`;
await buildCache.addProcessedFile(pathname, file.contents, file.hash);
}
// Go through same entry islands
for (const [island, entry] of mapIslandToEntry.entries()) {
const chunk = output.entryToChunk.get(entry);
if (chunk === undefined) {
throw new Error(
`Missing chunk for ${island.file}#${island.exportName}`,
);
}
buildCache.islands.set(island.name, `${prefix}${chunk}`);
}
await buildCache.flush();
this.#chunksReady.resolve();
if (!dev) {
// deno-lint-ignore no-console
console.log(
`Assets written to: ${colors.cyan(build.outDir)}`,
`Assets written to: ${colors.cyan(outDir)}`,
);
}
this.#ready.resolve();
}
}
export interface DenoConfig {
workspace?: string[];
compilerOptions?: {
jsx?: string;
jsxImportSource?: string;
jsxPrecompileSkipElements?: string[];
};
}
export async function findNearestDenoConfigWithCompilerOptions(
directory: string,
): Promise<{ config: DenoConfig; filePath: string }> {
let dir = directory;
while (true) {
for (const name of ["deno.json", "deno.jsonc"]) {
const filePath = path.join(dir, name);
try {
const file = await Deno.readTextFile(filePath);
let config;
if (name.endsWith(".jsonc")) {
config = JSONC.parse(file);
} else {
config = JSON.parse(file);
}
if (config.compilerOptions) return { config, filePath };
if (config.workspace) break;
break;
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
export function specToName(spec: string): string {
if (/^(https?:|file:)/.test(spec)) {
const url = new URL(spec);
if (url.pathname === "/") {
return pathToExportName(url.hostname);
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
const idx = spec.lastIndexOf("/");
return spec.slice(idx + 1);
} else if (spec.startsWith("jsr:")) {
const match = spec.match(
/jsr:@([^/]+)\/([^@/]+)(@[\^~]?\d+\.\d+\.\d+([^/]+)?)?(\/.*)?$/,
)!;
if (match[5] === undefined) {
return pathToExportName(`${match[1]}_${match[2]}`);
}
return pathToExportName(match[5]);
} else if (spec.startsWith("npm:")) {
const match = spec.match(
/npm:(@([^/]+)\/([^@/]+)|[^@/]+)(@[\^~]?\d+\.\d+\.\d+([^/]+)?)?(\/.*)?$/,
)!;
if (match[6] === undefined) {
if (match[2] === undefined) {
return pathToExportName(match[1]);
}
return pathToExportName(`${match[2]}_${match[3]}`);
}
return pathToExportName(match[6]);
}
throw new Error(
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory that contains a 'compilerOptions' field.`,
);
const match = spec.match(/^(@([^/]+)\/([^@/]+)|[^@/]+)(\/.*)?$/);
if (match !== null) {
if (match[4] === undefined) {
if (match[2] !== undefined) {
return pathToExportName(`${match[2]}_${match[3]}`);
}
return pathToExportName(match[1]);
}
return pathToExportName(match[4]);
}
return pathToExportName(spec);
}
+101 -65
View File
@@ -1,16 +1,21 @@
import { expect } from "@std/expect";
import * as path from "@std/path";
import { Builder } from "./builder.ts";
import { Builder, specToName } from "./builder.ts";
import { App } from "../app.ts";
import { RemoteIsland } from "@marvinh-test/fresh-island";
import { BUILD_ID } from "../runtime/build_id.ts";
import { withTmpDir } from "../test_utils.ts";
Deno.test({
name: "Builder - chain onTransformStaticFile",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const logs: string[] = [];
const builder = new Builder();
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
builder.onTransformStaticFile(
{ pluginName: "A", filter: /\.css$/ },
() => {
@@ -30,16 +35,8 @@ Deno.test({
},
);
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
expect(logs).toEqual(["A", "B", "C"]);
},
@@ -50,24 +47,22 @@ Deno.test({
Deno.test({
name: "Builder - handles Windows paths",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.mkdir(path.join(tmp, "images"));
await Deno.writeTextFile(
path.join(tmp, "images", "batman.svg"),
"<svg></svg>",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const snapshotJson = await Deno.readTextFile(
path.join(tmp, "dist", "snapshot.json"),
path.join(tmp, "dist", "static-files.json"),
);
expect(snapshotJson).toContain("/images/batman.svg");
},
@@ -78,20 +73,18 @@ Deno.test({
Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
"body { background: url('/foo.jpg'); }",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const css = await Deno.readTextFile(
path.join(tmp, "dist", "static", "foo.css"),
@@ -106,20 +99,17 @@ Deno.test({
Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
`:root { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='rgb(76, 154.5, 137.5)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E"); }`,
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
const css = await Deno.readTextFile(
path.join(tmp, "dist", "static", "foo.css"),
@@ -135,20 +125,18 @@ Deno.test({
Deno.test({
name: "Builder - can bundle islands from JSR",
fn: async () => {
const builder = new Builder();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
app.island("jsr:@marvinh-test/fresh-island", "RemoteIsland", RemoteIsland);
const outDir = path.join(tmp, "dist");
const builder = new Builder({ outDir });
await builder.build(app);
const specifier = "jsr:@marvinh-test/fresh-island";
builder.registerIsland(specifier);
await builder.build();
const name = specToName(specifier);
const code = await Deno.readTextFile(
path.join(
tmp,
@@ -157,7 +145,7 @@ Deno.test({
"_fresh",
"js",
BUILD_ID,
"RemoteIsland.js",
`${name}.js`,
),
);
expect(code).toContain('"remote-island"');
@@ -169,8 +157,14 @@ Deno.test({
Deno.test({
name: "Builder - exclude files",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const logs: string[] = [];
const builder = new Builder();
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
});
// String
builder.onTransformStaticFile(
@@ -196,20 +190,12 @@ Deno.test({
},
);
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
await Deno.writeTextFile(
path.join(tmp, "bar.css"),
"body { color: blue; }",
);
const app = new App({
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
await builder.build(app);
await builder.build();
expect(logs).toEqual(["A: bar.css", "B: bar.css", "C: bar.css"]);
},
@@ -220,17 +206,20 @@ Deno.test({
Deno.test({
name: "Builder - workspace folder middleware on listen",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
const app = new App({
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const builder = new Builder({
outDir: path.join(tmp, "dist"),
staticDir: tmp,
build: {
outDir: path.join(tmp, "dist"),
},
});
const app = new App();
const abort = new AbortController();
const port = 8011;
await builder.listen(app, { port, signal: abort.signal });
await builder.listen(() => Promise.resolve(app), {
port,
signal: abort.signal,
});
const res = await fetch(
`http://localhost:${port}/.well-known/appspecific/com.chrome.devtools.json`,
@@ -243,7 +232,7 @@ Deno.test({
expect(res.headers.get("etag")).toEqual(expect.any(String));
expect(json).toEqual({
workspace: {
root: app.config.root,
root: builder.config.root,
uuid: expect.any(String),
},
});
@@ -251,3 +240,50 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test("specToName", () => {
// HTTP
expect(specToName("http://example.com")).toEqual("example");
expect(specToName("http://example.com:8000")).toEqual("example");
expect(specToName("http://example.com:8000/foo/bar")).toEqual(
"bar",
);
// HTTPS
expect(specToName("https://example.com")).toEqual("example");
expect(specToName("https://example.com:8000")).toEqual("example");
expect(specToName("https://example.com:8000/foo/bar")).toEqual(
"bar",
);
// JSR
expect(specToName("jsr:@foo/bar")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@^1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0-alpha.32")).toEqual("foo_bar");
expect(specToName("jsr:@foo/bar@~1.0.0-alpha.32/asdf")).toEqual("asdf");
expect(specToName("jsr:@foo/bar/asdf")).toEqual("asdf");
// npm
expect(specToName("npm:foo")).toEqual("foo");
expect(specToName("npm:foo/bar")).toEqual("bar");
expect(specToName("npm:foo@1.0.0")).toEqual("foo");
expect(specToName("npm:foo@^1.0.0")).toEqual("foo");
expect(specToName("npm:foo@~1.0.0-alpha.32")).toEqual("foo");
expect(specToName("npm:@foo/bar")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar/asdf")).toEqual("asdf");
expect(specToName("npm:@foo/bar@1.0.0")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar@^1.0.0")).toEqual("foo_bar");
expect(specToName("npm:@foo/bar@~1.0.0-alpha.32")).toEqual("foo_bar");
// other
expect(specToName("foo")).toEqual("foo");
expect(specToName("@foo/bar")).toEqual("foo_bar");
expect(specToName("foo/bar")).toEqual("bar");
expect(specToName("@foo/bar/asdf")).toEqual("asdf");
expect(specToName("islands/foo.v2.tsx")).toEqual("foo_v2");
expect(specToName("/islands/_bar-baz-...-$.tsx")).toEqual("_bar_baz_$");
expect(specToName("/islands/1_hello.tsx")).toEqual("_hello");
});
+73
View File
@@ -0,0 +1,73 @@
import * as JSONC from "@std/jsonc";
import * as path from "@std/path";
export interface DenoConfig {
workspace?: string[];
compilerOptions?: {
jsx?: string;
jsxImportSource?: string;
jsxPrecompileSkipElements?: string[];
};
}
export async function checkDenoCompilerOptions(root: string) {
const denoJson = await findNearestDenoConfigWithCompilerOptions(
root,
);
const jsxImportSource = denoJson.config.compilerOptions?.jsxImportSource;
if (jsxImportSource === undefined) {
throw new Error(
`Option compilerOptions > jsxImportSource not set in: ${denoJson.filePath}`,
);
}
// Check precompile option
if (denoJson.config.compilerOptions?.jsx === "precompile") {
const expected = ["a", "img", "source", "body", "html", "head"];
const skipped = denoJson.config.compilerOptions.jsxPrecompileSkipElements;
if (!skipped || expected.some((name) => !skipped.includes(name))) {
throw new Error(
`Expected option compilerOptions > jsxPrecompileSkipElements to contain ${
expected.map((name) => `"${name}"`).join(", ")
}`,
);
}
}
return { jsxImportSource, denoJson: denoJson.filePath };
}
export async function findNearestDenoConfigWithCompilerOptions(
directory: string,
): Promise<{ config: DenoConfig; filePath: string }> {
let dir = directory;
while (true) {
for (const name of ["deno.json", "deno.jsonc"]) {
const filePath = path.join(dir, name);
try {
const file = await Deno.readTextFile(filePath);
let config;
if (name.endsWith(".jsonc")) {
config = JSONC.parse(file);
} else {
config = JSON.parse(file);
}
if (config.compilerOptions) return { config, filePath };
if (config.workspace) break;
break;
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
throw new Error(
`Could not find a deno.json or deno.jsonc file in the current directory or any parent directory that contains a 'compilerOptions' field.`,
);
}
+221 -76
View File
@@ -1,50 +1,79 @@
import type { BuildCache, StaticFile } from "../build_cache.ts";
import {
type BuildCache,
type FileSnapshot,
IslandPreparer,
type StaticFile,
} from "../build_cache.ts";
import * as path from "@std/path";
import { SEPARATOR as WINDOWS_SEPARATOR } from "@std/path/windows/constants";
import { getSnapshotPath, type ResolvedFreshConfig } from "../config.ts";
import type { BuildSnapshot } from "../build_cache.ts";
import { encodeHex } from "@std/encoding/hex";
import { crypto } from "@std/crypto";
import { fsAdapter } from "../fs.ts";
import type { FreshFileTransformer } from "./file_transformer.ts";
import { assertInDir } from "../utils.ts";
import type { FileTransformer } from "./file_transformer.ts";
import { assertInDir, pathToSpec } from "../utils.ts";
import type { ResolvedBuildConfig } from "./builder.ts";
import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts";
import type { Command } from "../commands.ts";
import type { ServerIslandRegistry } from "../context.ts";
export interface MemoryFile {
hash: string | null;
content: Uint8Array;
}
export interface DevBuildCache extends BuildCache {
islands: Map<string, string>;
export interface IslandModChunk {
name: string;
server: string;
browser: string | null;
}
addUnprocessedFile(pathname: string): void;
export type FsRouteFileNoMod<State> = Omit<FsRouteFile<State>, "mod">;
export interface FsRoute<State> {
id: string;
dir: string;
files: FsRouteFileNoMod<State>[];
}
export interface DevBuildCache<State> extends BuildCache<State> {
islandModNameToChunk: Map<string, IslandModChunk>;
addUnprocessedFile(pathname: string, dir: string): void;
addProcessedFile(
pathname: string,
content: Uint8Array,
hash: string | null,
): Promise<void>;
flush(): Promise<void>;
prepare(): Promise<void>;
}
export class MemoryBuildCache implements DevBuildCache {
hasSnapshot = true;
islands = new Map<string, string>();
export class MemoryBuildCache<State> implements DevBuildCache<State> {
#processedFiles = new Map<string, MemoryFile>();
#unprocessedFiles = new Map<string, string>();
#ready = Promise.withResolvers<void>();
#config: ResolvedBuildConfig;
#transformer: FileTransformer;
islandModNameToChunk = new Map<string, IslandModChunk>();
#fsRoutes: FsRoute<State>;
#commands: Command<State>[] = [];
root: string;
islandRegistry: ServerIslandRegistry = new Map();
constructor(
public config: ResolvedFreshConfig,
public buildId: string,
public transformer: FreshFileTransformer,
public target: string | string[],
config: ResolvedBuildConfig,
fsRoutes: FsRoute<State>,
transformer: FileTransformer,
) {
this.#config = config;
this.#fsRoutes = fsRoutes;
this.#transformer = transformer;
this.root = config.root;
}
getFsRoutes(): Command<State>[] {
return this.#commands;
}
async readFile(pathname: string): Promise<StaticFile | null> {
await this.#ready.promise;
const processed = this.#processedFiles.get(pathname);
if (processed !== undefined) {
return {
@@ -69,14 +98,14 @@ export class MemoryBuildCache implements DevBuildCache {
readable: file.readable,
close: () => file.close(),
};
} catch (_err) {
} catch {
return null;
}
}
let entry = pathname.startsWith("/") ? pathname.slice(1) : pathname;
entry = path.join(this.config.staticDir, entry);
const relative = path.relative(this.config.staticDir, entry);
entry = path.join(this.#config.staticDir, entry);
const relative = path.relative(this.#config.staticDir, entry);
if (relative.startsWith("..")) {
throw new Error(
`Processed file resolved outside of static dir ${entry}`,
@@ -84,16 +113,16 @@ export class MemoryBuildCache implements DevBuildCache {
}
// Might be a file that we still need to process
const transformed = await this.transformer.process(
const transformed = await this.#transformer.process(
entry,
"development",
this.target,
this.#config.target,
);
if (transformed !== null) {
for (let i = 0; i < transformed.length; i++) {
const file = transformed[i];
const relative = path.relative(this.config.staticDir, file.path);
const relative = path.relative(this.#config.staticDir, file.path);
if (relative.startsWith(".")) {
throw new Error(
`Processed file resolved outside of static dir ${file.path}`,
@@ -108,10 +137,10 @@ export class MemoryBuildCache implements DevBuildCache {
}
} else {
try {
const filePath = path.join(this.config.staticDir, pathname);
const relative = path.relative(this.config.staticDir, filePath);
const filePath = path.join(this.#config.staticDir, pathname);
const relative = path.relative(this.#config.staticDir, filePath);
if (!relative.startsWith(".") && (await Deno.stat(filePath)).isFile) {
this.addUnprocessedFile(pathname);
this.addUnprocessedFile(pathname, this.#config.staticDir);
return this.readFile(pathname);
}
} catch (err) {
@@ -124,14 +153,10 @@ export class MemoryBuildCache implements DevBuildCache {
return null;
}
getIslandChunkName(islandName: string): string | null {
return this.islands.get(islandName) ?? null;
}
addUnprocessedFile(pathname: string): void {
addUnprocessedFile(pathname: string, dir: string): void {
this.#unprocessedFiles.set(
pathname,
path.join(this.config.staticDir, pathname),
path.join(dir, pathname),
);
}
@@ -144,39 +169,68 @@ export class MemoryBuildCache implements DevBuildCache {
this.#processedFiles.set(pathname, { content, hash });
}
// deno-lint-ignore require-await
async flush(): Promise<void> {
this.#ready.resolve();
const preparer = new IslandPreparer();
// Load islands
await Promise.all(
Array.from(this.islandModNameToChunk.entries()).map(
async ([name, chunk]) => {
const fileUrl = path.toFileUrl(chunk.server);
const mod = await import(fileUrl.href);
if (chunk.browser === null) {
throw new Error(`Unexpected missing browser chunk`);
}
preparer.prepare(this.islandRegistry, mod, chunk.browser, name);
},
),
);
}
async prepare(): Promise<void> {
// Load FS routes
const files = await Promise.all(this.#fsRoutes.files.map(async (file) => {
const fileUrl = path.toFileUrl(file.filePath);
return {
...file,
mod: await import(fileUrl.href),
};
}));
this.#commands = fsItemsToCommands(files);
}
}
// await fsAdapter.mkdirp(staticOutDir);
export class DiskBuildCache implements DevBuildCache {
hasSnapshot = true;
islands = new Map<string, string>();
export class DiskBuildCache<State> implements DevBuildCache<State> {
#processedFiles = new Map<string, string | null>();
#unprocessedFiles = new Map<string, string>();
#transformer: FreshFileTransformer;
#target: string | string[];
#transformer: FileTransformer;
#config: ResolvedBuildConfig;
islandModNameToChunk = new Map<string, IslandModChunk>();
#fsRoutes: FsRoute<State>;
root: string;
islandRegistry: ServerIslandRegistry = new Map();
constructor(
public config: ResolvedFreshConfig,
public buildId: string,
transformer: FreshFileTransformer,
target: string | string[],
config: ResolvedBuildConfig,
fsRoutes: FsRoute<State>,
transformer: FileTransformer,
) {
this.#fsRoutes = fsRoutes;
this.#transformer = transformer;
this.#target = target;
this.#config = config;
this.root = config.root;
}
getIslandChunkName(islandName: string): string | null {
return this.islands.get(islandName) ?? null;
getFsRoutes(): Command<State>[] {
return [];
}
addUnprocessedFile(pathname: string): void {
addUnprocessedFile(pathname: string, dir: string): void {
this.#unprocessedFiles.set(
pathname.replaceAll(WINDOWS_SEPARATOR, "/"),
path.join(this.config.staticDir, pathname),
path.join(dir, pathname),
);
}
@@ -188,8 +242,8 @@ export class DiskBuildCache implements DevBuildCache {
this.#processedFiles.set(pathname, hash);
const outDir = pathname === "/metafile.json"
? this.config.build.outDir
: path.join(this.config.build.outDir, "static");
? this.#config.outDir
: path.join(this.#config.outDir, "static");
const filePath = path.join(outDir, pathname);
assertInDir(filePath, outDir);
@@ -202,9 +256,12 @@ export class DiskBuildCache implements DevBuildCache {
throw new Error("Not implemented in build mode");
}
async prepare(): Promise<void> {
// not needed
}
async flush(): Promise<void> {
const staticDir = this.config.staticDir;
const outDir = this.config.build.outDir;
const { staticDir, outDir, target, root } = this.#config;
if (await fsAdapter.isDirectory(staticDir)) {
const entries = fsAdapter.walk(staticDir, {
@@ -224,7 +281,7 @@ export class DiskBuildCache implements DevBuildCache {
const result = await this.#transformer.process(
entry.path,
"production",
this.#target,
target,
);
if (result !== null) {
@@ -237,30 +294,21 @@ export class DiskBuildCache implements DevBuildCache {
} else {
const relative = path.relative(staticDir, entry.path);
const pathname = `/${relative}`;
this.addUnprocessedFile(pathname);
this.addUnprocessedFile(pathname, staticDir);
}
}
}
const snapshot: BuildSnapshot = {
version: 1,
buildId: this.buildId,
islands: {},
staticFiles: {},
};
for (const [name, chunk] of this.islands.entries()) {
snapshot.islands[name] = chunk;
}
const staticFiles = new Map<string, FileSnapshot>();
for (const [name, filePath] of this.#unprocessedFiles.entries()) {
const file = await Deno.open(filePath);
const hash = await hashContent(file.readable);
snapshot.staticFiles[name] = {
staticFiles.set(name, {
name,
hash,
generated: false,
};
filePath: path.relative(root, filePath),
});
}
for (const [name, maybeHash] of this.#processedFiles.entries()) {
@@ -271,21 +319,118 @@ export class DiskBuildCache implements DevBuildCache {
continue;
}
const filePath = path.join(outDir, "static", name);
if (maybeHash === null) {
const filePath = path.join(this.config.build.outDir, "static", name);
const file = await Deno.open(filePath);
hash = await hashContent(file.readable);
}
snapshot.staticFiles[name] = {
staticFiles.set(name, {
name,
hash,
generated: true,
};
filePath: path.relative(root, filePath),
});
}
await Deno.writeTextFile(
getSnapshotPath(this.config),
JSON.stringify(snapshot, null, 2),
path.join(outDir, "static-files.json"),
JSON.stringify(Array.from(staticFiles.values()), null, 2),
);
const islandSpecifiers: string[] = [];
for (const spec of this.islandModNameToChunk.keys()) {
islandSpecifiers.push(spec);
}
const editWarning =
`// WARNING: DO NOT EDIT THIS FILE. It is autogenerated by Fresh.`;
const islands = Array.from(this.islandModNameToChunk.values());
await Deno.writeTextFile(
path.join(outDir, "snapshot.js"),
`${editWarning}
import { IslandPreparer } from "fresh/do-not-use";
import staticFileData from "./static-files.json" with { type: "json" };
// Import islands
${
islands
.map((item) => {
const spec = pathToSpec(path.relative(outDir, item.server));
return `import * as ${item.name} from "${spec}";`;
})
.join("\n")
}
// Import routes
${
this.#fsRoutes.files
.map((item, i) => {
const spec = pathToSpec(path.relative(outDir, item.filePath));
return `import * as fsRoute_${i} from "${spec}"`;
})
.join("\n")
}
export const version = ${JSON.stringify(this.#config.buildId)};
const prefix = \`/_fresh/js/\${version}\`;
export const islands = new Map();
const islandPreparer = new IslandPreparer();
${
islands.map((item) => {
// Strip prefix
const prefix = `/_fresh/js/${this.#config.buildId}`;
const chunkName = item.browser
? item.browser.slice(prefix.length)
: item.browser;
return `islandPreparer.prepare(islands, ${item.name}, \`\${prefix}${chunkName}\`, ${
JSON.stringify(item.name)
});`;
}).join("\n")
}
export const staticFiles = new Map();
for (let i = 0; i < staticFileData.length; i++) {
const data = staticFileData[i];
staticFiles.set(data.name, data);
}
export const fsRoutes = [
${
this.#fsRoutes.files
.map((item, i) => {
const id = JSON.stringify(item.id);
const pattern = JSON.stringify(item.pattern);
return ` { id: ${id}, mod: fsRoute_${i}, type: ${
JSON.stringify(item.type)
}, pattern: ${pattern} },`;
})
.join("\n")
}
];
`,
);
// TODO: Make main file configurable
const appPath = path.relative(outDir, root);
await Deno.writeTextFile(
path.join(outDir, "server.js"),
`${editWarning}
import { setBuildCache, ProdBuildCache, path } from "fresh/do-not-use";
import * as snapshot from "./snapshot.js";
import { app } from "${appPath}/main.ts";
const root = path.join(import.meta.dirname, ${JSON.stringify(appPath)});
setBuildCache(app, new ProdBuildCache(root, snapshot));
export default {
fetch: app.handler()
}`,
);
}
}
+12 -12
View File
@@ -1,30 +1,30 @@
import { expect } from "@std/expect";
import * as path from "@std/path";
import { MemoryBuildCache } from "./dev_build_cache.ts";
import { FreshFileTransformer } from "./file_transformer.ts";
import { FileTransformer } from "./file_transformer.ts";
import { createFakeFs, withTmpDir } from "../test_utils.ts";
import type { ResolvedFreshConfig } from "../mod.ts";
import type { ResolvedBuildConfig } from "./builder.ts";
Deno.test({
name: "MemoryBuildCache - should error if reading outside of staticDir",
fn: async () => {
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const config: ResolvedFreshConfig = {
const config: ResolvedBuildConfig = {
root: tmp,
mode: "development",
basePath: "/",
staticDir: path.join(tmp, "static"),
build: {
outDir: path.join(tmp, "dist"),
},
buildId: "",
ignore: [],
islandDir: "",
outDir: "",
routeDir: "",
staticDir: "",
target: "latest",
};
const fileTransformer = new FreshFileTransformer(createFakeFs({}));
const fileTransformer = new FileTransformer(createFakeFs({}), tmp);
const buildCache = new MemoryBuildCache(
config,
"testing",
{ dir: "", files: [], id: "" },
fileTransformer,
"latest",
);
const thrown = buildCache.readFile("../SECRETS.txt");
+7 -3
View File
@@ -23,6 +23,7 @@ export interface OnTransformArgs {
text: string;
content: Uint8Array;
mode: TransformMode;
root: string;
}
export type TransformFn = (
args: OnTransformArgs,
@@ -56,12 +57,14 @@ interface TransformReq {
inputFiles: string[];
}
export class FreshFileTransformer {
export class FileTransformer {
#transformers: Transformer[] = [];
#fs: FsAdapter;
#root: string;
constructor(fs: FsAdapter) {
constructor(fs: FsAdapter, root: string) {
this.#fs = fs;
this.#root = root;
}
onTransform(options: OnTransformOptions, callback: TransformFn): void {
@@ -147,6 +150,7 @@ export class FreshFileTransformer {
mode,
target,
content: req!.content,
root: this.#root,
get text() {
return new TextDecoder().decode(req!.content);
},
@@ -243,7 +247,7 @@ export class FreshFileTransformer {
const CSS_URL_REGEX = /url\(("[^"]+"|'[^']+'|[^)]+)\)/g;
export function cssAssetHash(transformer: FreshFileTransformer) {
export function cssAssetHash(transformer: FileTransformer) {
transformer.onTransform({
pluginName: "fresh-css",
filter: /\.css$/,
+18 -7
View File
@@ -1,14 +1,11 @@
import { expect } from "@std/expect";
import type { FsAdapter } from "../fs.ts";
import {
FreshFileTransformer,
type ProcessedFile,
} from "./file_transformer.ts";
import { FileTransformer, type ProcessedFile } from "./file_transformer.ts";
import { delay } from "../test_utils.ts";
function testTransformer(files: Record<string, string>) {
function testTransformer(files: Record<string, string>, root = "/") {
const mockFs: FsAdapter = {
cwd: () => "/",
cwd: () => root,
isDirectory: () => Promise.resolve(false),
mkdirp: () => Promise.resolve(),
walk: async function* foo() {
@@ -21,7 +18,7 @@ function testTransformer(files: Record<string, string>) {
return Promise.resolve(buf);
},
};
return new FreshFileTransformer(mockFs);
return new FileTransformer(mockFs, root);
}
function consumeResult(result: ProcessedFile[]) {
@@ -228,3 +225,17 @@ Deno.test(
]);
},
);
Deno.test("FileTransformer - pass root to args", async () => {
const transformer = testTransformer({ "foo.txt": "foo" }, "/<root>/");
let root = "";
transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
root = args.root;
return undefined;
});
await transformer.process("foo.txt", "development", "");
expect(root).toEqual("/<root>/");
});
+102
View File
@@ -0,0 +1,102 @@
import type { FsAdapter } from "../fs.ts";
import type { WalkEntry } from "@std/fs/walk";
import type { FsRouteFileNoMod } from "./dev_build_cache.ts";
import * as path from "@std/path";
import { pathToPattern } from "../router.ts";
import { CommandType } from "../commands.ts";
import { sortRoutePaths } from "../fs_routes.ts";
const GROUP_REG = /[/\\\\]\((_[^/\\\\]+)\)[/\\\\]/;
export async function crawlRouteDir<State>(
fs: FsAdapter,
routeDir: string,
ignore: RegExp[],
onIslandSpecifier: (spec: string) => void,
files: FsRouteFileNoMod<State>[],
) {
await walkDir(
fs,
routeDir,
(entry) => {
// A `(_islands)` path segment is a local island folder.
// Any route path segment wrapped in `(_...)` is ignored
// during route collection.
const match = entry.path.match(GROUP_REG);
if (match !== null) {
if (match[1] === "_islands") {
onIslandSpecifier(entry.path);
}
return;
}
const relative = path.relative(routeDir, entry.path);
const url = new URL(relative, "http://localhost/");
const id = url.pathname.slice(0, url.pathname.lastIndexOf("."));
let pattern = "*";
let routePattern = pattern;
let type = CommandType.Route;
if (id.endsWith("/_middleware")) {
type = CommandType.Middleware;
pattern = pathToPattern(
id.slice(1, -"/_middleware".length),
{ keepGroups: true },
);
routePattern = pattern;
} else if (id.endsWith("/_layout")) {
type = CommandType.Layout;
pattern = pathToPattern(
id.slice(1, -"/_layout".length),
{ keepGroups: true },
);
routePattern = pattern;
} else if (id.endsWith("/_app")) {
type = CommandType.App;
} else if (id.endsWith("/_404")) {
type = CommandType.NotFound;
} else if (id.endsWith("/_error") || id.endsWith("/_500")) {
type = CommandType.Error;
pattern = pathToPattern(
id.slice(1, -"/_error".length),
{ keepGroups: true },
);
routePattern = pattern;
} else {
pattern = pathToPattern(id.slice(1), { keepGroups: true });
if (id.endsWith("/index")) {
if (!pattern.endsWith("/")) {
pattern += "/";
}
}
routePattern = pathToPattern(id.slice(1));
}
files.push({ id, filePath: entry.path, type, pattern, routePattern });
},
ignore,
);
files.sort((a, b) => sortRoutePaths(a.id, b.id));
}
export async function walkDir(
fs: FsAdapter,
dir: string,
callback: (entry: WalkEntry) => void,
ignore: RegExp[],
) {
if (!await fs.isDirectory(dir)) return;
const entries = fs.walk(dir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
skip: ignore,
});
for await (const entry of entries) {
callback(entry);
}
}
+5 -1
View File
@@ -1,4 +1,8 @@
export { Builder } from "./builder.ts";
export {
Builder,
type BuildOptions,
type ResolvedBuildConfig,
} from "./builder.ts";
export {
type OnTransformArgs,
type OnTransformOptions,
-54
View File
@@ -1,54 +0,0 @@
import type { ComponentChildren } from "preact";
export function FinishSetup() {
return (
<Doc>
<div class="frsh-error-page">
<div style="max-width: 48rem; padding: 2rem 1rem; margin: 0 auto; font-family: sans-serif">
<h1>Finish setting up Fresh</h1>
<ol style="line-height: 1.6; margin-bottom: 1.5rem">
<li>
Go to your project in Deno Deploy and click the{" "}
<code>Settings</code> tab.
</li>
<li>
In the Git Integration section, enter <code>deno task build</code>
{" "}
in the <code>Build Command</code> input.
</li>
<li>
Save the changes.
</li>
</ol>
</div>
</div>
</Doc>
);
}
export function ForgotBuild() {
return (
<Doc>
<div class="frsh-error-page">
<div style="max-width: 48rem; padding: 2rem 1rem; margin: 0 auto; font-family: sans-serif">
<h1>Missing build directory</h1>
<p>
Did you forget to run <code>deno task build</code>?
</p>
</div>
</div>
</Doc>
);
}
function Doc(props: { children?: ComponentChildren }) {
return (
<html>
<head>
<meta charset="utf-8" />
<title>Finish setting up Fresh</title>
</head>
<body>{props.children}</body>
</html>
);
}
+281
View File
@@ -0,0 +1,281 @@
import type { AnyComponent } from "preact";
import type { RouteConfig } from "./types.ts";
import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
import type { MiddlewareFn } from "./middlewares/mod.ts";
import type { AsyncAnyComponent } from "./render.ts";
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
import type { PageProps } from "./render.ts";
import {
type Command,
CommandType,
newAppCmd,
newErrorCmd,
newLayoutCmd,
newMiddlewareCmd,
newNotFoundCmd,
newRouteCmd,
} from "./commands.ts";
export interface FreshFsMod<State> {
config?: RouteConfig;
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
handlers?: RouteHandler<unknown, State>;
default?:
| AnyComponent<PageProps<unknown, State>>
| AsyncAnyComponent<PageProps<unknown, State>>;
}
export interface FsRouteFile<State> {
id: string; // FIXME remove?
filePath: string;
mod: FreshFsMod<State>;
pattern: string;
type: CommandType;
routePattern: string;
}
// deno-lint-ignore no-explicit-any
function isFreshFile<State>(mod: any): mod is FreshFsMod<State> {
if (mod === null || typeof mod !== "object") return false;
return typeof mod.default === "function" ||
typeof mod.config === "object" || typeof mod.handlers === "object" ||
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
typeof mod.handler === "function";
}
export interface FsRoutesOptions {
/**
* Parent directory for the `/routes` and `/islands` folders.
*
* By default, the `root` config option of the provided app is used.
* @default app.config.root
*/
dir?: string;
ignoreFilePattern?: RegExp[];
loadRoute: (path: string) => Promise<unknown>;
}
export function fsItemsToCommands<State>(
items: FsRouteFile<State>[],
): Command<State>[] {
const commands: Command<State>[] = [];
for (let i = 0; i < items.length; i++) {
const { filePath, type, mod, pattern, routePattern } = items[i];
if (!isFreshFile<State>(mod)) {
throw new Error(
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${filePath}`,
);
}
const handlers = mod.handlers ?? mod.handler ?? null;
if (typeof handlers === "function" && handlers.length > 1) {
throw new Error(
`Handlers must only have one argument but found more than one. Check the function signature in: ${filePath}`,
);
}
switch (type) {
case CommandType.Middleware: {
let middlewares = (handlers ?? mod.default) as unknown as
| MiddlewareFn<State>
| MiddlewareFn<State>[]
| HandlerByMethod<unknown, State> ??
null;
if (middlewares === null) continue;
if (isHandlerByMethod(middlewares)) {
warnInvalidRoute(
"Middleware does not support object handlers with GET, POST, etc.",
);
continue;
}
if (!Array.isArray(middlewares)) {
middlewares = [middlewares];
}
commands.push(
newMiddlewareCmd(pattern, middlewares, true),
);
continue;
}
case CommandType.Layout: {
if (handlers !== null) {
warnInvalidRoute("Layout does not support handlers");
}
if (!mod.default) continue;
commands.push(newLayoutCmd(pattern, mod.default, mod.config, true));
continue;
}
case CommandType.Error: {
commands.push(newErrorCmd(
pattern,
{
component: mod.default ?? undefined,
config: mod.config ?? undefined,
// deno-lint-ignore no-explicit-any
handler: (handlers as any) ?? undefined,
},
true,
));
continue;
}
case CommandType.NotFound: {
commands.push(newNotFoundCmd({
config: mod.config,
component: mod.default,
// deno-lint-ignore no-explicit-any
handler: handlers as any ?? undefined,
}));
continue;
}
case CommandType.App: {
if (mod.default === undefined) continue;
commands.push(newAppCmd(mod.default));
continue;
}
case CommandType.Route: {
commands.push(newRouteCmd(
pattern,
{
config: {
...mod.config,
routeOverride: mod.config?.routeOverride ?? routePattern,
},
// deno-lint-ignore no-explicit-any
handler: (handlers as any) ?? undefined,
component: mod.default,
},
false,
));
continue;
}
case CommandType.Handler:
throw new Error(`Not supported`);
case CommandType.FsRoute:
throw new Error(`Nested FsRoutes are not supported`);
default:
throw new Error(`Unknown command type: ${type}`);
}
}
return commands;
}
function warnInvalidRoute(message: string) {
// deno-lint-ignore no-console
console.warn(
`🍋 %c[WARNING] Unsupported route config: ${message}`,
"color:rgb(251, 184, 0)",
);
}
const APP_REG = /_app(?!\.[tj]sx?)?$/;
/**
* Sort route paths where special Fresh files like `_app`,
* `_layout` and `_middleware` are sorted in front.
*/
export function sortRoutePaths(a: string, b: string) {
// The `_app` route should always be the first
if (APP_REG.test(a)) return -1;
else if (APP_REG.test(b)) return 1;
const aLen = a.length;
const bLen = b.length;
let segment = false;
let aIdx = 0;
let bIdx = 0;
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
const charA = a.charAt(aIdx);
const charB = b.charAt(bIdx);
// When comparing a grouped route with a non-grouped one, we
// need to skip over the group name to effectively compare the
// actual route.
if (charA === "(" && charB !== "(") {
if (charB == "[") return -1;
return 1;
} else if (charB === "(" && charA !== "(") {
if (charA == "[") return 1;
return -1;
}
if (charA === "/" || charB === "/") {
segment = true;
// If the other path doesn't close the segment
// then we don't need to continue
if (charA !== "/") return 1;
if (charB !== "/") return -1;
continue;
}
if (segment) {
segment = false;
const scoreA = getRoutePathScore(charA, a, aIdx);
const scoreB = getRoutePathScore(charB, b, bIdx);
if (scoreA === scoreB) {
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
continue;
}
return scoreA > scoreB ? -1 : 1;
}
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
// If we're at the end of A or B, then we assume that the longer
// path is more specific
if (aIdx === aLen - 1 && bIdx < bLen - 1) {
return 1;
} else if (bIdx === bLen - 1 && aIdx < aLen - 1) {
return -1;
}
}
return 0;
}
/**
* Assign a score based on the first two characters of a path segment.
* The goal is to sort `_middleware` and `_layout` in front of everything
* and `[` or `[...` last respectively.
*/
function getRoutePathScore(char: string, s: string, i: number): number {
if (char === "_") {
if (i + 1 < s.length) {
if (s[i + 1] === "e") return 4;
if (s[i + 1] === "m") return 6;
}
return 5;
} else if (char === "[") {
if (i + 1 < s.length && s[i + 1] === ".") {
return 0;
}
return 1;
}
if (
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" &&
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x"
) {
return 3;
}
return 2;
}
@@ -1,42 +1,38 @@
import { App } from "../../app.ts";
import {
type FreshFsItem,
fsRoutes,
type FsRoutesOptions,
sortRoutePaths,
type TESTING_ONLY__FsRoutesOptions,
} from "./mod.ts";
import { delay, FakeServer } from "../../test_utils.ts";
import { createFakeFs } from "../../test_utils.ts";
import { App, setBuildCache } from "./app.ts";
import { type FreshFsMod, sortRoutePaths } from "./fs_routes.ts";
import { delay, FakeServer, MockBuildCache } from "./test_utils.ts";
import { createFakeFs } from "./test_utils.ts";
import { expect, fn } from "@std/expect";
import { stub } from "@std/testing/mock";
import { type HandlerByMethod, type HandlerFn, page } from "../../handlers.ts";
import type { Method } from "../../router.ts";
import { parseHtml } from "../../../tests/test_utils.tsx";
import type { FreshContext } from "../../context.ts";
import { HttpError } from "../../error.ts";
import { type HandlerByMethod, type HandlerFn, page } from "./handlers.ts";
import type { Method } from "./router.ts";
import { parseHtml } from "../tests/test_utils.tsx";
import type { Context } from "./context.ts";
import { HttpError } from "./error.ts";
import type { FsRouteFileNoMod } from "./dev/dev_build_cache.ts";
import { crawlRouteDir } from "./dev/fs_crawl.ts";
import * as path from "@std/path";
async function createServer<T>(
files: Record<string, string | Uint8Array | FreshFsItem<T>>,
files: Record<string, string | Uint8Array | FreshFsMod<T>>,
): Promise<FakeServer> {
const app = new App<T>();
const fs = createFakeFs(files);
const routeDir = path.join(fs.cwd(), "routes");
const rawFiles: FsRouteFileNoMod<T>[] = [];
await crawlRouteDir(fs, routeDir, [], () => {}, rawFiles);
const fsFiles = rawFiles.map((file) => {
// deno-lint-ignore no-explicit-any
return { ...file, mod: files[file.filePath] as any };
});
const app = new App<T>()
.fsRoutes();
const buildCache = new MockBuildCache<T>(fsFiles);
setBuildCache<T>(app, buildCache);
await fsRoutes(
app,
{
dir: ".",
loadIsland: async () => {},
// deno-lint-ignore require-await
loadRoute: async (filePath) => {
const full = `routes/${filePath.replaceAll(/[\\]+/g, "/")}`;
if (full in files) {
return files[full];
}
throw new Error(`Mock FS: file ${full} not found`);
},
_fs: createFakeFs(files),
} as FsRoutesOptions & TESTING_ONLY__FsRoutesOptions,
);
return new FakeServer(app.handler());
}
@@ -178,7 +174,7 @@ Deno.test("fsRoutes - middleware", async () => {
const server = await createServer<{ text: string }>({
"routes/index.ts": { handler: (ctx) => new Response(ctx.state.text) },
"routes/_middleware.ts": {
default: ((ctx: FreshContext<{ text: string }>) => {
default: ((ctx: Context<{ text: string }>) => {
ctx.state.text = "ok";
return ctx.next();
// deno-lint-ignore no-explicit-any
@@ -1546,3 +1542,25 @@ Deno.test("fsRoutes - call correct middleware", async () => {
text = await res.text();
expect(text).toEqual("_middleware");
});
// Issue: https://github.com/denoland/fresh/issues/2045
Deno.test("fsRoutes - merge group methods", async () => {
const server = await createServer({
"routes/(foo)/bar/index.ts": {
handler: {
POST: () => new Response("POST ok"),
},
},
"routes/bar/index.ts": {
handler: {
GET: () => new Response("GET ok"),
},
},
});
let res = await server.get("/bar");
expect(await res.text()).toEqual("GET ok");
res = await server.post("/bar");
expect(await res.text()).toEqual("POST ok");
});
+5
View File
@@ -0,0 +1,5 @@
import * as path from "@std/path";
export { setBuildCache } from "./app.ts";
export { IslandPreparer, ProdBuildCache } from "./build_cache.ts";
export { path };
+11 -14
View File
@@ -4,11 +4,14 @@ import type { BuildCache, StaticFile } from "../build_cache.ts";
import { expect } from "@std/expect";
import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
import { BUILD_ID } from "../runtime/build_id.ts";
import type { Command } from "../commands.ts";
import type { ServerIslandRegistry } from "../context.ts";
class MockBuildCache implements BuildCache {
root = "";
buildId = "MockId";
files = new Map<string, StaticFile>();
hasSnapshot = true;
islandRegistry: ServerIslandRegistry = new Map();
constructor(files: Record<string, { hash: string | null; content: string }>) {
const encoder = new TextEncoder();
@@ -25,13 +28,15 @@ class MockBuildCache implements BuildCache {
}
}
// deno-lint-ignore no-explicit-any
getFsRoutes(): Command<any>[] {
return [];
}
// deno-lint-ignore require-await
async readFile(pathname: string): Promise<StaticFile | null> {
return this.files.get(pathname) ?? null;
}
getIslandChunkName(_islandName: string): string | null {
return null;
}
}
Deno.test("static files - 200", async () => {
@@ -135,13 +140,9 @@ Deno.test("static files - disables caching in development", async () => {
{
buildCache,
config: {
root: "",
basePath: "",
build: {
outDir: "",
},
mode: "development",
root: ".",
staticDir: "",
},
},
);
@@ -163,13 +164,9 @@ Deno.test("static files - enables caching in production", async () => {
{
buildCache,
config: {
root: "",
basePath: "",
build: {
outDir: "",
},
mode: "production",
root: ".",
staticDir: "",
},
},
);
-1
View File
@@ -1,6 +1,5 @@
export { App, type ListenOptions } from "./app.ts";
export { trailingSlashes } from "./middlewares/trailing_slashes.ts";
export { fsRoutes, type FsRoutesOptions } from "./plugins/fs_routes/mod.ts";
export {
type HandlerByMethod,
type HandlerFn,
-397
View File
@@ -1,397 +0,0 @@
import type { AnyComponent } from "preact";
import type { App } from "../../app.ts";
import type { WalkEntry } from "@std/fs/walk";
import * as path from "@std/path";
import type { RouteConfig } from "../../types.ts";
import type { RouteHandler } from "../../handlers.ts";
import type { MiddlewareFn } from "../../middlewares/mod.ts";
import type { AsyncAnyComponent } from "../../render.ts";
import { pathToPattern } from "../../router.ts";
import { type HandlerFn, isHandlerByMethod } from "../../handlers.ts";
import { type FsAdapter, fsAdapter } from "../../fs.ts";
import { parseRootPath } from "../../config.ts";
import type { PageProps } from "../../render.ts";
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
const GROUP_REG = /(^|[/\\\\])\((_[^/\\\\]+)\)[/\\\\]/;
interface InternalRoute<State> {
path: string;
base: string;
filePath: string;
config: RouteConfig | null;
handlers:
| MiddlewareFn<State>[]
| RouteHandler<unknown, State>
| null;
component: AnyComponent<PageProps<unknown, State>> | null;
}
export interface FreshFsItem<State> {
config?: RouteConfig;
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
handlers?: RouteHandler<unknown, State>;
default?:
| AnyComponent<PageProps<unknown, State>>
| AsyncAnyComponent<PageProps<unknown, State>>;
}
// deno-lint-ignore no-explicit-any
function isFreshFile<State>(mod: any): mod is FreshFsItem<State> {
return mod !== null && typeof mod === "object" &&
typeof mod.default === "function" ||
typeof mod.config === "object" || typeof mod.handlers === "object" ||
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
typeof mod.handler === "function";
}
export interface FsRoutesOptions {
/**
* Parent directory for the `/routes` and `/islands` folders.
*
* By default, the `root` config option of the provided app is used.
* @default app.config.root
*/
dir?: string;
ignoreFilePattern?: RegExp[];
loadRoute: (path: string) => Promise<unknown>;
loadIsland: (path: string) => Promise<unknown>;
}
export interface TESTING_ONLY__FsRoutesOptions {
_fs?: FsAdapter;
}
export async function fsRoutes<State>(
app: App<State>,
options_: FsRoutesOptions,
) {
const options = options_ as FsRoutesOptions & TESTING_ONLY__FsRoutesOptions;
const ignore = options.ignoreFilePattern ?? [TEST_FILE_PATTERN];
const fs = options._fs ?? fsAdapter;
const dir = options.dir
? parseRootPath(options.dir, fs.cwd())
: app.config.root;
const islandDir = path.join(dir, "islands");
const routesDir = path.join(dir, "routes");
const islandPaths: string[] = [];
const relRoutePaths: string[] = [];
// Walk routes folder
await Promise.all([
walkDir(
islandDir,
(entry) => {
islandPaths.push(entry.path);
},
ignore,
fs,
),
walkDir(
routesDir,
(entry) => {
const relative = path.relative(routesDir, entry.path);
// A `(_islands)` path segment is a local island folder.
// Any route path segment wrapped in `(_...)` is ignored
// during route collection.
const match = relative.match(GROUP_REG);
if (match && match[2][0] === "_") {
if (match[2] === "_islands") {
islandPaths.push(entry.path);
}
return;
}
const url = new URL(relative, "http://localhost/");
relRoutePaths.push(url.pathname.slice(1));
},
ignore,
fs,
),
]);
await Promise.all(islandPaths.map(async (islandPath) => {
const relative = path.relative(islandDir, islandPath);
// deno-lint-ignore no-explicit-any
const mod = await options.loadIsland(relative) as any;
for (const key of Object.keys(mod)) {
const maybeFn = mod[key];
if (typeof maybeFn === "function") {
app.island(islandPath, key, maybeFn);
}
}
}));
const routeModules: InternalRoute<State>[] = await Promise.all(
relRoutePaths.map(async (routePath) => {
const mod = await options.loadRoute(routePath);
if (!isFreshFile(mod)) {
throw new Error(
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${routePath}`,
);
}
const handlers = mod.handlers ?? mod.handler ?? null;
if (typeof handlers === "function" && handlers.length > 1) {
throw new Error(
`Handlers must only have one argument but found more than one. Check the function signature in: ${
path.join(routesDir, routePath)
}`,
);
}
const normalizedPath = `/${
routePath.slice(0, routePath.lastIndexOf("."))
}`;
const base = normalizedPath.slice(0, normalizedPath.lastIndexOf("/"));
const isMiddleware = normalizedPath.endsWith("/_middleware");
return {
path: normalizedPath,
filePath: routePath,
base,
handlers: mod.handlers ?? mod.handler ??
(isMiddleware ? mod.default ?? null : null),
config: mod.config ?? null,
component: isMiddleware ? null : mod.default ?? null,
} as InternalRoute<State>;
}),
);
routeModules.sort((a, b) => sortRoutePaths(a.path, b.path));
const errorPaths = new Set<string>();
for (let i = 0; i < routeModules.length; i++) {
const routeMod = routeModules[i];
const normalized = routeMod.path;
if (normalized.endsWith("/_app")) {
const component = routeMod.component;
if (component !== null) {
app.appWrapper(component);
}
continue;
} else if (normalized.endsWith("/_middleware")) {
if (routeMod.handlers === null) continue;
if (isHandlerByMethod(routeMod.handlers)) {
warnInvalidRoute(
"Middleware does not support object handlers with GET, POST, etc.",
);
continue;
}
const pattern = pathToPattern(
normalized.slice(1, -"/_middleware".length),
{ keepGroups: true },
);
const handlers = (Array.isArray(routeMod.handlers)
? routeMod.handlers
: [routeMod.handlers]) as MiddlewareFn<State>[];
app.use(pattern, ...handlers);
continue;
} else if (normalized.endsWith("/_layout")) {
if (routeMod.handlers !== null) {
warnInvalidRoute("Layout does not support handlers");
}
const pattern = pathToPattern(normalized.slice(1, -"/_layout".length), {
keepGroups: true,
});
const { component, config } = routeMod;
if (component !== null) {
app.layout(pattern, component, config ?? undefined);
}
continue;
} else if (normalized.endsWith("/_error")) {
const pattern = pathToPattern(normalized.slice(1, -"/_error".length), {
keepGroups: true,
});
errorPaths.add(pattern);
app.onError(pattern, {
config: routeMod.config ?? undefined,
component: routeMod.component ?? undefined,
// deno-lint-ignore no-explicit-any
handler: routeMod.handlers as any ?? undefined,
});
continue;
} else if (normalized.endsWith("/_404")) {
app.notFound({
config: routeMod.config ?? undefined,
component: routeMod.component ?? undefined,
// deno-lint-ignore no-explicit-any
handler: routeMod.handlers as any ?? undefined,
});
continue;
} else if (normalized.endsWith("/_500")) {
const pattern = pathToPattern(normalized.slice(1, -"/_500".length), {
keepGroups: true,
});
if (errorPaths.has(pattern)) continue;
app.onError(pattern, {
config: routeMod.config ?? undefined,
component: routeMod.component ?? undefined,
// deno-lint-ignore no-explicit-any
handler: routeMod.handlers as any ?? undefined,
});
continue;
}
let pattern = pathToPattern(normalized.slice(1), { keepGroups: true });
if (normalized.endsWith("/index")) {
if (!pattern.endsWith("/")) {
pattern += "/";
}
}
const routePattern = pathToPattern(normalized.slice(1));
app.route(pattern, {
config: {
...routeMod.config ?? undefined,
routeOverride: routeMod.config?.routeOverride ?? routePattern,
},
component: routeMod.component ?? undefined,
// deno-lint-ignore no-explicit-any
handler: routeMod.handlers as any ?? undefined,
});
}
}
function warnInvalidRoute(message: string) {
// deno-lint-ignore no-console
console.warn(
`🍋 %c[WARNING] Unsupported route config: ${message}`,
"color:rgb(251, 184, 0)",
);
}
async function walkDir(
dir: string,
callback: (entry: WalkEntry) => void,
ignore: RegExp[],
fs: FsAdapter,
) {
if (!await fs.isDirectory(dir)) return;
const entries = fs.walk(dir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
skip: ignore,
});
for await (const entry of entries) {
callback(entry);
}
}
const APP_REG = /_app(?!\.[tj]sx?)?$/;
/**
* Sort route paths where special Fresh files like `_app`,
* `_layout` and `_middleware` are sorted in front.
*/
export function sortRoutePaths(a: string, b: string) {
// The `_app` route should always be the first
if (APP_REG.test(a)) return -1;
else if (APP_REG.test(b)) return 1;
const aLen = a.length;
const bLen = b.length;
let segment = false;
let aIdx = 0;
let bIdx = 0;
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
const charA = a.charAt(aIdx);
const charB = b.charAt(bIdx);
// When comparing a grouped route with a non-grouped one, we
// need to skip over the group name to effectively compare the
// actual route.
if (charA === "(" && charB !== "(") {
if (charB == "[") return -1;
return 1;
} else if (charB === "(" && charA !== "(") {
if (charA == "[") return 1;
return -1;
}
if (charA === "/" || charB === "/") {
segment = true;
// If the other path doesn't close the segment
// then we don't need to continue
if (charA !== "/") return 1;
if (charB !== "/") return -1;
continue;
}
if (segment) {
segment = false;
const scoreA = getRoutePathScore(charA, a, aIdx);
const scoreB = getRoutePathScore(charB, b, bIdx);
if (scoreA === scoreB) {
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
continue;
}
return scoreA > scoreB ? -1 : 1;
}
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
// If we're at the end of A or B, then we assume that the longer
// path is more specific
if (aIdx === aLen - 1 && bIdx < bLen - 1) {
return 1;
} else if (bIdx === bLen - 1 && aIdx < aLen - 1) {
return -1;
}
}
return 0;
}
/**
* Assign a score based on the first two characters of a path segment.
* The goal is to sort `_middleware` and `_layout` in front of everything
* and `[` or `[...` last respectively.
*/
function getRoutePathScore(char: string, s: string, i: number): number {
if (char === "_") {
if (i + 1 < s.length) {
if (s[i + 1] === "e") return 4;
if (s[i + 1] === "m") return 6;
}
return 5;
} else if (char === "[") {
if (i + 1 < s.length && s[i + 1] === ".") {
return 0;
}
return 1;
}
if (
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" &&
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x"
) {
return 3;
}
return 2;
}
+3 -8
View File
@@ -12,7 +12,6 @@ import {
} from "./runtime/server/preact_hooks.tsx";
import type { Context } from "./context.ts";
import { recordSpanError, tracer } from "./otel.ts";
import type { BuildCache } from "./build_cache.ts";
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
import { renderToString } from "preact-render-to-string";
import { BUILD_ID } from "./runtime/build_id.ts";
@@ -62,7 +61,6 @@ export function preactRender<State, Data>(
vnode: VNode,
ctx: PageProps<Data, State>,
state: RenderState,
buildCache: BuildCache,
headers: Headers,
) {
try {
@@ -90,12 +88,9 @@ export function preactRender<State, Data>(
const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`;
let link = `<${encodeURI(runtimeUrl)}>; rel="modulepreload"; as="script"`;
state.islands.forEach((island) => {
const chunk = buildCache.getIslandChunkName(island.name);
if (chunk !== null) {
link += `, <${
encodeURI(`${basePath}${chunk}`)
}>; rel="modulepreload"; as="script"`;
}
link += `, <${
encodeURI(`${basePath}${island.file}`)
}>; rel="modulepreload"; as="script"`;
});
if (link !== "") {
+2 -1
View File
@@ -288,7 +288,8 @@ export function mergePath(basePath: string, path: string): string {
if (basePath.endsWith("*")) basePath = basePath.slice(0, -1);
if (basePath === "/") basePath = "";
if (path === "*" || path === "/*") path = "/*";
if (path === "*") path = "";
else if (path === "/*") path = "/*";
const s = (basePath !== "" && path === "/") ? "" : path;
return basePath + s;
+1 -1
View File
@@ -161,6 +161,6 @@ Deno.test("mergePath", () => {
expect(mergePath("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
expect(mergePath("*", "/baz")).toEqual("/baz");
expect(mergePath("/*", "/baz")).toEqual("/baz");
expect(mergePath("/foo", "*")).toEqual("/foo/*");
expect(mergePath("/foo", "*")).toEqual("/foo");
expect(mergePath("/foo", "/*")).toEqual("/foo/*");
});
+8 -21
View File
@@ -14,7 +14,6 @@ import type { Stringifiers } from "../../jsonify/stringify.ts";
import type { PageProps } from "../../render.ts";
import { Partial, type PartialProps } from "../shared.ts";
import { stringify } from "../../jsonify/stringify.ts";
import type { ServerIslandRegistry } from "../../context.ts";
import type { Island } from "../../context.ts";
import {
assetHashingHook,
@@ -90,7 +89,6 @@ export class RenderState {
constructor(
// deno-lint-ignore no-explicit-any
public ctx: PageProps<any, any>,
public islandRegistry: ServerIslandRegistry,
public buildCache: BuildCache,
public partialId: string,
) {
@@ -203,7 +201,7 @@ options[OptionsType.DIFF] = (vnode) => {
} else if (
!PATCHED.has(vnode) && !hasIslandOwner(RENDER_STATE, vnode)
) {
const island = RENDER_STATE.islandRegistry.get(vnode.type);
const island = RENDER_STATE.buildCache.islandRegistry.get(vnode.type);
if (island === undefined) {
// Not an island, but we might need to preserve keys
if (vnode.key !== undefined) {
@@ -324,7 +322,7 @@ function hasIslandOwner(current: RenderState, vnode: VNode): boolean {
let tmpVNode = vnode;
let owner;
while ((owner = current.owners.get(tmpVNode)) !== undefined) {
if (current.islandRegistry.has(owner.type as ComponentType)) {
if (current.buildCache.islandRegistry.has(owner.type as ComponentType)) {
return true;
}
tmpVNode = owner;
@@ -425,23 +423,16 @@ export interface PartialStateJson {
}
function FreshRuntimeScript() {
const { islands, nonce, ctx, islandProps, partialId, buildCache } =
RENDER_STATE!;
const { islands, nonce, ctx, islandProps, partialId } = RENDER_STATE!;
const basePath = ctx.config.basePath;
const islandArr = Array.from(islands);
if (ctx.url.searchParams.has(PARTIAL_SEARCH_PARAM)) {
const islands = islandArr.map((island) => {
const chunk = buildCache.getIslandChunkName(island.name);
if (chunk === null) {
throw new Error(
`Could not find chunk for ${island.name} ${island.file}#${island.exportName}`,
);
}
return {
exportName: island.exportName,
chunk,
chunk: island.file,
name: island.name,
};
});
@@ -464,16 +455,12 @@ function FreshRuntimeScript() {
);
} else {
const islandImports = islandArr.map((island) => {
const chunk = buildCache.getIslandChunkName(island.name);
if (chunk === null) {
throw new Error(
`Could not find chunk for ${island.name} ${island.file}#${island.exportName}`,
);
}
const named = island.exportName === "default"
? island.name
: `{ ${island.exportName} }`;
return `import ${named} from "${`${basePath}${chunk}`}";`;
: island.exportName === island.name
? `{ ${island.exportName} }`
: `{ ${island.exportName} as ${island.name} }`;
return `import ${named} from "${`${basePath}${island.file}`}";`;
}).join("");
const islandObj = "{" + islandArr.map((island) => island.name)
+25 -10
View File
@@ -1,9 +1,11 @@
import { Context } from "./context.ts";
import { Context, type ServerIslandRegistry } from "./context.ts";
import type { FsAdapter } from "./fs.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { BuildCache, StaticFile } from "./build_cache.ts";
import type { ResolvedFreshConfig } from "./config.ts";
import type { WalkEntry } from "@std/fs/walk";
import { DEFAULT_CONN_INFO } from "./app.ts";
import type { Command } from "./commands.ts";
import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts";
const STUB = {} as unknown as Deno.ServeHandlerInfo;
@@ -61,20 +63,16 @@ export class FakeServer {
}
const DEFAULT_CONFIG: ResolvedFreshConfig = {
build: {
outDir: "",
},
root: "",
mode: "production",
basePath: "",
root: "",
staticDir: "",
};
export function serveMiddleware<T>(
middleware: (ctx: Context<T>) => Response | Promise<Response>,
options: {
config?: ResolvedFreshConfig;
buildCache?: BuildCache;
buildCache?: BuildCache<T>;
next?: () => Promise<Response>;
route?: string | null;
} = {},
@@ -84,7 +82,7 @@ export function serveMiddleware<T>(
(() => new Response("not found", { status: 404 }));
const config = options.config ?? DEFAULT_CONFIG;
const buildCache = options.buildCache ??
new ProdBuildCache(config, new Map(), new Map(), true);
new MockBuildCache<T>([]);
const ctx = new Context<T>(
req,
@@ -94,7 +92,6 @@ export function serveMiddleware<T>(
{},
config,
() => Promise.resolve(next()),
new Map(),
buildCache,
);
return await middleware(ctx);
@@ -144,3 +141,21 @@ export async function withTmpDir(
},
};
}
export class MockBuildCache<State> implements BuildCache<State> {
#files: FsRouteFile<State>[];
root = "";
islandRegistry: ServerIslandRegistry = new Map();
constructor(files: FsRouteFile<State>[]) {
this.#files = files;
}
getFsRoutes(): Command<State>[] {
return fsItemsToCommands(this.#files);
}
readFile(_pathname: string): Promise<StaticFile | null> {
return Promise.resolve(null);
}
}
+21
View File
@@ -47,3 +47,24 @@ export function escapeScript(
.replaceAll(SCRIPT_ESCAPE, "<\\/$1")
.replaceAll(COMMENT_ESCAPE, options.json ? "\\u003C!--" : "\\x3C!--");
}
export class UniqueNamer {
#seen = new Map<string, number>();
getUniqueName(name: string): string {
const count = this.#seen.get(name);
if (count === undefined) {
this.#seen.set(name, 1);
} else {
this.#seen.set(name, count + 1);
name = `${name}_${count}`;
}
return name;
}
}
const PATH_TO_SPEC = /[\\/]+/g;
export function pathToSpec(str: string): string {
return str.replaceAll(PATH_TO_SPEC, "/");
}
+7 -1
View File
@@ -1,5 +1,5 @@
import { expect } from "@std/expect";
import { escapeScript, pathToExportName } from "./utils.ts";
import { escapeScript, pathToExportName, pathToSpec } from "./utils.ts";
Deno.test("filenameToExportName", () => {
expect(pathToExportName("/islands/foo.tsx")).toBe("foo");
@@ -52,3 +52,9 @@ Deno.test("escapeScript - json", () => {
"\\u003C!--<\\/ScRIpt>",
);
});
Deno.test("pathToSpec", () => {
expect(pathToSpec("/foo/bar")).toEqual("/foo/bar");
expect(pathToSpec("\\foo\\bar")).toEqual("/foo/bar");
expect(pathToSpec("\\\\foo//bar")).toEqual("/foo/bar");
});
+4 -15
View File
@@ -1,35 +1,24 @@
import { App, staticFiles } from "fresh";
import {
allIslandApp,
ALL_ISLAND_DIR,
assertNotSelector,
assertSelector,
buildProd,
Doc,
getIsland,
parseHtml,
withBrowserApp,
} from "./test_utils.tsx";
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { Partial } from "fresh/runtime";
import { getBuildCache, setBuildCache } from "../src/app.ts";
await buildProd(allIslandApp);
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
function testApp<T>(): App<T> {
const selfCounter = getIsland("SelfCounter.tsx");
const partialInIsland = getIsland("PartialInIsland.tsx");
const jsonIsland = getIsland("JsonIsland.tsx");
const app = new App<T>()
.island(selfCounter, "SelfCounter", SelfCounter)
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.use(staticFiles());
setBuildCache(app, getBuildCache(allIslandApp));
allIslandCache(app);
return app;
}
@@ -0,0 +1,15 @@
import { useSignal } from "@preact/signals";
import { useEffect } from "preact/hooks";
export function Foo() {
const active = useSignal(false);
useEffect(() => {
active.value = true;
}, []);
return (
<div id="foo-both" class={active.value ? "ready" : ""}>
{active.value ? "it works" : "it doesn't work"}
</div>
);
}
@@ -0,0 +1,11 @@
import { Foo } from "./(_islands)/Foo.tsx";
import { Foo as Foo2 } from "../foo/(_islands)/Foo.tsx";
export default function Home() {
return (
<div>
<Foo />
<Foo2 />
</div>
);
}
@@ -8,7 +8,7 @@ export function Foo() {
}, []);
return (
<div class={active.value ? "ready" : ""}>
<div id="foo" class={active.value ? "ready" : ""}>
{active.value ? "it works" : "it doesn't work"}
</div>
);
+1 -2
View File
@@ -1,8 +1,7 @@
import { Builder } from "../../../src/dev/mod.ts";
import { app } from "./main.tsx";
const builder = new Builder();
await builder.listen(app, {
await builder.listen(() => import("./main.tsx"), {
port: 4001,
});
+1 -1
View File
@@ -1,6 +1,6 @@
import { App } from "../../../src/app.ts";
const app = new App({ staticDir: "./static" }).get(
const app = new App().get(
"/",
(ctx) =>
ctx.render(
+48 -95
View File
@@ -1,4 +1,4 @@
import { App, fsRoutes, staticFiles } from "fresh";
import { App, staticFiles } from "fresh";
import { Counter } from "./fixtures_islands/Counter.tsx";
import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
@@ -12,42 +12,49 @@ import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
import { signal } from "@preact/signals";
import {
allIslandApp,
ALL_ISLAND_DIR,
buildProd,
Doc,
getIsland,
ISLAND_GROUP_DIR,
withBrowserApp,
} from "./test_utils.tsx";
import { parseHtml, waitForText } from "./test_utils.tsx";
import { expect } from "@std/expect";
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx";
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
import * as path from "@std/path";
import { setBuildCache } from "../src/app.ts";
import { getBuildCache } from "../src/app.ts";
import type { FreshConfig } from "../src/config.ts";
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts";
await buildProd(allIslandApp);
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
const islandGroupCache = await buildProd({ root: ISLAND_GROUP_DIR });
function testApp(config?: FreshConfig): App<unknown> {
const app = new App(config)
.use(staticFiles())
.fsRoutes();
allIslandCache(app);
return app;
}
function testGroupApp(config?: FreshConfig): App<unknown> {
const app = new App(config)
.use(staticFiles())
.fsRoutes();
islandGroupCache(app);
function testApp(config?: FreshConfig) {
const app = new App(config);
setBuildCache(app, getBuildCache(allIslandApp));
app.use(staticFiles());
return app;
}
Deno.test({
name: "islands - should make signals interactive",
fn: async () => {
const counterIsland = getIsland("Counter.tsx");
const app = testApp()
.island(counterIsland, "Counter", Counter)
.get("/", (ctx) => {
const sig = signal(3);
return ctx.render(
@@ -69,11 +76,7 @@ Deno.test({
Deno.test({
name: "islands - revive multiple islands from one island file",
fn: async () => {
const multipleIslands = getIsland("Multiple.tsx");
const app = testApp()
.island(multipleIslands, "Multiple1", Multiple1)
.island(multipleIslands, "Multiple2", Multiple2)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -98,10 +101,7 @@ Deno.test({
Deno.test({
name: "islands - revive multiple islands with shared signal",
fn: async () => {
const counterIsland = getIsland("Counter.tsx");
const app = testApp()
.island(counterIsland, "Counter", Counter)
.get("/", (ctx) => {
const sig = signal(0);
return ctx.render(
@@ -126,10 +126,7 @@ Deno.test({
Deno.test({
name: "islands - import json",
fn: async () => {
const jsonIsland = getIsland("JsonIsland.tsx");
const app = testApp()
.island(jsonIsland, "JsonIsland", Counter)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -153,10 +150,7 @@ Deno.test({
Deno.test({
name: "islands - returns null",
fn: async () => {
const nullIsland = getIsland("NullIsland.tsx");
const app = testApp()
.island(nullIsland, "NullIsland", NullIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -175,12 +169,7 @@ Deno.test({
Deno.test({
name: "islands - only instantiate top level island",
fn: async () => {
const counter = getIsland("Counter.tsx");
const islandInIsland = getIsland("IslandInIsland.tsx");
const app = testApp()
.island(counter, "Counter", Counter)
.island(islandInIsland, "IslandInIsland", IslandInIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -204,10 +193,7 @@ Deno.test({
Deno.test({
name: "islands - pass null JSX props to islands",
fn: async () => {
const jsxIsland = getIsland("JsxIsland.tsx");
const app = testApp()
.island(jsxIsland, "JsxIsland", JsxIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -231,10 +217,7 @@ Deno.test({
Deno.test({
name: "islands - pass JSX props to islands",
fn: async () => {
const jsxIsland = getIsland("JsxIsland.tsx");
const app = testApp()
.island(jsxIsland, "JsxIsland", JsxIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -260,10 +243,7 @@ Deno.test({
Deno.test({
name: "islands - never serialize children prop",
fn: async () => {
const jsxChildrenIsland = getIsland("JsxChildrenIsland.tsx");
const app = testApp()
.island(jsxChildrenIsland, "JsxChildrenIsland", JsxChildrenIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -294,12 +274,7 @@ Deno.test({
Deno.test({
name: "islands - instantiate islands in jsx children",
fn: async () => {
const passThrough = getIsland("PassThrough.tsx");
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.island(passThrough, "PassThrough", PassThrough)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -325,12 +300,7 @@ Deno.test({
Deno.test({
name: "islands - instantiate islands in jsx children with slots",
fn: async () => {
const counterWithSlots = getIsland("CounterWithSlots.tsx");
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.island(counterWithSlots, "CounterWithSlots", CounterWithSlots)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -367,12 +337,7 @@ Deno.test({
Deno.test({
name: "islands - nested children slots",
fn: async () => {
const passThrough = getIsland("PassThrough.tsx");
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.island(passThrough, "PassThrough", PassThrough)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -408,12 +373,7 @@ Deno.test({
Deno.test({
name: "islands - conditional jsx children",
fn: async () => {
const jsxConditional = getIsland("JsxConditional.tsx");
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.island(jsxConditional, "JsxConditional", JsxConditional)
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -450,10 +410,7 @@ Deno.test({
Deno.test({
name: "islands - revive DOM attributes",
fn: async () => {
const jsxConditional = getIsland("JsxConditional.tsx");
const app = testApp()
.island(jsxConditional, "JsxConditional", JsxConditional)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -523,12 +480,7 @@ Deno.test({
Deno.test({
name: "islands - revive island with fn inside",
fn: async () => {
const fragmentIsland = getIsland("FragmentIsland.tsx");
const fnIsland = getIsland("FnIsland.tsx");
const app = testApp()
.island(fragmentIsland, "FragmentIsland", FragmentIsland)
.island(fnIsland, "FnIsland", FnIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -552,10 +504,7 @@ Deno.test({
Deno.test({
name: "islands - escape props",
fn: async () => {
const escapeIsland = getIsland("EscapeIsland.tsx");
const app = testApp()
.island(escapeIsland, "EscapeIsland", EscapeIsland)
.get("/", (ctx) => {
return ctx.render(
<Doc>
@@ -648,10 +597,7 @@ Deno.test({
Deno.test({
name: "islands - stub Node 'process.env'",
fn: async () => {
const nodeProcess = getIsland("NodeProcess.tsx");
const app = testApp()
.island(nodeProcess, "NodeProcess", NodeProcess)
.get("/", (ctx) =>
ctx.render(
<Doc>
@@ -675,10 +621,7 @@ Deno.test({
Deno.test({
name: "islands - in base path",
fn: async () => {
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp({ basePath: "/foo" })
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) =>
ctx.render(
<Doc>
@@ -699,10 +642,7 @@ Deno.test({
Deno.test({
name: "islands - preserve f-* attributes",
fn: async () => {
const freshAttrs = getIsland("FreshAttrs.tsx");
const app = testApp()
.island(freshAttrs, "FreshAttrs", FreshAttrs)
.get("/", (ctx) =>
ctx.render(
<Doc>
@@ -728,16 +668,7 @@ Deno.test({
Deno.test({
name: "fsRoutes - load islands from group folder",
fn: async () => {
const app = testApp();
await fsRoutes(app, {
dir: path.join(
import.meta.dirname!,
"fixture_island_groups",
),
loadIsland: (path) => import("./fixture_island_groups/islands/" + path),
loadRoute: (path) => import("./fixture_island_groups/routes/" + path),
});
const app = testGroupApp();
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
@@ -752,13 +683,34 @@ Deno.test({
},
});
Deno.test({
name: "fsRoutes - load islands from group folder with same name",
fn: async () => {
const app = testGroupApp();
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/both`, { waitUntil: "load" });
await page.locator(".ready").wait();
// Page would error here
let text = await page
.locator<HTMLDivElement>("#foo.ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
text = await page
.locator<HTMLDivElement>("#foo-both.ready")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
});
},
});
Deno.test({
name: "islands - adds preload HTTP headers",
fn: async () => {
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp()
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) =>
ctx.render(
<Doc>
@@ -771,6 +723,7 @@ Deno.test({
await res.body?.cancel();
const link = res.headers.get("Link");
expect(link).toMatch(
/<\/_fresh\/js\/[a-zA-Z0-9]+\/fresh-runtime\.js>; rel="modulepreload"; as="script", <\/_fresh\/js\/[a-zA-Z0-9]+\/SelfCounter\.js>; rel="modulepreload"; as="script"/,
);
+6 -16
View File
@@ -1,14 +1,13 @@
import { App, staticFiles } from "fresh";
import { Partial } from "fresh/runtime";
import {
allIslandApp,
ALL_ISLAND_DIR,
assertMetaContent,
assertNotSelector,
buildProd,
charset,
Doc,
favicon,
getIsland,
parseHtml,
waitFor,
waitForText,
@@ -21,29 +20,20 @@ import { FakeServer } from "../src/test_utils.ts";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
import * as path from "@std/path";
import { getBuildCache, setBuildCache } from "../src/app.ts";
import { retry } from "@std/async/retry";
const loremIpsum = await Deno.readTextFile(
path.join(import.meta.dirname!, "lorem_ipsum.txt"),
);
await buildProd(allIslandApp);
const applyBuildCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
function testApp<T>(): App<T> {
const selfCounter = getIsland("SelfCounter.tsx");
const partialInIsland = getIsland("PartialInIsland.tsx");
const jsonIsland = getIsland("JsonIsland.tsx");
const optOutPartialLink = getIsland("OptOutPartialLink.tsx");
const app = new App<T>()
.island(selfCounter, "SelfCounter", SelfCounter)
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.island(optOutPartialLink, "OptOutPartialLink", OptOutPartialLink)
.use(staticFiles());
setBuildCache(app, getBuildCache(allIslandApp));
applyBuildCache<T>(app);
return app;
}
@@ -305,7 +295,7 @@ Deno.test({
);
});
await buildProd(app);
applyBuildCache(app);
const server = new FakeServer(app.handler());
let checked = false;
try {
@@ -452,7 +442,7 @@ Deno.test({
await page.locator(".partial-update").wait();
const doc = parseHtml(await page.content());
const raw = JSON.parse(doc.querySelector("pre")?.textContent!);
const raw = JSON.parse(doc.querySelector("pre")!.textContent!);
expect(raw).toEqual({ foo: 123 });
assertNotSelector(doc, ".output");
+34 -80
View File
@@ -1,32 +1,13 @@
import { App, getIslandRegistry, setBuildCache } from "../src/app.ts";
import type { App } from "../src/app.ts";
import { launch, type Page } from "@astral/astral";
import * as colors from "@std/fmt/colors";
import { DOMParser, HTMLElement } from "linkedom";
import { Builder } from "../src/dev/builder.ts";
import { Builder, type BuildOptions } from "../src/dev/builder.ts";
import { TextLineStream } from "@std/streams/text-line-stream";
import * as path from "@std/path";
import type { ComponentChildren } from "preact";
import { expect } from "@std/expect";
import { ProdBuildCache } from "../src/build_cache.ts";
import { Counter } from "./fixtures_islands/Counter.tsx";
import { CounterWithSlots } from "./fixtures_islands/CounterWithSlots.tsx";
import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
import { FnIsland } from "./fixtures_islands/FnIsland.tsx";
import { FragmentIsland } from "./fixtures_islands/FragmentIsland.tsx";
import { IslandInIsland } from "./fixtures_islands/IslandInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { JsxChildrenIsland } from "./fixtures_islands/JsxChildrenIsland.tsx";
import { JsxConditional } from "./fixtures_islands/JsxConditional.tsx";
import { JsxIsland } from "./fixtures_islands/JsxIsland.tsx";
import { NullIsland } from "./fixtures_islands/NullIsland.tsx";
import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
import { PassThrough } from "./fixtures_islands/PassThrough.tsx";
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
import { Multiple1, Multiple2 } from "./fixtures_islands/Multiple.tsx";
import { Foo } from "./fixture_island_groups/routes/foo/(_islands)/Foo.tsx";
import { NodeProcess } from "./fixtures_islands/NodeProcess.tsx";
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
import { OptOutPartialLink } from "./fixtures_islands/OptOutPartialLink.tsx";
import { mergeReadableStreams } from "@std/streams";
const browser = await launch({
args: [
@@ -35,17 +16,9 @@ const browser = await launch({
? ["--no-sandbox"]
: []),
],
headless: true,
headless: Deno.env.get("HEADLESS") !== "false",
});
export function getIsland(pathname: string) {
return path.join(
import.meta.dirname!,
"fixtures_islands",
pathname,
);
}
export const charset = <meta charset="utf-8" />;
export const favicon = (
@@ -71,17 +44,21 @@ export function Doc(props: { children?: ComponentChildren; title?: string }) {
);
}
export async function buildProd(app: App<unknown>) {
export const ALL_ISLAND_DIR = path.join(
import.meta.dirname!,
"fixtures_islands",
);
export const ISLAND_GROUP_DIR = path.join(
import.meta.dirname!,
"fixture_island_groups",
);
export async function buildProd(
options: Omit<BuildOptions, "outDir">,
): Promise<<T>(app: App<T>) => void> {
const outDir = await Deno.makeTempDir();
// FIXME: Sharing build output path is weird
app.config.build.outDir = outDir;
const builder = new Builder({});
await builder.build(app);
const cache = await ProdBuildCache.fromSnapshot(
app.config,
getIslandRegistry(app).size,
);
setBuildCache(app, cache);
const builder = new Builder({ outDir, ...options });
return await builder.build({ mode: "production", snapshot: "memory" });
}
export async function withBrowserApp(
@@ -128,15 +105,21 @@ export async function withChildProcessServer(
args: ["task", task],
stdin: "null",
stdout: "piped",
stderr: "inherit",
stderr: "piped",
cwd: dir,
signal: aborter.signal,
}).spawn();
const lines: ReadableStream<string> = cp.stdout
const linesStdout: ReadableStream<string> = cp.stdout
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
const linesStderr: ReadableStream<string> = cp.stderr
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
const lines = mergeReadableStreams(linesStdout, linesStderr);
const output: string[] = [];
let address = "";
let found = false;
@@ -348,40 +331,11 @@ export function getStdOutput(
return { stdout, stderr };
}
export const allIslandApp = new App()
.island(getIsland("Counter.tsx"), "Counter", Counter)
.island(
getIsland("CounterWithSlots.tsx"),
"CounterWithSlots",
CounterWithSlots,
)
.island(getIsland("EscapeIsland.tsx"), "EscapeIsland", EscapeIsland)
.island(getIsland("FnIsland.tsx"), "FnIsland", FnIsland)
.island(getIsland("FragmentIsland.tsx"), "FragmentIsland", FragmentIsland)
.island(getIsland("IslandInIsland.tsx"), "IslandInIsland", IslandInIsland)
.island(getIsland("JsonIsland.tsx"), "JsonIsland", JsonIsland)
.island(
getIsland("JsxChildrenIsland.tsx"),
"JsxChildrenIsland",
JsxChildrenIsland,
)
.island(getIsland("JsxConditional.tsx"), "JsxConditional", JsxConditional)
.island(getIsland("JsxIsland.tsx"), "JsxIsland", JsxIsland)
.island(getIsland("Multiple.tsx"), "Multiple1", Multiple1)
.island(getIsland("Multiple.tsx"), "Multiple2", Multiple2)
.island(getIsland("NullIsland.tsx"), "NullIsland", NullIsland)
.island(getIsland("PartialInIsland.tsx"), "PartialInIsland", PartialInIsland)
.island(getIsland("PassThrough.tsx"), "PassThrough", PassThrough)
.island(getIsland("SelfCounter.tsx"), "SelfCounter", SelfCounter)
.island(getIsland("NodeProcess.tsx"), "NodeProcess", NodeProcess)
.island(getIsland("FreshAttrs.tsx"), "FreshAttrs", FreshAttrs)
.island(
getIsland("OptOutPartialLink.tsx"),
"OptOutPartialLink",
OptOutPartialLink,
)
.island(
getIsland("../fixture_island_groups/routes/foo/(_islands)/Foo.tsx"),
"Foo",
Foo,
);
const ISLAND_FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures_islands");
const allIslandBuilder = new Builder({});
for await (const entry of Deno.readDirSync(ISLAND_FIXTURE_DIR)) {
if (entry.name.endsWith(".json")) continue;
const spec = path.join(ISLAND_FIXTURE_DIR, entry.name);
allIslandBuilder.registerIsland(spec);
}
+4
View File
@@ -135,6 +135,10 @@ export async function updateProject(dir: string) {
) {
tasks.check = "deno fmt --check && deno lint && deno check";
}
if (tasks.preview === "deno run -A main.ts") {
tasks.preview = "deno serve -A _fresh/server.js";
}
}
});
+1 -1
View File
@@ -132,7 +132,7 @@ Deno.test("update - 1.x project deno.json tasks + lock", async () => {
.toEqual({
build: "deno run -A dev.ts build",
check: "deno fmt --check && deno lint && deno check",
preview: "deno run -A main.ts",
preview: "deno serve -A _fresh/server.js",
start: "deno run -A --watch=static/,routes/ dev.ts",
update: "deno run -A -r jsr:@fresh/update .",
});
+1 -1
View File
@@ -2,6 +2,6 @@
"tasks": {
"start": "deno run -A --watch=static/,routes/,../src,../docs dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno run -A main.ts"
"preview": "deno serve -A _fresh/server.js"
}
}
+3 -4
View File
@@ -1,14 +1,13 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import { Builder } from "fresh/dev";
import { app } from "./main.ts";
import { tailwind } from "@fresh/plugin-tailwind";
const builder = new Builder({ target: "safari12" });
tailwind(builder, app);
tailwind(builder);
if (Deno.args.includes("build")) {
await builder.build(app);
await builder.build();
} else {
await builder.listen(app);
await builder.listen(() => import("./main.ts"));
}
+4 -12
View File
@@ -1,14 +1,6 @@
import { App, fsRoutes, staticFiles, trailingSlashes } from "fresh";
import { App, staticFiles, trailingSlashes } from "fresh";
export const app = new App({ root: import.meta.url })
export const app = new App()
.use(staticFiles())
.use(trailingSlashes("never"));
await fsRoutes(app, {
loadIsland: (path) => import(`./islands/${path}`),
loadRoute: (path) => import(`./routes/${path}`),
});
if (import.meta.main) {
await app.listen();
}
.use(trailingSlashes("never"))
.fsRoutes();
+3 -1
View File
@@ -4,7 +4,9 @@ import { buildProd, withBrowserApp } from "../tests/test_utils.tsx";
import { expect } from "@std/expect";
import { retry } from "@std/async/retry";
await buildProd(app);
const applyCache = await buildProd({ root: import.meta.dirname! });
applyCache(app);
const handler = app.handler();
Deno.test("CORS should not set on GET /fresh-badge.svg", async () => {