Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@babel/core": "^7.29.0",
"@babel/traverse": "^7.29.0",
"@babel/types": "^7.29.0",
"@solidjs/file-routes": "^0.1.0",
"@solidjs/meta": "0.30.0-next.0",
"@solidjs/router": "0.17.0-next.6",
"@solidjs/web": "2.0.0-beta.21",
Expand Down
116 changes: 22 additions & 94 deletions packages/start/src/config/fs-router.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,23 @@
import type { StaticExportEntry } from "oxc-parser";

import {
analyzeModule,
BaseFileSystemRouter,
cleanPath,
getExportName,
getLocalExportName,
PageFileSystemRouter,
type FileSystemRouterConfig,
} from "./fs-routes/router.ts";

export class SolidStartClientFileRouter extends BaseFileSystemRouter {
toPath(src: string) {
const routePath = cleanPath(src, this.config)
// remove the initial slash
.slice(1)
.replace(/index$/, "")
.replace(/\[([^/]+)\]/g, (_, m) => {
if (m.length > 3 && m.startsWith("...")) {
return `*${m.slice(3)}`;
}
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
return `:${m.slice(1, -1)}?`;
}
return `:${m}`;
});

return routePath?.length > 0 ? `/${routePath}` : "/";
}

toRoute(src: string) {
const path = this.toPath(src);
type ModuleRef,
type RouteManifestEntry,
} from "@solidjs/file-routes";

if (src.endsWith(".md") || src.endsWith(".mdx")) {
return {
page: true,
$component: {
src: src,
pick: ["$css"],
},
$$route: undefined,
path,
// filePath: src
};
}

const exports = analyzeModule(src);
const exportNames = exports.map(getExportName);
const localExportNames = exports.map(getLocalExportName).filter(name => name !== undefined);
const hasDefault = exportNames.includes("default");
const hasRouteConfig = exportNames.includes("route");
if (hasDefault) {
return {
page: true,
$component: {
src: src,
pick: [...localExportNames.filter(name => name !== "route"), "default", "$css"],
},
$$route: hasRouteConfig
? {
src: src,
pick: ["route"],
}
: undefined,
path,
// filePath: src
};
}
}
}
/**
* The client router is the plain page convention from `@solidjs/file-routes`:
* default export = page, optional `route` config export, md/mdx pages.
*/
export { PageFileSystemRouter as SolidStartClientFileRouter };

const HTTP_METHODS = ["HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];

function getExportName(entry: StaticExportEntry) {
return entry.exportName.name ?? "default";
}

function getLocalExportName(entry: StaticExportEntry) {
const name = getExportName(entry);
if (name === "default") return;
return name === (entry.localName.name ?? entry.importName.name ?? name) ? name : undefined;
}

function createHTTPHandlers(src: string, exports: readonly string[]) {
const handlers: Record<string, any> = {};
const handlers: Record<string, ModuleRef> = {};
for (const exp of exports) {
if (HTTP_METHODS.includes(exp)) {
handlers[`$${exp}`] = {
Expand All @@ -99,33 +36,24 @@ function createHTTPHandlers(src: string, exports: readonly string[]) {
return handlers;
}

export class SolidStartServerFileRouter extends BaseFileSystemRouter {
/**
* SolidStart's server convention on top of the shared page convention:
* modules may additionally export `GET`, `POST`, etc. API handlers, and
* component refs are omitted entirely when SSR is disabled (`dataOnly`).
*/
export class SolidStartServerFileRouter extends PageFileSystemRouter {
declare config: FileSystemRouterConfig & { dataOnly?: boolean };

constructor(config: FileSystemRouterConfig & { dataOnly?: boolean }) {
super(config);
}

toPath(src: string) {
const routePath = cleanPath(src, this.config)
// remove the initial slash
.slice(1)
.replace(/index$/, "")
.replace(/\[([^/]+)\]/g, (_, m) => {
if (m.length > 3 && m.startsWith("...")) {
return `*${m.slice(3)}`;
}
if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
return `:${m.slice(1, -1)}?`;
}
return `:${m}`;
});
toRoute(src: string): RouteManifestEntry | undefined {
if (this.config.toRoute) return this.config.toRoute(src, this);

return routePath?.length > 0 ? `/${routePath}` : "/";
}

toRoute(src: string) {
const path = this.toPath(src);
if (path === undefined) return;

if (src.endsWith(".md") || src.endsWith(".mdx")) {
return {
page: true,
Expand Down
66 changes: 0 additions & 66 deletions packages/start/src/config/fs-routes/fs-watcher.ts

This file was deleted.

136 changes: 0 additions & 136 deletions packages/start/src/config/fs-routes/index.ts

This file was deleted.

Loading
Loading