Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial typings fs-router folder #709

Merged
merged 16 commits into from
Feb 9, 2023
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
export default function fileRoutesImport({ types: t }) {
// @ts-ignore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why @ts-ignore has been added here?

Copy link
Member Author

@birkskyum birkskyum Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise it'll say:

Cannot find module '@babel/types/lib' or its corresponding type declarations.ts(2307)

Copy link

@aminya aminya Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't @types/babel__types have the types? I think by installing this package and importing things from @babe/types the issue would be resolved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have another look at this

import type * as BabelTypes from "@babel/types/lib";

type Path = {
node: { body: any; source: { value: string; }; specifiers: any[]; };
scope: { generateUidIdentifier: (arg0: string) => any; };
replaceWith: (arg0: any) => void;
insertBefore: (arg0: any) => void;
}

type State = {
namespaceSpec: any[];
}

export default function fileRoutesImport({ types: t }: { types: typeof BabelTypes }) {
return {
visitor: {
ImportDeclaration(path, state) {
ImportDeclaration(path: Path, state: State) {
if (path.node.source.value !== "solid-start") {
return;
}
Expand Down Expand Up @@ -29,12 +43,12 @@ export default function fileRoutesImport({ types: t }) {
}
},
Program: {
enter(_path, state) {
enter(_path: Path, state: State) {
state.namespaceSpec = [];
},
exit(path, state) {
exit(path: Path, state: State) {
const body = path.node.body;
let lastImportIndex;
let lastImportIndex: number;
if (state.namespaceSpec.length) {
for (let i = 0; i < body.length; i++) {
if (body[i].type === "ImportDeclaration") {
Expand All @@ -49,7 +63,7 @@ export default function fileRoutesImport({ types: t }) {
t.stringLiteral("solid-start/root/FileRoutes")
);
body.unshift(newImport);
lastImportIndex++;
lastImportIndex!++;
const namespaceId = path.scope.generateUidIdentifier(specifier.local.name);
const namespaceFix = t.variableDeclaration("const", [
t.variableDeclarator(
Expand All @@ -60,7 +74,7 @@ export default function fileRoutesImport({ types: t }) {
])
)
]);
body.splice(lastImportIndex + 1, 0, namespaceFix);
body.splice(lastImportIndex! + 1, 0, namespaceFix);
specifier.local.name = namespaceId.name;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/start/fs-router/manifest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// make asset lookup
import { posix } from "path";
import { toPath } from "./path-utils.js";
import { toPath } from "./path-utils.ts";

/**
* Output:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* @param removePathlessLayouts
* @returns
*/
export function toPath(id, removePathlessLayouts = true) {
export function toPath(id: string, removePathlessLayouts = true): string {
const idWithoutIndex = id.endsWith("/index") ? id.slice(0, -"index".length) : id;
return (
removePathlessLayouts ? idWithoutIndex.replace(/\/\([^)/]+\)/g, "") : idWithoutIndex
).replace(/\[([^\/]+)\]/g, (_, m) => {
).replace(/\[([^\/]+)\]/g, (_, m: string) => {
if (m.length > 3 && m.startsWith("...")) {
return `*${m.slice(3)}`;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/start/fs-router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import esbuild from "esbuild";
import fg from "fast-glob";
import fs from "fs";
import path, { join } from "path";
import { toPath } from "./path-utils.js";
import { toPath } from "./path-utils.ts";

const log = debug("solid-start");
const ROUTE_KEYS = ["component", "path", "data", "children"];
Expand Down
1 change: 1 addition & 0 deletions packages/start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"wait-on": "^6.0.1"
},
"devDependencies": {
"@babel/types": "^7.20.7",
"@cloudflare/workers-types": "^3.19.0",
"@solidjs/meta": "^0.28.2",
"@solidjs/router": "^0.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/start/vite/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { loadEnv, normalizePath } from "vite";
import inspect from "vite-plugin-inspect";
import solid from "vite-plugin-solid";
import printUrls from "../dev/print-routes.js";
import fileRoutesImport from "../fs-router/fileRoutesImport.js";
import fileRoutesImport from "../fs-router/fileRoutesImport.ts";
import { Router, stringifyApiRoutes, stringifyPageRoutes } from "../fs-router/router.js";
import routeData from "../server/routeData.js";
import routeDataHmr from "../server/routeDataHmr.js";
Expand Down
71 changes: 19 additions & 52 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.