Skip to content
Open
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
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
},
"typescript.tsdk": "node_modules/typescript/lib",
"editor.formatOnSave": true,
"editor.defaultFormatter": "oxc.oxc-vscode"
"editor.defaultFormatter": "oxc.oxc-vscode",
"[typescript]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
}
}
1 change: 0 additions & 1 deletion packages/start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@babel/traverse": "^7.28.3",
"@babel/types": "^7.28.5",
"@solidjs/meta": "^0.29.4",
"@tanstack/server-functions-plugin": "1.134.5",
"@types/babel__traverse": "^7.28.0",
"@types/micromatch": "^4.0.9",
"cookie-es": "^2.0.0",
Expand Down
43 changes: 4 additions & 39 deletions packages/start/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { TanStackServerFnPlugin } from "@tanstack/server-functions-plugin";
import { defu } from "defu";
import { globSync } from "node:fs";
import { extname, isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { normalizePath, type PluginOption } from "vite";
import { type PluginOption } from "vite";
import solid, { type Options as SolidOptions } from "vite-plugin-solid";

import { serverFunctionsPlugin } from "../directives/index.ts";
import { DEFAULT_EXTENSIONS, VIRTUAL_MODULES, VITE_ENVIRONMENTS } from "./constants.ts";
import { devServer } from "./dev-server.ts";
import { SolidStartClientFileRouter, SolidStartServerFileRouter } from "./fs-router.ts";
Expand Down Expand Up @@ -163,41 +161,8 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
},
}),
lazy(),
// Must be placed after fsRoutes, as treeShake will remove the
// server fn exports added in by this plugin
TanStackServerFnPlugin({
// This is the ID that will be available to look up and import
// our server function manifest and resolve its module
manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,
directive: "use server",
callers: [
{
envConsumer: "client",
envName: VITE_ENVIRONMENTS.client,
getRuntimeCode: () =>
`import { createServerReference } from "${normalizePath(
fileURLToPath(new URL("../server/server-runtime", import.meta.url))
)}"`,
replacer: opts => `createServerReference('${opts.functionId}')`,
},
{
envConsumer: "server",
envName: VITE_ENVIRONMENTS.server,
getRuntimeCode: () =>
`import { createServerReference } from '${normalizePath(
fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url))
)}'`,
replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`,
},
],
provider: {
envName: VITE_ENVIRONMENTS.server,
getRuntimeCode: () =>
`import { createServerReference } from '${normalizePath(
fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url))
)}'`,
replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`,
},
serverFunctionsPlugin({
manifest: VIRTUAL_MODULES.serverFnManifest,
}),
{
name: "solid-start:virtual-modules",
Expand Down
32 changes: 32 additions & 0 deletions packages/start/src/directives/bubble-function-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type * as babel from "@babel/core";
import * as t from "@babel/types";

export function bubbleFunctionDeclaration(path: babel.NodePath<t.FunctionDeclaration>): void {
const decl = path.node;
// Check if declaration is FunctionDeclaration
if (decl.id) {
const block = (path.findParent(current => current.isBlockStatement()) ||
path.scope.getProgramParent().path) as babel.NodePath<t.BlockStatement>;

const [tmp] = block.unshiftContainer(
"body",
t.variableDeclaration("const", [
t.variableDeclarator(
decl.id,
t.functionExpression(decl.id, decl.params, decl.body, decl.generator, decl.async),
),
]),
);
path.scope.registerDeclaration(tmp);
// tmp.skip();
if (path.parentPath.isExportNamedDeclaration()) {
path.parentPath.replaceWith(
t.exportNamedDeclaration(undefined, [t.exportSpecifier(decl.id, decl.id)]),
);
} else if (path.parentPath.isExportDefaultDeclaration()) {
path.replaceWith(decl.id);
} else {
path.remove();
}
}
}
56 changes: 56 additions & 0 deletions packages/start/src/directives/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from "node:path";
import * as babel from "@babel/core";
import { directivesPlugin, type StateContext } from "./plugin.ts";
import xxHash32 from "./xxhash32.ts";

export interface CompileResult {
valid: boolean;
code: string;
map: babel.BabelFileResult["map"];
}

export type CompileOptions = Omit<StateContext, "count" | "hash" | "imports">;

export async function compile(
id: string,
code: string,
options: CompileOptions,
): Promise<CompileResult> {
const context: StateContext = {
...options,
hash: xxHash32(id).toString(16),
count: 0,
imports: new Map(),
}
const pluginOption = [
directivesPlugin,
context,
];
const plugins: NonNullable<NonNullable<babel.TransformOptions["parserOpts"]>["plugins"]> = [
"jsx",
];
if (/\.[mc]?tsx?$/i.test(id)) {
plugins.push("typescript");
}
const result = await babel.transformAsync(code, {
plugins: [pluginOption],
parserOpts: {
plugins,
},
filename: path.basename(id),
ast: false,
sourceMaps: true,
configFile: false,
babelrc: false,
sourceFileName: id,
});

if (result) {
return {
valid: context.count > 0,
code: result.code || "",
map: result.map,
};
}
throw new Error("invariant");
}
22 changes: 22 additions & 0 deletions packages/start/src/directives/generate-unique-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type * as babel from "@babel/core";
import * as t from "@babel/types";

export function generateUniqueName(path: babel.NodePath, name: string): t.Identifier {
let uid: string;
let i = 1;
do {
uid = name + "_" + i;
i++;
} while (
path.scope.hasLabel(uid) ||
path.scope.hasBinding(uid) ||
path.scope.hasGlobal(uid) ||
path.scope.hasReference(uid)
);

const program = path.scope.getProgramParent();
program.references[uid] = true;
program.uids[uid] = true;

return t.identifier(uid);
}
39 changes: 39 additions & 0 deletions packages/start/src/directives/get-descriptive-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { NodePath } from "@babel/core";

export function getDescriptiveName(path: NodePath, defaultName: string): string {
let current: NodePath | null = path;
while (current) {
switch (current.node.type) {
case "FunctionDeclaration":
case "FunctionExpression": {
if (current.node.id) {
return current.node.id.name;
}
break;
}
case "VariableDeclarator": {
if (current.node.id.type === "Identifier") {
return current.node.id.name;
}
break;
}
case "ClassPrivateMethod":
case "ClassMethod":
case "ObjectMethod": {
switch (current.node.key.type) {
case "Identifier":
return current.node.key.name;
case "PrivateName":
return current.node.key.id.name;
default:
break;
}
break;
}
default:
break;
}
current = current.parentPath;
}
return defaultName;
}
34 changes: 34 additions & 0 deletions packages/start/src/directives/get-import-identifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type * as babel from "@babel/core";
import * as t from "@babel/types";
import { generateUniqueName } from "./generate-unique-name.ts";
import type { ImportDefinition } from "./types.ts";

export function getImportIdentifier(
imports: Map<string, t.Identifier>,
path: babel.NodePath,
registration: ImportDefinition,
): t.Identifier {
const name = registration.kind === "named" ? registration.name : "default";
const target = `${registration.source}[${name}]`;
const current = imports.get(target);
if (current) {
return current;
}
const programParent = path.scope.getProgramParent();
const uid = generateUniqueName(programParent.path, name);
programParent.registerDeclaration(
(programParent.path as babel.NodePath<t.Program>).unshiftContainer(
"body",
t.importDeclaration(
[
registration.kind === "named"
? t.importSpecifier(uid, t.identifier(registration.name))
: t.importDefaultSpecifier(uid),
],
t.stringLiteral(registration.source),
),
)[0],
);
imports.set(target, uid);
return uid;
}
14 changes: 14 additions & 0 deletions packages/start/src/directives/get-root-statement-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type * as babel from "@babel/core";
import * as t from "@babel/types";

export function getRootStatementPath(path: babel.NodePath): babel.NodePath {
let current = path.parentPath;
while (current) {
const next = current.parentPath;
if (next && t.isProgram(next.node)) {
return current;
}
current = next;
}
return path;
}
Loading
Loading