@tschk/moonshine-cli 0.4.0 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -18
- package/bin/moonshine.ts +31 -17
- package/package.json +28 -14
- package/src/adopt/gaps.ts +33 -6
- package/src/adopt/plan.ts +77 -11
- package/src/adopt/project.ts +12 -2
- package/src/adopt/report.ts +2 -2
- package/src/adopt/scan.ts +18 -3
- package/src/adopt/templates.ts +157 -79
- package/src/adopt/types.ts +6 -3
- package/src/adopt.ts +15 -17
- package/src/build.ts +43 -3
- package/src/compile-graph.ts +6 -0
- package/src/compile-output.ts +1 -0
- package/src/compile.ts +72 -64
- package/src/config.ts +2 -1
- package/src/dev.ts +12 -1
- package/src/new.ts +193 -67
- package/src/preview-native.ts +147 -0
- package/src/preview-worker.ts +38 -0
- package/src/preview.ts +60 -13
package/src/preview.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { resolve } from "node:path";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
3
4
|
import { createBunServer } from "@tschk/moonshine-deploy-bun";
|
|
4
5
|
import { readManifest } from "@tschk/moonshine-compiler";
|
|
5
6
|
import {
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
13
14
|
type RouteArtifact,
|
|
14
15
|
} from "@tschk/moonshine-framework";
|
|
15
16
|
import { findConfig, loadConfig, type MoonshineCliConfig } from "./config.js";
|
|
17
|
+
import { startNativePreview } from "./preview-native";
|
|
16
18
|
|
|
17
19
|
export type PreviewHandle = {
|
|
18
20
|
url: URL;
|
|
@@ -32,6 +34,7 @@ function resolveManifest(
|
|
|
32
34
|
routes: manifest.routes.map((route: RouteArtifact) => ({
|
|
33
35
|
...route,
|
|
34
36
|
file: abs(route.file)!,
|
|
37
|
+
clientFile: abs(route.clientFile),
|
|
35
38
|
dataFile: abs(route.dataFile),
|
|
36
39
|
layouts: route.layouts?.map(abs).filter(Boolean) as string[] | undefined,
|
|
37
40
|
middleware: route.middleware?.map(abs).filter(Boolean) as
|
|
@@ -50,16 +53,46 @@ async function loadConfigMaybe(
|
|
|
50
53
|
|
|
51
54
|
async function resolveRenderer(
|
|
52
55
|
name: string | undefined,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
projectDir: string,
|
|
57
|
+
): Promise<Renderer> {
|
|
58
|
+
const [specifier, exported] =
|
|
59
|
+
name === "solid"
|
|
60
|
+
? (["@tschk/moonshine-solid", "solidRenderer"] as const)
|
|
61
|
+
: name === "crepus"
|
|
62
|
+
? (["@tschk/crepus-moonshine", "crepusRenderer"] as const)
|
|
63
|
+
: (["@tschk/moonshine-react", "reactRenderer"] as const);
|
|
64
|
+
function installedPath(base: string): string {
|
|
65
|
+
const directories =
|
|
66
|
+
createRequire(resolve(base, "package.json")).resolve.paths(specifier) ??
|
|
67
|
+
[];
|
|
68
|
+
if (
|
|
69
|
+
!directories.some((directory) =>
|
|
70
|
+
existsSync(join(directory, specifier, "package.json")),
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
throw new Error(`Renderer package ${specifier} is not installed`);
|
|
74
|
+
return Bun.resolveSync(specifier, base);
|
|
75
|
+
}
|
|
76
|
+
let path: string;
|
|
77
|
+
try {
|
|
78
|
+
path = installedPath(projectDir);
|
|
79
|
+
} catch {
|
|
80
|
+
try {
|
|
81
|
+
path = installedPath(import.meta.dir);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Renderer package ${specifier} is not installed; run bun add ${specifier} in ${projectDir}`,
|
|
85
|
+
{ cause: error },
|
|
86
|
+
);
|
|
87
|
+
}
|
|
62
88
|
}
|
|
89
|
+
const module = await import(path);
|
|
90
|
+
const renderer = module[exported];
|
|
91
|
+
if (typeof renderer?.render !== "function")
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Renderer package ${specifier} does not export ${exported}`,
|
|
94
|
+
);
|
|
95
|
+
return renderer as Renderer;
|
|
63
96
|
}
|
|
64
97
|
|
|
65
98
|
export async function startPreview(options: {
|
|
@@ -76,9 +109,15 @@ export async function startPreview(options: {
|
|
|
76
109
|
}
|
|
77
110
|
|
|
78
111
|
const raw = await readManifest(manifestPath);
|
|
79
|
-
|
|
112
|
+
if (raw.renderer === "native") {
|
|
113
|
+
return startNativePreview({ projectDir, manifestPath, port: options.port });
|
|
114
|
+
}
|
|
80
115
|
const config = await loadConfigMaybe(projectDir);
|
|
81
|
-
const
|
|
116
|
+
const manifest = resolveManifest(projectDir, raw);
|
|
117
|
+
const renderer = await resolveRenderer(
|
|
118
|
+
raw.renderer ?? config.renderer,
|
|
119
|
+
projectDir,
|
|
120
|
+
);
|
|
82
121
|
const staticDir = resolve(projectDir, ".moonshine", PUBLIC_DIR);
|
|
83
122
|
const serverBundlePath = resolve(
|
|
84
123
|
projectDir,
|
|
@@ -91,28 +130,36 @@ export async function startPreview(options: {
|
|
|
91
130
|
// that was never supposed to be loaded at runtime. Say which bundle failed
|
|
92
131
|
// and why, once, instead of making the user infer it from the wreckage.
|
|
93
132
|
let modules: Record<string, RouteModule> = {};
|
|
133
|
+
let requestScope: Parameters<typeof createRequestHandler>[0]["requestScope"];
|
|
94
134
|
try {
|
|
95
135
|
const bundle = (await import(serverBundlePath)) as {
|
|
96
136
|
modules?: Record<string, RouteModule>;
|
|
137
|
+
requestScope?: typeof requestScope;
|
|
97
138
|
};
|
|
98
139
|
modules = bundle.modules ?? {};
|
|
140
|
+
requestScope = bundle.requestScope;
|
|
99
141
|
} catch (error) {
|
|
142
|
+
if (raw.requestContext) throw error;
|
|
100
143
|
console.warn(
|
|
101
144
|
`moonshine preview: could not load ${serverBundlePath}\n` +
|
|
102
145
|
` ${error instanceof Error ? error.message : String(error)}\n` +
|
|
103
146
|
` Server-rendered routes will fail. Run \`moonshine build\` first.`,
|
|
104
147
|
);
|
|
105
148
|
}
|
|
149
|
+
if (raw.requestContext && typeof requestScope !== "function")
|
|
150
|
+
throw new Error(
|
|
151
|
+
"Request context runner missing from server bundle; rebuild the project",
|
|
152
|
+
);
|
|
106
153
|
const fetch = createRequestHandler({
|
|
107
154
|
manifest,
|
|
108
155
|
modules,
|
|
109
156
|
renderer,
|
|
157
|
+
...(raw.requestContext ? { requestScope } : {}),
|
|
110
158
|
staticDir,
|
|
111
159
|
});
|
|
112
160
|
const server = createBunServer({
|
|
113
161
|
fetch,
|
|
114
162
|
port: options.port ?? 0,
|
|
115
|
-
staticDir,
|
|
116
163
|
});
|
|
117
164
|
|
|
118
165
|
return {
|