@pracht/cli 1.1.4 → 1.2.0
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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/bin/pracht.js +1 -40
- package/dist/build-BzQAQjuy.mjs +212 -0
- package/dist/build-metadata-IABPFFKk.mjs +49 -0
- package/dist/dev-BCFe3g38.mjs +25 -0
- package/dist/doctor-BcoqyBk-.mjs +25 -0
- package/dist/generate-BXkePCIx.mjs +404 -0
- package/dist/index.mjs +41 -0
- package/dist/inspect-DIjjCfs3.mjs +128 -0
- package/dist/manifest-DGq1n5LT.mjs +99 -0
- package/dist/project-DydjqBoS.mjs +155 -0
- package/dist/verification-Dfl3X4Zo.mjs +372 -0
- package/dist/verify-CObGxWtW.mjs +31 -0
- package/package.json +7 -2
- package/{lib/build-metadata.js → src/build-metadata.ts} +22 -9
- package/{lib/build-shared.js → src/build-shared.ts} +39 -13
- package/src/commands/build.ts +132 -0
- package/src/commands/dev.ts +27 -0
- package/src/commands/doctor.ts +33 -0
- package/{lib/commands/generate.js → src/commands/generate.ts} +178 -72
- package/{lib/commands/inspect.js → src/commands/inspect.ts} +81 -30
- package/src/commands/verify.ts +37 -0
- package/{lib/constants.js → src/constants.ts} +12 -2
- package/src/index.ts +21 -0
- package/{lib/manifest.js → src/manifest.ts} +32 -13
- package/{lib/project.js → src/project.ts} +48 -20
- package/src/utils.ts +72 -0
- package/{lib/verification.js → src/verification.ts} +103 -34
- package/tsdown.config.ts +8 -0
- package/lib/cli.js +0 -164
- package/lib/commands/build.js +0 -120
- package/lib/commands/dev.js +0 -16
- package/lib/commands/doctor.js +0 -21
- package/lib/commands/verify.js +0 -21
|
@@ -5,7 +5,14 @@ import { DEFAULT_SECURITY_HEADERS, VERSION } from "./constants.js";
|
|
|
5
5
|
|
|
6
6
|
const ROUTE_STATE_REQUEST_HEADER = "x-pracht-route-state-request";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
interface HeaderSettable {
|
|
9
|
+
setHeader(key: string, value: string): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function setDefaultSecurityHeaders(
|
|
13
|
+
res: HeaderSettable,
|
|
14
|
+
headers: Record<string, string> = {},
|
|
15
|
+
): void {
|
|
9
16
|
for (const [key, value] of Object.entries({
|
|
10
17
|
...DEFAULT_SECURITY_HEADERS,
|
|
11
18
|
...headers,
|
|
@@ -14,14 +21,23 @@ export function setDefaultSecurityHeaders(res, headers = {}) {
|
|
|
14
21
|
}
|
|
15
22
|
}
|
|
16
23
|
|
|
24
|
+
interface VercelBuildOutputOptions {
|
|
25
|
+
functionName?: string;
|
|
26
|
+
headersManifest?: Record<string, Record<string, string>>;
|
|
27
|
+
isgRoutes: string[];
|
|
28
|
+
regions?: string[];
|
|
29
|
+
root: string;
|
|
30
|
+
staticRoutes: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
17
33
|
export function writeVercelBuildOutput({
|
|
18
34
|
functionName,
|
|
19
35
|
headersManifest = {},
|
|
36
|
+
isgRoutes,
|
|
20
37
|
regions,
|
|
21
38
|
root,
|
|
22
39
|
staticRoutes,
|
|
23
|
-
|
|
24
|
-
}) {
|
|
40
|
+
}: VercelBuildOutputOptions): string {
|
|
25
41
|
const outputDir = join(root, ".vercel/output");
|
|
26
42
|
const staticDir = join(outputDir, "static");
|
|
27
43
|
const functionDir = join(outputDir, "functions", `${functionName || "render"}.func`);
|
|
@@ -49,9 +65,19 @@ export function writeVercelBuildOutput({
|
|
|
49
65
|
return ".vercel/output";
|
|
50
66
|
}
|
|
51
67
|
|
|
52
|
-
function createVercelOutputConfig({
|
|
68
|
+
function createVercelOutputConfig({
|
|
69
|
+
functionName,
|
|
70
|
+
headersManifest,
|
|
71
|
+
staticRoutes,
|
|
72
|
+
isgRoutes,
|
|
73
|
+
}: {
|
|
74
|
+
functionName?: string;
|
|
75
|
+
headersManifest: Record<string, Record<string, string>>;
|
|
76
|
+
isgRoutes: string[];
|
|
77
|
+
staticRoutes: string[];
|
|
78
|
+
}): Record<string, unknown> {
|
|
53
79
|
const target = `/${functionName || "render"}`;
|
|
54
|
-
const routes = [
|
|
80
|
+
const routes: Record<string, unknown>[] = [
|
|
55
81
|
{
|
|
56
82
|
dest: target,
|
|
57
83
|
has: [{ type: "header", key: ROUTE_STATE_REQUEST_HEADER, value: "1" }],
|
|
@@ -76,7 +102,7 @@ function createVercelOutputConfig({ functionName, headersManifest, staticRoutes,
|
|
|
76
102
|
routes.push({ handle: "filesystem" });
|
|
77
103
|
routes.push({ dest: target, src: "/(.*)" });
|
|
78
104
|
|
|
79
|
-
const headers = [
|
|
105
|
+
const headers: Record<string, unknown>[] = [
|
|
80
106
|
{
|
|
81
107
|
headers: [
|
|
82
108
|
{
|
|
@@ -111,8 +137,8 @@ function createVercelOutputConfig({ functionName, headersManifest, staticRoutes,
|
|
|
111
137
|
};
|
|
112
138
|
}
|
|
113
139
|
|
|
114
|
-
function createVercelFunctionConfig({ regions }) {
|
|
115
|
-
const config = {
|
|
140
|
+
function createVercelFunctionConfig({ regions }: { regions?: string[] }): Record<string, unknown> {
|
|
141
|
+
const config: Record<string, unknown> = {
|
|
116
142
|
entrypoint: "server.js",
|
|
117
143
|
runtime: "edge",
|
|
118
144
|
};
|
|
@@ -124,11 +150,11 @@ function createVercelFunctionConfig({ regions }) {
|
|
|
124
150
|
return config;
|
|
125
151
|
}
|
|
126
152
|
|
|
127
|
-
function sortStaticRoutes(routes) {
|
|
153
|
+
function sortStaticRoutes(routes: string[]): string[] {
|
|
128
154
|
return [...new Set(routes)].sort((left, right) => right.length - left.length);
|
|
129
155
|
}
|
|
130
156
|
|
|
131
|
-
function routeToRouteExpression(route) {
|
|
157
|
+
function routeToRouteExpression(route: string): string {
|
|
132
158
|
if (route === "/") {
|
|
133
159
|
return "^/$";
|
|
134
160
|
}
|
|
@@ -136,7 +162,7 @@ function routeToRouteExpression(route) {
|
|
|
136
162
|
return `^${escapeRegex(route)}/?$`;
|
|
137
163
|
}
|
|
138
164
|
|
|
139
|
-
function routeToStaticHtmlPath(route) {
|
|
165
|
+
function routeToStaticHtmlPath(route: string): string {
|
|
140
166
|
if (route === "/") {
|
|
141
167
|
return "/index.html";
|
|
142
168
|
}
|
|
@@ -144,10 +170,10 @@ function routeToStaticHtmlPath(route) {
|
|
|
144
170
|
return `${route}/index.html`;
|
|
145
171
|
}
|
|
146
172
|
|
|
147
|
-
function routeToHeaderSource(route) {
|
|
173
|
+
function routeToHeaderSource(route: string): string {
|
|
148
174
|
return route === "/" ? "/" : route;
|
|
149
175
|
}
|
|
150
176
|
|
|
151
|
-
function escapeRegex(value) {
|
|
177
|
+
function escapeRegex(value: string): string {
|
|
152
178
|
return value.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
|
|
153
179
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { defineCommand } from "citty";
|
|
5
|
+
import { build as viteBuild } from "vite";
|
|
6
|
+
|
|
7
|
+
import { readClientBuildAssets } from "../build-metadata.js";
|
|
8
|
+
import { writeVercelBuildOutput } from "../build-shared.js";
|
|
9
|
+
|
|
10
|
+
export default defineCommand({
|
|
11
|
+
meta: {
|
|
12
|
+
name: "build",
|
|
13
|
+
description: "Production build (client + server)",
|
|
14
|
+
},
|
|
15
|
+
async run() {
|
|
16
|
+
const root = process.cwd();
|
|
17
|
+
|
|
18
|
+
console.log("\n Building client...\n");
|
|
19
|
+
await viteBuild({
|
|
20
|
+
root,
|
|
21
|
+
build: {
|
|
22
|
+
outDir: "dist",
|
|
23
|
+
manifest: true,
|
|
24
|
+
rollupOptions: {
|
|
25
|
+
input: "virtual:pracht/client",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.log("\n Building server...\n");
|
|
31
|
+
await viteBuild({
|
|
32
|
+
root,
|
|
33
|
+
build: {
|
|
34
|
+
outDir: "dist/server",
|
|
35
|
+
ssr: "virtual:pracht/server",
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const serverEntry = resolve(root, "dist/server/server.js");
|
|
40
|
+
let clientDir: string;
|
|
41
|
+
if (existsSync(resolve(root, "dist/client/.vite/manifest.json"))) {
|
|
42
|
+
clientDir = resolve(root, "dist/client");
|
|
43
|
+
} else {
|
|
44
|
+
clientDir = resolve(root, "dist/client");
|
|
45
|
+
const distRoot = resolve(root, "dist");
|
|
46
|
+
mkdirSync(clientDir, { recursive: true });
|
|
47
|
+
for (const entry of readdirSync(distRoot)) {
|
|
48
|
+
if (entry === "server" || entry === "client") continue;
|
|
49
|
+
const sourcePath = join(distRoot, entry);
|
|
50
|
+
const destinationPath = join(clientDir, entry);
|
|
51
|
+
cpSync(sourcePath, destinationPath, { recursive: true });
|
|
52
|
+
rmSync(sourcePath, { force: true, recursive: true });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (existsSync(serverEntry)) {
|
|
57
|
+
const serverMod = await import(serverEntry);
|
|
58
|
+
const { prerenderApp } = serverMod;
|
|
59
|
+
const { clientEntryUrl, cssManifest, jsManifest } = readClientBuildAssets(root);
|
|
60
|
+
|
|
61
|
+
const { pages, isgManifest } = await prerenderApp({
|
|
62
|
+
app: serverMod.resolvedApp,
|
|
63
|
+
clientEntryUrl: clientEntryUrl ?? undefined,
|
|
64
|
+
cssManifest,
|
|
65
|
+
jsManifest,
|
|
66
|
+
registry: serverMod.registry,
|
|
67
|
+
withISGManifest: true,
|
|
68
|
+
});
|
|
69
|
+
const headersManifest: Record<string, Record<string, string>> = Object.fromEntries(
|
|
70
|
+
pages.map((page: { path: string; headers?: Record<string, string> }) => [
|
|
71
|
+
page.path,
|
|
72
|
+
page.headers ?? {},
|
|
73
|
+
]),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (pages.length > 0) {
|
|
77
|
+
console.log(`\n Prerendering ${pages.length} SSG/ISG route(s)...\n`);
|
|
78
|
+
for (const page of pages) {
|
|
79
|
+
const filePath =
|
|
80
|
+
page.path === "/"
|
|
81
|
+
? join(clientDir, "index.html")
|
|
82
|
+
: join(clientDir, page.path, "index.html");
|
|
83
|
+
|
|
84
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
85
|
+
writeFileSync(filePath, page.html, "utf-8");
|
|
86
|
+
console.log(` ${page.path} → ${filePath.replace(root + "/", "")}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (Object.keys(headersManifest).length > 0) {
|
|
91
|
+
const headersManifestJson = `${JSON.stringify(headersManifest, null, 2)}\n`;
|
|
92
|
+
writeFileSync(
|
|
93
|
+
resolve(root, "dist/server/headers-manifest.json"),
|
|
94
|
+
headersManifestJson,
|
|
95
|
+
"utf-8",
|
|
96
|
+
);
|
|
97
|
+
mkdirSync(resolve(clientDir, "_pracht"), { recursive: true });
|
|
98
|
+
writeFileSync(resolve(clientDir, "_pracht/headers.json"), headersManifestJson, "utf-8");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (Object.keys(isgManifest).length > 0) {
|
|
102
|
+
const isgManifestPath = resolve(root, "dist/server/isg-manifest.json");
|
|
103
|
+
writeFileSync(isgManifestPath, JSON.stringify(isgManifest, null, 2), "utf-8");
|
|
104
|
+
console.log(
|
|
105
|
+
`\n ISG manifest → dist/server/isg-manifest.json (${Object.keys(isgManifest).length} route(s))\n`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (serverMod.buildTarget === "cloudflare") {
|
|
110
|
+
console.log("\n Cloudflare worker → dist/server/server.js\n");
|
|
111
|
+
console.log(" Deploy with: wrangler deploy\n");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (serverMod.buildTarget === "vercel") {
|
|
115
|
+
const outputPath = writeVercelBuildOutput({
|
|
116
|
+
functionName: serverMod.vercelFunctionName,
|
|
117
|
+
isgRoutes: Object.keys(isgManifest),
|
|
118
|
+
headersManifest,
|
|
119
|
+
regions: serverMod.vercelRegions,
|
|
120
|
+
root,
|
|
121
|
+
staticRoutes: pages
|
|
122
|
+
.map((page: { path: string }) => page.path)
|
|
123
|
+
.filter((path: string) => !(path in isgManifest)),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
console.log(`\n Vercel build output → ${outputPath}\n`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
console.log("\n Build complete.\n");
|
|
131
|
+
},
|
|
132
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
import { createServer } from "vite";
|
|
3
|
+
|
|
4
|
+
export default defineCommand({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "dev",
|
|
7
|
+
description: "Start development server with HMR",
|
|
8
|
+
},
|
|
9
|
+
args: {
|
|
10
|
+
port: {
|
|
11
|
+
type: "positional",
|
|
12
|
+
description: "Port number",
|
|
13
|
+
required: false,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
async run({ args }) {
|
|
17
|
+
const port = parseInt(process.env.PORT || args.port || "3000", 10);
|
|
18
|
+
|
|
19
|
+
const server = await createServer({
|
|
20
|
+
root: process.cwd(),
|
|
21
|
+
server: { port },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
await server.listen();
|
|
25
|
+
server.printUrls();
|
|
26
|
+
},
|
|
27
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineCommand } from "citty";
|
|
2
|
+
|
|
3
|
+
import { runDoctor } from "../verification.js";
|
|
4
|
+
|
|
5
|
+
export default defineCommand({
|
|
6
|
+
meta: {
|
|
7
|
+
name: "doctor",
|
|
8
|
+
description: "Validate app wiring",
|
|
9
|
+
},
|
|
10
|
+
args: {
|
|
11
|
+
json: {
|
|
12
|
+
type: "boolean",
|
|
13
|
+
description: "Output as JSON",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
async run({ args }) {
|
|
17
|
+
const report = runDoctor(process.cwd());
|
|
18
|
+
|
|
19
|
+
if (args.json) {
|
|
20
|
+
console.log(JSON.stringify(report, null, 2));
|
|
21
|
+
} else {
|
|
22
|
+
console.log(`Pracht doctor (${report.mode} mode)`);
|
|
23
|
+
for (const check of report.checks) {
|
|
24
|
+
console.log(`${check.status.toUpperCase().padEnd(5)} ${check.message}`);
|
|
25
|
+
}
|
|
26
|
+
console.log(report.ok ? "\nNo blocking issues found." : "\nBlocking issues found.");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!report.ok) {
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
});
|