ludicord 2.2.5 → 3.0.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/README.md +8 -4
- package/dist/activity/game-loop.d.ts +16 -0
- package/dist/activity/game-loop.js +33 -0
- package/dist/activity/index.d.ts +12 -0
- package/dist/activity/index.js +6 -0
- package/dist/activity/lifecycle.d.ts +11 -0
- package/dist/activity/lifecycle.js +41 -0
- package/dist/activity/presence.d.ts +10 -0
- package/dist/activity/presence.js +15 -0
- package/dist/activity/query.d.ts +21 -0
- package/dist/activity/query.js +148 -0
- package/dist/activity/shared-state.d.ts +10 -0
- package/dist/activity/shared-state.js +100 -0
- package/dist/activity/storage.d.ts +16 -0
- package/dist/activity/storage.js +88 -0
- package/dist/api/runtime.d.ts +5 -2
- package/dist/api/runtime.js +53 -10
- package/dist/auth/client.d.ts +0 -1
- package/dist/auth/client.js +3 -5
- package/dist/auth/index.d.ts +0 -1
- package/dist/auth/index.js +0 -1
- package/dist/auth/server.d.ts +2 -1
- package/dist/auth/server.js +161 -16
- package/dist/auth/types.d.ts +5 -1
- package/dist/auth/types.js +0 -1
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli/index.js +116 -19
- package/dist/cli/project-commands.d.ts +1 -5
- package/dist/cli/project-commands.js +14 -162
- package/dist/compiler/build.d.ts +10 -1
- package/dist/compiler/build.js +124 -16
- package/dist/compiler/bundle-report.d.ts +17 -0
- package/dist/compiler/bundle-report.js +60 -0
- package/dist/compiler/client-entry.d.ts +0 -1
- package/dist/compiler/client-entry.js +28 -4
- package/dist/compiler/client-tooling.d.ts +0 -1
- package/dist/compiler/client-tooling.js +8 -2
- package/dist/compiler/index.d.ts +3 -1
- package/dist/compiler/index.js +2 -1
- package/dist/compiler/scanner.d.ts +0 -1
- package/dist/compiler/scanner.js +59 -4
- package/dist/compiler/server-bundle.d.ts +1 -2
- package/dist/compiler/server-bundle.js +11 -2
- package/dist/compiler/type-generation.d.ts +0 -1
- package/dist/compiler/type-generation.js +2 -1
- package/dist/compiler/write-manifests.d.ts +0 -1
- package/dist/compiler/write-manifests.js +0 -1
- package/dist/config/defaults.d.ts +0 -1
- package/dist/config/defaults.js +14 -2
- package/dist/config/environment.d.ts +0 -1
- package/dist/config/environment.js +0 -1
- package/dist/config/index.d.ts +0 -1
- package/dist/config/index.js +0 -1
- package/dist/config/loader.d.ts +0 -1
- package/dist/config/loader.js +0 -1
- package/dist/config/project-root.d.ts +0 -1
- package/dist/config/project-root.js +0 -1
- package/dist/config/types.d.ts +25 -1
- package/dist/config/types.js +0 -1
- package/dist/discord/assets.d.ts +5 -0
- package/dist/discord/assets.js +29 -0
- package/dist/discord/data.d.ts +38 -2
- package/dist/discord/data.js +178 -11
- package/dist/discord/events.d.ts +28 -2
- package/dist/discord/events.js +109 -39
- package/dist/discord/index.d.ts +6 -5
- package/dist/discord/index.js +3 -3
- package/dist/discord/payload.d.ts +6 -0
- package/dist/discord/payload.js +5 -0
- package/dist/discord/runtime.d.ts +0 -1
- package/dist/discord/runtime.js +0 -1
- package/dist/discord/safe-area.d.ts +0 -1
- package/dist/discord/safe-area.js +0 -1
- package/dist/discord/testing.d.ts +0 -1
- package/dist/discord/testing.js +0 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/lint/index.d.ts +6 -0
- package/dist/lint/index.js +71 -0
- package/dist/manifests/types.d.ts +11 -1
- package/dist/manifests/types.js +0 -1
- package/dist/metadata/index.d.ts +13 -0
- package/dist/metadata/index.js +4 -0
- package/dist/navigation/index.d.ts +3 -1
- package/dist/navigation/index.js +1 -1
- package/dist/navigation/link.d.ts +8 -0
- package/dist/navigation/link.js +38 -0
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.js +0 -1
- package/dist/runtime/client/dev-panel.d.ts +0 -1
- package/dist/runtime/client/dev-panel.js +722 -38
- package/dist/runtime/client/embed-router.d.ts +8 -1
- package/dist/runtime/client/embed-router.js +116 -62
- package/dist/runtime/client/files.d.ts +5 -1
- package/dist/runtime/client/files.js +0 -1
- package/dist/runtime/client/minimize-card.d.ts +0 -1
- package/dist/runtime/client/minimize-card.js +0 -1
- package/dist/runtime/client/minimize.d.ts +0 -1
- package/dist/runtime/client/minimize.js +0 -1
- package/dist/runtime/client/root.d.ts +5 -3
- package/dist/runtime/client/root.js +28 -3
- package/dist/runtime/index.d.ts +1 -2
- package/dist/runtime/index.js +0 -1
- package/dist/runtime/server/dev-server.d.ts +0 -1
- package/dist/runtime/server/dev-server.js +187 -30
- package/dist/runtime/server/diagnostics.d.ts +1 -1
- package/dist/runtime/server/diagnostics.js +5 -4
- package/dist/runtime/server/index.d.ts +0 -1
- package/dist/runtime/server/index.js +0 -1
- package/dist/runtime/server/production-server.d.ts +2 -1
- package/dist/runtime/server/production-server.js +145 -25
- package/dist/security/discord.d.ts +0 -1
- package/dist/security/discord.js +0 -1
- package/dist/security/index.d.ts +0 -1
- package/dist/security/index.js +0 -1
- package/dist/security/secret-scan.d.ts +0 -1
- package/dist/security/secret-scan.js +0 -1
- package/dist/server/discord-rest.d.ts +42 -7
- package/dist/server/discord-rest.js +135 -18
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +3 -1
- package/dist/server/instrumentation.d.ts +16 -0
- package/dist/server/instrumentation.js +3 -0
- package/dist/server/proxy.d.ts +21 -0
- package/dist/server/proxy.js +76 -0
- package/dist/server/session.d.ts +0 -1
- package/dist/server/session.js +0 -1
- package/dist/server/types.d.ts +0 -1
- package/dist/server/types.js +0 -1
- package/dist/sessions/cookies.d.ts +0 -1
- package/dist/sessions/cookies.js +0 -1
- package/dist/sessions/crypto.d.ts +0 -1
- package/dist/sessions/crypto.js +0 -1
- package/dist/sessions/data-store.d.ts +18 -0
- package/dist/sessions/data-store.js +45 -0
- package/dist/shared/errors.d.ts +0 -1
- package/dist/shared/errors.js +0 -1
- package/dist/testing/index.d.ts +0 -1
- package/dist/testing/index.js +2 -2
- package/dist/version.d.ts +1 -2
- package/dist/version.js +1 -2
- package/dist/ws/client.d.ts +0 -1
- package/dist/ws/client.js +0 -1
- package/dist/ws/index.d.ts +3 -2
- package/dist/ws/index.js +1 -1
- package/dist/ws/server.d.ts +5 -3
- package/dist/ws/server.js +65 -19
- package/dist/ws/shared-state.d.ts +13 -0
- package/dist/ws/shared-state.js +137 -0
- package/package.json +14 -1
package/dist/compiler/build.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { rename, rm } from "node:fs/promises";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import { build as viteBuild, } from "vite";
|
|
4
6
|
import { loadLudicordConfig } from "../config/loader.js";
|
|
@@ -12,8 +14,12 @@ import { bundleServerGraph } from "./server-bundle.js";
|
|
|
12
14
|
import { loadProjectEnvironment } from "../config/environment.js";
|
|
13
15
|
import { clientContainsSecret } from "../security/secret-scan.js";
|
|
14
16
|
import { checkProjectTypes } from "../runtime/server/diagnostics.js";
|
|
17
|
+
import { createBundleReport } from "./bundle-report.js";
|
|
15
18
|
const VIRTUAL_BUILD_ENTRY = "virtual:ludicord-production-entry";
|
|
16
19
|
const RESOLVED_BUILD_ENTRY = "\0virtual:ludicord-production-entry";
|
|
20
|
+
export const LUDICORD_BUILD_SCHEMA_VERSION = 2;
|
|
21
|
+
const STALE_BUILD_DIRECTORY = /^\.ludicord-(?:build|previous)-\d+-[0-9a-f-]+$/i;
|
|
22
|
+
const STALE_BUILD_AGE_MS = 24 * 60 * 60 * 1_000;
|
|
17
23
|
function safeOutputDirectory(projectRoot) {
|
|
18
24
|
const output = path.resolve(projectRoot, ".ludicord");
|
|
19
25
|
if (path.dirname(output) !== path.resolve(projectRoot) || path.basename(output) !== ".ludicord") {
|
|
@@ -21,6 +27,84 @@ function safeOutputDirectory(projectRoot) {
|
|
|
21
27
|
}
|
|
22
28
|
return output;
|
|
23
29
|
}
|
|
30
|
+
function temporaryBuildDirectory(projectRoot) {
|
|
31
|
+
const output = path.resolve(projectRoot, `.ludicord-build-${process.pid}-${randomUUID()}`);
|
|
32
|
+
if (path.dirname(output) !== path.resolve(projectRoot) || !path.basename(output).startsWith(".ludicord-build-")) {
|
|
33
|
+
throw new Error("LUDICORD7002: Refusing to use an unsafe temporary build directory.");
|
|
34
|
+
}
|
|
35
|
+
return output;
|
|
36
|
+
}
|
|
37
|
+
export function cleanupStaleBuildDirectories(projectRoot, now = Date.now()) {
|
|
38
|
+
const root = path.resolve(projectRoot);
|
|
39
|
+
const removed = [];
|
|
40
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
41
|
+
if (!entry.isDirectory() || !STALE_BUILD_DIRECTORY.test(entry.name))
|
|
42
|
+
continue;
|
|
43
|
+
const candidate = path.resolve(root, entry.name);
|
|
44
|
+
if (path.dirname(candidate) !== root)
|
|
45
|
+
continue;
|
|
46
|
+
try {
|
|
47
|
+
if (now - statSync(candidate).mtimeMs < STALE_BUILD_AGE_MS)
|
|
48
|
+
continue;
|
|
49
|
+
rmSync(candidate, { recursive: true, force: true });
|
|
50
|
+
removed.push(entry.name);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// A locked stale directory must not prevent a new atomic build.
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return removed;
|
|
57
|
+
}
|
|
58
|
+
function isTransientFilesystemLock(error) {
|
|
59
|
+
const code = error?.code;
|
|
60
|
+
return code === "EPERM" || code === "EACCES" || code === "EBUSY" || code === "ENOTEMPTY";
|
|
61
|
+
}
|
|
62
|
+
async function retryFilesystemLock(operation) {
|
|
63
|
+
for (let attempt = 0;; attempt += 1) {
|
|
64
|
+
try {
|
|
65
|
+
await operation();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (!isTransientFilesystemLock(error) || attempt >= 9)
|
|
70
|
+
throw error;
|
|
71
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(25 * 2 ** attempt, 250)));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function installBuild(projectRoot, temporary, output) {
|
|
76
|
+
const backup = path.resolve(projectRoot, `.ludicord-previous-${process.pid}-${randomUUID()}`);
|
|
77
|
+
let previousMoved = false;
|
|
78
|
+
let replacementInstalled = false;
|
|
79
|
+
try {
|
|
80
|
+
if (existsSync(output)) {
|
|
81
|
+
await retryFilesystemLock(() => rename(output, backup));
|
|
82
|
+
previousMoved = true;
|
|
83
|
+
}
|
|
84
|
+
await retryFilesystemLock(() => rename(temporary, output));
|
|
85
|
+
replacementInstalled = true;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (!replacementInstalled && !existsSync(output) && previousMoved && existsSync(backup)) {
|
|
89
|
+
try {
|
|
90
|
+
await retryFilesystemLock(() => rename(backup, output));
|
|
91
|
+
}
|
|
92
|
+
catch (restoreError) {
|
|
93
|
+
throw new AggregateError([error, restoreError], "LUDICORD7004: Build replacement failed and the previous output could not be restored.");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
if (previousMoved) {
|
|
99
|
+
try {
|
|
100
|
+
await rm(backup, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// A valid replacement is already installed. A locked backup is safer to
|
|
104
|
+
// leave in place than turning a successful build into a false failure.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
24
108
|
function buildPlugin(projectRoot, manifests, config) {
|
|
25
109
|
return {
|
|
26
110
|
name: "ludicord:production-entry",
|
|
@@ -37,7 +121,7 @@ function buildPlugin(projectRoot, manifests, config) {
|
|
|
37
121
|
async function buildClient(projectRoot, outputDirectory, manifests, config) {
|
|
38
122
|
const result = (await viteBuild({
|
|
39
123
|
configFile: false,
|
|
40
|
-
cacheDir: path.join(
|
|
124
|
+
cacheDir: path.join(outputDirectory, "cache", "client"),
|
|
41
125
|
customLogger: ludicordLogger(),
|
|
42
126
|
root: projectRoot,
|
|
43
127
|
publicDir: existsSync(path.join(projectRoot, "public"))
|
|
@@ -93,15 +177,22 @@ async function buildClient(projectRoot, outputDirectory, manifests, config) {
|
|
|
93
177
|
</body>
|
|
94
178
|
</html>
|
|
95
179
|
`, "utf8");
|
|
96
|
-
return
|
|
180
|
+
return {
|
|
181
|
+
clientEntry,
|
|
182
|
+
bundleReport: createBundleReport(path.join(outputDirectory, "client"), clientEntry, config.build.clientAssetWarningLimit),
|
|
183
|
+
};
|
|
97
184
|
}
|
|
98
|
-
function buildMetadata(clientEntry, api, websocket, config) {
|
|
185
|
+
function buildMetadata(clientEntry, api, websocket, instrumentation, proxy, bundleReport, config) {
|
|
99
186
|
return {
|
|
187
|
+
buildSchemaVersion: LUDICORD_BUILD_SCHEMA_VERSION,
|
|
100
188
|
frameworkVersion: LUDICORD_VERSION,
|
|
101
189
|
createdAt: new Date().toISOString(),
|
|
102
190
|
clientEntry,
|
|
103
191
|
api,
|
|
104
192
|
websocket,
|
|
193
|
+
...(instrumentation === undefined ? {} : { instrumentation }),
|
|
194
|
+
...(proxy === undefined ? {} : { proxy }),
|
|
195
|
+
bundleReport,
|
|
105
196
|
config: {
|
|
106
197
|
discord: {
|
|
107
198
|
...(config.discord.clientId === undefined
|
|
@@ -116,6 +207,8 @@ function buildMetadata(clientEntry, api, websocket, config) {
|
|
|
116
207
|
defaultEmbed: config.activity.defaultEmbed,
|
|
117
208
|
outsideDiscord: config.activity.outsideDiscord,
|
|
118
209
|
},
|
|
210
|
+
react: config.react,
|
|
211
|
+
build: config.build,
|
|
119
212
|
server: config.server,
|
|
120
213
|
websocket: config.websocket,
|
|
121
214
|
},
|
|
@@ -124,36 +217,51 @@ function buildMetadata(clientEntry, api, websocket, config) {
|
|
|
124
217
|
export async function buildLudicordProject(projectRoot) {
|
|
125
218
|
const root = path.resolve(projectRoot);
|
|
126
219
|
loadProjectEnvironment(root, "production");
|
|
220
|
+
cleanupStaleBuildDirectories(root);
|
|
127
221
|
const outputDirectory = safeOutputDirectory(root);
|
|
222
|
+
const temporaryDirectory = temporaryBuildDirectory(root);
|
|
128
223
|
const manifests = generateManifests(root);
|
|
129
224
|
const { config } = await loadLudicordConfig(root);
|
|
130
|
-
|
|
131
|
-
mkdirSync(outputDirectory, { recursive: true });
|
|
225
|
+
mkdirSync(temporaryDirectory, { recursive: true });
|
|
132
226
|
try {
|
|
133
227
|
generateLudicordTypes(root, manifests, config);
|
|
134
228
|
const typeErrors = checkProjectTypes(root);
|
|
135
229
|
if (typeErrors.length > 0)
|
|
136
230
|
throw new Error(typeErrors.map((item) => `${item.file ?? "TypeScript"}:${item.line ?? 1}:${item.column ?? 1}\n${item.message}\n${item.frame ?? ""}`).join("\n\n"));
|
|
137
|
-
const clientEntry = await buildClient(root,
|
|
138
|
-
const graph = await bundleServerGraph(root, path.join(
|
|
231
|
+
const { clientEntry, bundleReport } = await buildClient(root, temporaryDirectory, manifests, config);
|
|
232
|
+
const graph = await bundleServerGraph(root, path.join(temporaryDirectory, "server"), manifests);
|
|
139
233
|
const api = graph.bundles.filter((route) => route.kind === "api").map((route) => ({ source: route.source, bundle: `server/${route.bundle}` }));
|
|
140
234
|
const websocket = graph.bundles.filter((route) => route.kind === "ws").map((route) => ({ source: route.source, bundle: `server/${route.bundle}` }));
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
235
|
+
const instrumentationRoute = graph.bundles.find((route) => route.kind === "instrumentation");
|
|
236
|
+
const instrumentation = instrumentationRoute === undefined
|
|
237
|
+
? undefined
|
|
238
|
+
: { source: instrumentationRoute.source, bundle: `server/${instrumentationRoute.bundle}` };
|
|
239
|
+
const proxyRoute = graph.bundles.find((route) => route.kind === "proxy");
|
|
240
|
+
const proxy = proxyRoute === undefined
|
|
241
|
+
? undefined
|
|
242
|
+
: { source: proxyRoute.source, bundle: `server/${proxyRoute.bundle}` };
|
|
243
|
+
writeManifests(manifests, path.join(temporaryDirectory, "manifests"));
|
|
244
|
+
mkdirSync(path.join(temporaryDirectory, "types"), { recursive: true });
|
|
245
|
+
writeFileSync(path.join(temporaryDirectory, "bundle-report.json"), `${JSON.stringify(bundleReport, null, 2)}\n`, "utf8");
|
|
246
|
+
const metadata = buildMetadata(clientEntry, api, websocket, instrumentation, proxy, bundleReport, config);
|
|
247
|
+
writeFileSync(path.join(temporaryDirectory, "build.json"), `${JSON.stringify(metadata, null, 2)}\n`, "utf8");
|
|
248
|
+
if (clientContainsSecret(path.join(temporaryDirectory, "client")))
|
|
146
249
|
throw new Error("LUDICORD7001: Server secret detected in client output.");
|
|
250
|
+
await installBuild(root, temporaryDirectory, outputDirectory);
|
|
147
251
|
return {
|
|
148
252
|
outputDirectory,
|
|
149
253
|
manifests,
|
|
150
254
|
metadata,
|
|
255
|
+
bundleReport,
|
|
151
256
|
formatSummary() {
|
|
152
257
|
const routes = [
|
|
153
258
|
"/ Activity",
|
|
154
259
|
...manifests.embeds.map(({ route }) => `embed:${route.padEnd(22)} Embed`),
|
|
155
260
|
...manifests.api.map(({ route }) => `${route.padEnd(29)} API`),
|
|
156
261
|
...manifests.websocket.map(({ route }) => `${route.padEnd(29)} WebSocket`),
|
|
262
|
+
...(manifests.activity.proxy === undefined
|
|
263
|
+
? []
|
|
264
|
+
: ["app/proxy.ts Proxy"]),
|
|
157
265
|
];
|
|
158
266
|
return `Ludicord ${LUDICORD_VERSION}
|
|
159
267
|
Building Discord Activity for production...
|
|
@@ -162,6 +270,7 @@ Building Discord Activity for production...
|
|
|
162
270
|
✓ Scanned embeds
|
|
163
271
|
✓ Scanned API routes
|
|
164
272
|
✓ Scanned WebSocket routes
|
|
273
|
+
✓ Scanned request proxy
|
|
165
274
|
✓ Generated manifests
|
|
166
275
|
✓ Compiled React client
|
|
167
276
|
✓ Compiled server runtime
|
|
@@ -173,13 +282,12 @@ Route Type
|
|
|
173
282
|
────────────────────────────────────────
|
|
174
283
|
${routes.join("\n")}
|
|
175
284
|
|
|
176
|
-
✓ Build completed`;
|
|
285
|
+
✓ Build completed${bundleReport.warnings.length === 0 ? "" : `\n\nBundle warnings\n${bundleReport.warnings.map((warning) => `! ${warning}`).join("\n")}`}`;
|
|
177
286
|
},
|
|
178
287
|
};
|
|
179
288
|
}
|
|
180
289
|
catch (error) {
|
|
181
|
-
rmSync(
|
|
290
|
+
rmSync(temporaryDirectory, { recursive: true, force: true });
|
|
182
291
|
throw error;
|
|
183
292
|
}
|
|
184
293
|
}
|
|
185
|
-
//# sourceMappingURL=build.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface LudicordBundleAsset {
|
|
2
|
+
readonly file: string;
|
|
3
|
+
readonly type: "javascript" | "stylesheet" | "asset";
|
|
4
|
+
readonly bytes: number;
|
|
5
|
+
readonly gzipBytes: number;
|
|
6
|
+
readonly entry: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface LudicordBundleReport {
|
|
9
|
+
readonly generatedAt: string;
|
|
10
|
+
readonly totalBytes: number;
|
|
11
|
+
readonly totalGzipBytes: number;
|
|
12
|
+
readonly assets: readonly LudicordBundleAsset[];
|
|
13
|
+
readonly warnings: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function createBundleReport(clientDirectory: string, clientEntry: string, warningLimit: number): LudicordBundleReport;
|
|
16
|
+
export declare function formatBundleBytes(bytes: number): string;
|
|
17
|
+
export declare function formatBundleReport(report: LudicordBundleReport): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { gzipSync } from "node:zlib";
|
|
4
|
+
function listFiles(directory) {
|
|
5
|
+
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
6
|
+
const target = path.join(directory, entry.name);
|
|
7
|
+
return entry.isDirectory() ? listFiles(target) : entry.isFile() ? [target] : [];
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function assetType(file) {
|
|
11
|
+
if (/\.[cm]?js$/i.test(file))
|
|
12
|
+
return "javascript";
|
|
13
|
+
if (/\.css$/i.test(file))
|
|
14
|
+
return "stylesheet";
|
|
15
|
+
return "asset";
|
|
16
|
+
}
|
|
17
|
+
export function createBundleReport(clientDirectory, clientEntry, warningLimit) {
|
|
18
|
+
const assets = listFiles(clientDirectory)
|
|
19
|
+
.filter((file) => !file.endsWith("vite-manifest.json"))
|
|
20
|
+
.map((file) => {
|
|
21
|
+
const relative = path.relative(clientDirectory, file).split(path.sep).join("/");
|
|
22
|
+
const source = readFileSync(file);
|
|
23
|
+
return {
|
|
24
|
+
file: relative,
|
|
25
|
+
type: assetType(relative),
|
|
26
|
+
bytes: statSync(file).size,
|
|
27
|
+
gzipBytes: gzipSync(source, { level: 9 }).byteLength,
|
|
28
|
+
entry: relative === clientEntry,
|
|
29
|
+
};
|
|
30
|
+
})
|
|
31
|
+
.sort((left, right) => right.bytes - left.bytes || left.file.localeCompare(right.file));
|
|
32
|
+
const warnings = assets
|
|
33
|
+
.filter(({ bytes }) => bytes > warningLimit)
|
|
34
|
+
.map(({ file, bytes }) => `${file} is ${bytes.toLocaleString("en-US")} bytes (limit ${warningLimit.toLocaleString("en-US")}).`);
|
|
35
|
+
return {
|
|
36
|
+
generatedAt: new Date().toISOString(),
|
|
37
|
+
totalBytes: assets.reduce((total, asset) => total + asset.bytes, 0),
|
|
38
|
+
totalGzipBytes: assets.reduce((total, asset) => total + asset.gzipBytes, 0),
|
|
39
|
+
assets,
|
|
40
|
+
warnings,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function formatBundleBytes(bytes) {
|
|
44
|
+
if (bytes < 1_024)
|
|
45
|
+
return `${bytes} B`;
|
|
46
|
+
if (bytes < 1_048_576)
|
|
47
|
+
return `${(bytes / 1_024).toFixed(1)} kB`;
|
|
48
|
+
return `${(bytes / 1_048_576).toFixed(2)} MB`;
|
|
49
|
+
}
|
|
50
|
+
export function formatBundleReport(report) {
|
|
51
|
+
const rows = report.assets.map((asset) => `${asset.file.padEnd(46)} ${formatBundleBytes(asset.bytes).padStart(10)} ${formatBundleBytes(asset.gzipBytes).padStart(10)}${asset.entry ? " entry" : ""}`);
|
|
52
|
+
return `LUDICORD BUNDLE ANALYSIS
|
|
53
|
+
|
|
54
|
+
File Size Gzip
|
|
55
|
+
────────────────────────────────────────────────────────────────────────
|
|
56
|
+
${rows.length === 0 ? "(no client assets)" : rows.join("\n")}
|
|
57
|
+
────────────────────────────────────────────────────────────────────────
|
|
58
|
+
Total${"".padEnd(45)} ${formatBundleBytes(report.totalBytes).padStart(10)} ${formatBundleBytes(report.totalGzipBytes).padStart(10)}
|
|
59
|
+
${report.warnings.length === 0 ? "\n✓ No client asset exceeds the configured warning limit" : `\nWarnings\n${report.warnings.map((warning) => `! ${warning}`).join("\n")}`}`;
|
|
60
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import type { ResolvedLudicordConfig } from "../config/types.js";
|
|
2
2
|
import type { LudicordManifests } from "../manifests/types.js";
|
|
3
3
|
export declare function clientEntry(root: string, manifests: LudicordManifests, config: ResolvedLudicordConfig, development: boolean): string;
|
|
4
|
-
//# sourceMappingURL=client-entry.d.ts.map
|
|
@@ -6,7 +6,12 @@ export function clientEntry(root, manifests, config, development) {
|
|
|
6
6
|
const imports = [`import React from "react";`, `import { createRoot } from "react-dom/client";`,
|
|
7
7
|
`import { LudicordRoot } from "ludicord/internal";`, `import Pages from ${JSON.stringify(file(manifests.activity.root))};`];
|
|
8
8
|
const components = [];
|
|
9
|
-
|
|
9
|
+
let metadata = "";
|
|
10
|
+
if (manifests.activity.metadata !== undefined) {
|
|
11
|
+
imports.push(`import ActivityMetadata from ${JSON.stringify(file(manifests.activity.metadata))};`);
|
|
12
|
+
metadata = ", metadata: ActivityMetadata";
|
|
13
|
+
}
|
|
14
|
+
for (const key of ["layout", "loading", "error", "globalError", "notFound", "minimize", "authLoading", "authError", "authDenied"]) {
|
|
10
15
|
const source = manifests.activity[key];
|
|
11
16
|
if (source !== undefined) {
|
|
12
17
|
const name = `File_${key}`;
|
|
@@ -23,13 +28,32 @@ export function clientEntry(root, manifests, config, development) {
|
|
|
23
28
|
devFakeAuth: development && process.env.LUDICORD_DEV_FAKE_AUTH === "true",
|
|
24
29
|
websocket: { enabled: config.websocket.enabled, reconnect: config.websocket.reconnect },
|
|
25
30
|
};
|
|
31
|
+
const embedDefinitions = manifests.embeds.map((route, routeIndex) => {
|
|
32
|
+
const fields = [`load: () => import(${JSON.stringify(file(route.file))})`];
|
|
33
|
+
for (const [key, source] of [["loading", route.loading], ["error", route.error]]) {
|
|
34
|
+
if (source === undefined)
|
|
35
|
+
continue;
|
|
36
|
+
const name = `Embed_${routeIndex}_${key}`;
|
|
37
|
+
imports.push(`import ${name} from ${JSON.stringify(file(source))};`);
|
|
38
|
+
fields.push(`${key}: ${name}`);
|
|
39
|
+
}
|
|
40
|
+
if (route.layouts !== undefined) {
|
|
41
|
+
const layouts = route.layouts.map((source, layoutIndex) => {
|
|
42
|
+
const name = `Embed_${routeIndex}_layout_${layoutIndex}`;
|
|
43
|
+
imports.push(`import ${name} from ${JSON.stringify(file(source))};`);
|
|
44
|
+
return name;
|
|
45
|
+
});
|
|
46
|
+
fields.push(`layouts: [${layouts.join(",")}]`);
|
|
47
|
+
}
|
|
48
|
+
return `${JSON.stringify(route.route)}: { ${fields.join(", ")} }`;
|
|
49
|
+
});
|
|
26
50
|
return `${imports.join("\n")}
|
|
27
51
|
globalThis.__LUDICORD_PUBLIC_CONFIG__ = ${JSON.stringify(publicConfig)};
|
|
28
|
-
globalThis.__LUDICORD_EMBEDS__ = {${
|
|
52
|
+
globalThis.__LUDICORD_EMBEDS__ = {${embedDefinitions.join(",\n")}};
|
|
29
53
|
const container = document.getElementById("ludicord-root");
|
|
30
54
|
if (!container) throw new Error("LUDICORD2003: Missing #ludicord-root container.");
|
|
31
55
|
const root = createRoot(container);
|
|
32
|
-
|
|
56
|
+
const activity = React.createElement(LudicordRoot, { files: {${components.join(",")}}${metadata} }, React.createElement(Pages));
|
|
57
|
+
root.render(${config.react.strictMode ? "React.createElement(React.StrictMode, null, activity)" : "activity"});
|
|
33
58
|
${development ? `if (import.meta.hot) import.meta.hot.dispose(() => root.unmount());` : ""}`;
|
|
34
59
|
}
|
|
35
|
-
//# sourceMappingURL=client-entry.js.map
|
|
@@ -2,4 +2,3 @@ import type { AliasOptions, Logger, PluginOption } from "vite";
|
|
|
2
2
|
export declare function clientAliases(projectRoot: string): AliasOptions;
|
|
3
3
|
export declare function ludicordLogger(onError?: (error: unknown) => void): Logger;
|
|
4
4
|
export declare function clientPlugins(projectRoot: string, onCompile?: (file: string, elapsed?: number) => void): Promise<PluginOption[]>;
|
|
5
|
-
//# sourceMappingURL=client-tooling.d.ts.map
|
|
@@ -17,7 +17,14 @@ export function clientAliases(projectRoot) {
|
|
|
17
17
|
return require.resolve(name);
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
const entries = [
|
|
20
|
+
const entries = [
|
|
21
|
+
"react",
|
|
22
|
+
"react/jsx-runtime",
|
|
23
|
+
"react/jsx-dev-runtime",
|
|
24
|
+
"react-dom",
|
|
25
|
+
"react-dom/client",
|
|
26
|
+
"@discord/embedded-app-sdk",
|
|
27
|
+
].map((name) => ({ find: new RegExp(`^${name.replaceAll("/", "\\/")}$`), replacement: resolve(name) }));
|
|
21
28
|
const runtime = {
|
|
22
29
|
ludicord: "../index.js", "ludicord/internal": "../runtime/client/root.js", "ludicord/runtime": "../runtime/index.js",
|
|
23
30
|
"ludicord/navigation": "../navigation/index.js", "ludicord/discord": "../discord/index.js", "ludicord/auth": "../auth/index.js",
|
|
@@ -94,4 +101,3 @@ export async function clientPlugins(projectRoot, onCompile) {
|
|
|
94
101
|
}
|
|
95
102
|
return plugins;
|
|
96
103
|
}
|
|
97
|
-
//# sourceMappingURL=client-tooling.js.map
|
package/dist/compiler/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { generateManifests, scanApiRoutes, scanEmbeds, scanWebSocketRoutes, validateActivityRoot, } from "./scanner.js";
|
|
2
2
|
export { writeManifests } from "./write-manifests.js";
|
|
3
|
+
export { cleanupStaleBuildDirectories } from "./build.js";
|
|
4
|
+
export { createBundleReport, formatBundleBytes, formatBundleReport } from "./bundle-report.js";
|
|
5
|
+
export type { LudicordBundleAsset, LudicordBundleReport } from "./bundle-report.js";
|
|
3
6
|
export { generateLudicordTypes } from "./type-generation.js";
|
|
4
7
|
export { loadLudicordConfig } from "../config/loader.js";
|
|
5
8
|
export { resolveProjectRoot } from "../config/project-root.js";
|
|
6
9
|
export type { ActivityManifest, ApiManifestEntry, LudicordManifests, EmbedManifestEntry, RouteParameter, WebSocketManifestEntry, } from "../manifests/types.js";
|
|
7
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/compiler/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { generateManifests, scanApiRoutes, scanEmbeds, scanWebSocketRoutes, validateActivityRoot, } from "./scanner.js";
|
|
2
2
|
export { writeManifests } from "./write-manifests.js";
|
|
3
|
+
export { cleanupStaleBuildDirectories } from "./build.js";
|
|
4
|
+
export { createBundleReport, formatBundleBytes, formatBundleReport } from "./bundle-report.js";
|
|
3
5
|
export { generateLudicordTypes } from "./type-generation.js";
|
|
4
6
|
export { loadLudicordConfig } from "../config/loader.js";
|
|
5
7
|
export { resolveProjectRoot } from "../config/project-root.js";
|
|
6
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -4,4 +4,3 @@ export declare function scanEmbeds(projectRoot: string): EmbedManifestEntry[];
|
|
|
4
4
|
export declare function scanApiRoutes(projectRoot: string): ApiManifestEntry[];
|
|
5
5
|
export declare function scanWebSocketRoutes(projectRoot: string): WebSocketManifestEntry[];
|
|
6
6
|
export declare function generateManifests(projectRoot: string): LudicordManifests;
|
|
7
|
-
//# sourceMappingURL=scanner.d.ts.map
|
package/dist/compiler/scanner.js
CHANGED
|
@@ -23,6 +23,13 @@ function listFiles(directory) {
|
|
|
23
23
|
return files;
|
|
24
24
|
}
|
|
25
25
|
function parseSegment(rawSegment, file) {
|
|
26
|
+
const routeGroup = /^\(([^()]+)\)$/.exec(rawSegment);
|
|
27
|
+
if (routeGroup !== null) {
|
|
28
|
+
return { segment: "", catchAll: false, routeGroup: true };
|
|
29
|
+
}
|
|
30
|
+
if (rawSegment.includes("(") || rawSegment.includes(")")) {
|
|
31
|
+
throw new LudicordError("LUDICORD1401", `Invalid route group segment "${rawSegment}" in ${file}.`);
|
|
32
|
+
}
|
|
26
33
|
if (!rawSegment.includes("[") && !rawSegment.includes("]")) {
|
|
27
34
|
return { segment: rawSegment, catchAll: false };
|
|
28
35
|
}
|
|
@@ -92,11 +99,15 @@ function scanRouteKind(projectRoot, definition) {
|
|
|
92
99
|
throw new LudicordError("LUDICORD1401", `${definition.kind} routes must be placed in a named directory: ${toProjectPath(projectRoot, filePath)}.`);
|
|
93
100
|
}
|
|
94
101
|
const segments = rawSegments.map((segment) => parseSegment(segment, toProjectPath(projectRoot, filePath)));
|
|
95
|
-
const
|
|
102
|
+
const routeSegments = segments.filter(({ routeGroup }) => routeGroup !== true);
|
|
103
|
+
if (routeSegments.length === 0) {
|
|
104
|
+
throw new LudicordError("LUDICORD1401", `${definition.kind} routes need at least one URL segment in ${toProjectPath(projectRoot, filePath)}.`);
|
|
105
|
+
}
|
|
106
|
+
const invalidCatchAllIndex = routeSegments.findIndex((segment, index) => segment.catchAll && index !== routeSegments.length - 1);
|
|
96
107
|
if (invalidCatchAllIndex !== -1) {
|
|
97
108
|
throw new LudicordError("LUDICORD1401", `Catch-all segments must be last in ${toProjectPath(projectRoot, filePath)}.`);
|
|
98
109
|
}
|
|
99
|
-
const routeSuffix =
|
|
110
|
+
const routeSuffix = routeSegments.map(({ segment }) => segment).join("/");
|
|
100
111
|
const route = `${definition.prefix}${routeSuffix}`;
|
|
101
112
|
if (route === "/_ludicord" || route.startsWith("/_ludicord/")) {
|
|
102
113
|
throw new LudicordError("LUDICORD1402", `The route ${route} conflicts with Ludicord internals.`);
|
|
@@ -112,7 +123,7 @@ function scanRouteKind(projectRoot, definition) {
|
|
|
112
123
|
kind: definition.kind,
|
|
113
124
|
route,
|
|
114
125
|
file: projectFile,
|
|
115
|
-
parameters:
|
|
126
|
+
parameters: routeSegments.flatMap(({ parameter }) => parameter === undefined ? [] : [parameter]),
|
|
116
127
|
});
|
|
117
128
|
}
|
|
118
129
|
return entries.sort((left, right) => left.route.localeCompare(right.route) || left.file.localeCompare(right.file));
|
|
@@ -130,9 +141,14 @@ export function validateActivityRoot(projectRoot) {
|
|
|
130
141
|
const file = path.join(projectRoot, "app", name);
|
|
131
142
|
return existsSync(file) ? toProjectPath(projectRoot, file) : undefined;
|
|
132
143
|
};
|
|
144
|
+
const metadata = optionalFile("metadata.ts");
|
|
145
|
+
const instrumentation = optionalFile("instrumentation.ts");
|
|
146
|
+
const proxy = optionalFile("proxy.ts");
|
|
133
147
|
const layout = optionalFile("layout.tsx");
|
|
134
148
|
const loading = optionalFile("loading.tsx");
|
|
135
149
|
const error = optionalFile("error.tsx");
|
|
150
|
+
const globalError = optionalFile("global-error.tsx");
|
|
151
|
+
const notFound = optionalFile("not-found.tsx");
|
|
136
152
|
const extras = Object.fromEntries([
|
|
137
153
|
["minimize", "minimize.tsx"], ["authLoading", "auth/loading.tsx"],
|
|
138
154
|
["authError", "auth/error.tsx"], ["authDenied", "auth/denied.tsx"],
|
|
@@ -142,19 +158,59 @@ export function validateActivityRoot(projectRoot) {
|
|
|
142
158
|
}));
|
|
143
159
|
return {
|
|
144
160
|
root: "app/pages.tsx",
|
|
161
|
+
...(metadata === undefined ? {} : { metadata }),
|
|
162
|
+
...(instrumentation === undefined ? {} : { instrumentation }),
|
|
163
|
+
...(proxy === undefined ? {} : { proxy }),
|
|
145
164
|
...(layout === undefined ? {} : { layout }),
|
|
146
165
|
...(loading === undefined ? {} : { loading }),
|
|
147
166
|
...(error === undefined ? {} : { error }),
|
|
167
|
+
...(globalError === undefined ? {} : { globalError }),
|
|
168
|
+
...(notFound === undefined ? {} : { notFound }),
|
|
148
169
|
...extras,
|
|
149
170
|
};
|
|
150
171
|
}
|
|
151
172
|
export function scanEmbeds(projectRoot) {
|
|
173
|
+
const embedRoot = path.join(projectRoot, "app", "embeds");
|
|
152
174
|
return scanRouteKind(projectRoot, {
|
|
153
175
|
kind: "embed",
|
|
154
176
|
directory: path.join("app", "embeds"),
|
|
155
177
|
basename: "embed",
|
|
156
178
|
prefix: "",
|
|
157
179
|
duplicateCode: "LUDICORD1101",
|
|
180
|
+
}).map((entry) => {
|
|
181
|
+
const routeDirectory = path.dirname(path.join(projectRoot, entry.file));
|
|
182
|
+
const directories = [];
|
|
183
|
+
for (let directory = routeDirectory; directory.startsWith(embedRoot); directory = path.dirname(directory)) {
|
|
184
|
+
directories.unshift(directory);
|
|
185
|
+
if (directory === embedRoot)
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
const convention = (directory, name) => {
|
|
189
|
+
const candidate = path.join(directory, name);
|
|
190
|
+
return existsSync(candidate) && statSync(candidate).isFile()
|
|
191
|
+
? toProjectPath(projectRoot, candidate)
|
|
192
|
+
: undefined;
|
|
193
|
+
};
|
|
194
|
+
const layouts = directories.flatMap((directory) => {
|
|
195
|
+
const layout = convention(directory, "layout.tsx");
|
|
196
|
+
return layout === undefined ? [] : [layout];
|
|
197
|
+
});
|
|
198
|
+
const nearest = (name) => {
|
|
199
|
+
for (const directory of [...directories].reverse()) {
|
|
200
|
+
const candidate = convention(directory, name);
|
|
201
|
+
if (candidate !== undefined)
|
|
202
|
+
return candidate;
|
|
203
|
+
}
|
|
204
|
+
return undefined;
|
|
205
|
+
};
|
|
206
|
+
const loading = nearest("loading.tsx");
|
|
207
|
+
const error = nearest("error.tsx");
|
|
208
|
+
return {
|
|
209
|
+
...entry,
|
|
210
|
+
...(layouts.length === 0 ? {} : { layouts }),
|
|
211
|
+
...(loading === undefined ? {} : { loading }),
|
|
212
|
+
...(error === undefined ? {} : { error }),
|
|
213
|
+
};
|
|
158
214
|
});
|
|
159
215
|
}
|
|
160
216
|
export function scanApiRoutes(projectRoot) {
|
|
@@ -183,4 +239,3 @@ export function generateManifests(projectRoot) {
|
|
|
183
239
|
websocket: scanWebSocketRoutes(projectRoot),
|
|
184
240
|
};
|
|
185
241
|
}
|
|
186
|
-
//# sourceMappingURL=scanner.js.map
|
|
@@ -3,9 +3,8 @@ export declare function bundleServerGraph(root: string, output: string, manifest
|
|
|
3
3
|
bundles: {
|
|
4
4
|
source: string;
|
|
5
5
|
bundle: string;
|
|
6
|
-
kind: "api" | "ws";
|
|
6
|
+
kind: "api" | "ws" | "instrumentation" | "proxy";
|
|
7
7
|
}[];
|
|
8
8
|
dependencies: Set<string>;
|
|
9
9
|
loadModule<T>(absoluteFile: string): Promise<T>;
|
|
10
10
|
}>;
|
|
11
|
-
//# sourceMappingURL=server-bundle.d.ts.map
|
|
@@ -3,7 +3,17 @@ import { pathToFileURL } from "node:url";
|
|
|
3
3
|
import { build } from "esbuild";
|
|
4
4
|
export async function bundleServerGraph(root, output, manifests) {
|
|
5
5
|
const routes = [...manifests.api.map((route, index) => ({ ...route, name: `api/${index}` })),
|
|
6
|
-
...manifests.websocket.map((route, index) => ({ ...route, name: `ws/${index}` }))
|
|
6
|
+
...manifests.websocket.map((route, index) => ({ ...route, name: `ws/${index}` })),
|
|
7
|
+
...(manifests.activity.instrumentation === undefined ? [] : [{
|
|
8
|
+
kind: "instrumentation",
|
|
9
|
+
file: manifests.activity.instrumentation,
|
|
10
|
+
name: "instrumentation/index",
|
|
11
|
+
}]),
|
|
12
|
+
...(manifests.activity.proxy === undefined ? [] : [{
|
|
13
|
+
kind: "proxy",
|
|
14
|
+
file: manifests.activity.proxy,
|
|
15
|
+
name: "proxy/index",
|
|
16
|
+
}])];
|
|
7
17
|
const bundles = routes.map((route) => ({ source: route.file, bundle: `${route.name}.mjs`, kind: route.kind }));
|
|
8
18
|
const dependencies = new Set();
|
|
9
19
|
if (routes.length > 0) {
|
|
@@ -28,4 +38,3 @@ export async function bundleServerGraph(root, output, manifests) {
|
|
|
28
38
|
},
|
|
29
39
|
};
|
|
30
40
|
}
|
|
31
|
-
//# sourceMappingURL=server-bundle.js.map
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import type { ResolvedLudicordConfig } from "../config/types.js";
|
|
2
2
|
import type { LudicordManifests } from "../manifests/types.js";
|
|
3
3
|
export declare function generateLudicordTypes(projectRoot: string, manifests: LudicordManifests, config: ResolvedLudicordConfig): string;
|
|
4
|
-
//# sourceMappingURL=type-generation.d.ts.map
|
|
@@ -65,6 +65,8 @@ ${routeParams(manifests.websocket)}
|
|
|
65
65
|
readonly defaultEmbed: ${JSON.stringify(config.activity.defaultEmbed)};
|
|
66
66
|
readonly outsideDiscord: ${JSON.stringify(config.activity.outsideDiscord)};
|
|
67
67
|
readonly authRequired: ${String(config.discord.auth.required)};
|
|
68
|
+
readonly strictMode: ${String(config.react.strictMode)};
|
|
69
|
+
readonly clientAssetWarningLimit: ${String(config.build.clientAssetWarningLimit)};
|
|
68
70
|
};
|
|
69
71
|
}
|
|
70
72
|
}
|
|
@@ -90,4 +92,3 @@ export function generateLudicordTypes(projectRoot, manifests, config) {
|
|
|
90
92
|
ensureLudicordEnv(root);
|
|
91
93
|
return publicFile;
|
|
92
94
|
}
|
|
93
|
-
//# sourceMappingURL=type-generation.js.map
|