@mandujs/core 0.54.2 → 0.54.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mandujs/core",
3
- "version": "0.54.2",
3
+ "version": "0.54.3",
4
4
  "description": "Mandu Framework Core - Spec, Generator, Guard, Runtime",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -110,9 +110,11 @@
110
110
  "./components/Image": "./src/components/Image.tsx"
111
111
  },
112
112
  "files": [
113
- "src/**/*"
113
+ "src/**/*",
114
+ "scripts/postinstall-lock.ts"
114
115
  ],
115
116
  "scripts": {
117
+ "postinstall": "bun ./scripts/postinstall-lock.ts",
116
118
  "test": "bun test tests/streaming-ssr && bun test tests/hydration tests/typing src",
117
119
  "test:hydration": "bun test tests/hydration",
118
120
  "test:streaming": "bun test tests/streaming-ssr",
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Refresh an existing Guard lock after package-manager updates.
3
+ *
4
+ * Lifecycle scripts run from inside the installed package, so projectRoot
5
+ * defaults to INIT_CWD when Bun provides it. This helper is intentionally
6
+ * best-effort: installs must not fail because a project has a stale, invalid,
7
+ * or temporarily unreadable Mandu config.
8
+ */
9
+
10
+ import path from "node:path";
11
+ import {
12
+ validateConfig,
13
+ type ValidatedManduConfig,
14
+ } from "../src/config/validate.js";
15
+ import { CONFIG_FILES } from "../src/config/mandu.js";
16
+ import {
17
+ generateLockfile,
18
+ readLockfile,
19
+ readMcpConfig,
20
+ writeLockfile,
21
+ LOCKFILE_PATH,
22
+ } from "../src/lockfile/index.js";
23
+
24
+ export type PostinstallLockAction =
25
+ | "updated"
26
+ | "skipped-disabled"
27
+ | "skipped-no-project-config"
28
+ | "skipped-no-lockfile"
29
+ | "skipped-invalid-lockfile"
30
+ | "skipped-invalid-config"
31
+ | "skipped-invalid-mcp-config"
32
+ | "skipped-write-failed";
33
+
34
+ export interface PostinstallLockResult {
35
+ action: PostinstallLockAction;
36
+ projectRoot: string;
37
+ hash?: string;
38
+ error?: string;
39
+ }
40
+
41
+ export interface PostinstallLockOptions {
42
+ projectRoot?: string;
43
+ env?: NodeJS.ProcessEnv;
44
+ verbose?: boolean;
45
+ log?: (message: string) => void;
46
+ warn?: (message: string) => void;
47
+ }
48
+
49
+ export async function refreshGuardLockAfterInstall(
50
+ options: PostinstallLockOptions = {},
51
+ ): Promise<PostinstallLockResult> {
52
+ const env = options.env ?? process.env;
53
+ const projectRoot = path.resolve(
54
+ options.projectRoot ?? env.INIT_CWD ?? process.cwd(),
55
+ );
56
+ const verbose = options.verbose ?? env.MANDU_POSTINSTALL_VERBOSE === "1";
57
+ const log = options.log ?? console.log;
58
+ const warn = options.warn ?? console.warn;
59
+
60
+ const report = (result: PostinstallLockResult): PostinstallLockResult => {
61
+ if (verbose) {
62
+ if (result.action === "updated") {
63
+ log(`[Mandu] refreshed ${LOCKFILE_PATH} (${result.hash})`);
64
+ } else if (result.error) {
65
+ warn(`[Mandu] ${result.action}: ${result.error}`);
66
+ } else {
67
+ log(`[Mandu] ${result.action}`);
68
+ }
69
+ }
70
+ return result;
71
+ };
72
+
73
+ if (env.MANDU_POSTINSTALL_LOCK === "0") {
74
+ return report({ action: "skipped-disabled", projectRoot });
75
+ }
76
+
77
+ if (!(await hasProjectConfig(projectRoot))) {
78
+ return report({ action: "skipped-no-project-config", projectRoot });
79
+ }
80
+
81
+ let existingLockfile: Awaited<ReturnType<typeof readLockfile>>;
82
+ try {
83
+ existingLockfile = await readLockfile(projectRoot);
84
+ } catch (error) {
85
+ return report({
86
+ action: "skipped-invalid-lockfile",
87
+ projectRoot,
88
+ error: stringifyError(error),
89
+ });
90
+ }
91
+
92
+ if (!existingLockfile) {
93
+ return report({ action: "skipped-no-lockfile", projectRoot });
94
+ }
95
+
96
+ const validation = await validateConfig(projectRoot);
97
+ if (!validation.valid || !validation.config) {
98
+ return report({
99
+ action: "skipped-invalid-config",
100
+ projectRoot,
101
+ error:
102
+ validation.errors?.map((entry) => entry.message).join("; ") ??
103
+ "Mandu config validation failed",
104
+ });
105
+ }
106
+
107
+ let mcpConfig: Record<string, unknown> | null;
108
+ try {
109
+ mcpConfig = await readMcpConfig(projectRoot);
110
+ } catch (error) {
111
+ return report({
112
+ action: "skipped-invalid-mcp-config",
113
+ projectRoot,
114
+ error: stringifyError(error),
115
+ });
116
+ }
117
+
118
+ try {
119
+ const lockfile = generateLockfile(
120
+ validation.config as ValidatedManduConfig,
121
+ {
122
+ includeSnapshot: existingLockfile.snapshot !== undefined,
123
+ includeMcpServerHashes: true,
124
+ },
125
+ mcpConfig,
126
+ );
127
+ await writeLockfile(projectRoot, lockfile);
128
+ return report({ action: "updated", projectRoot, hash: lockfile.configHash });
129
+ } catch (error) {
130
+ return report({
131
+ action: "skipped-write-failed",
132
+ projectRoot,
133
+ error: stringifyError(error),
134
+ });
135
+ }
136
+ }
137
+
138
+ async function hasProjectConfig(projectRoot: string): Promise<boolean> {
139
+ for (const fileName of CONFIG_FILES) {
140
+ if (await Bun.file(path.join(projectRoot, fileName)).exists()) {
141
+ return true;
142
+ }
143
+ }
144
+ return false;
145
+ }
146
+
147
+ function stringifyError(error: unknown): string {
148
+ return error instanceof Error ? error.message : String(error);
149
+ }
150
+
151
+ if (import.meta.main) {
152
+ await refreshGuardLockAfterInstall();
153
+ }
@@ -29,10 +29,11 @@ import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from "fs";
29
29
  import { tmpdir } from "os";
30
30
  import path from "path";
31
31
  import type { RouteSpec } from "../../spec/schema";
32
- import {
33
- _testOnly_scanIslandFiles,
34
- _testOnly_getHydratedRoutes,
35
- } from "../build";
32
+ import {
33
+ _testOnly_scanIslandFiles,
34
+ _testOnly_scanPartialFiles,
35
+ _testOnly_getHydratedRoutes,
36
+ } from "../build";
36
37
  import { HMR_PERF } from "../../perf/hmr-markers";
37
38
  import {
38
39
  _resetCacheForTesting as _resetPerfCache,
@@ -372,7 +373,7 @@ describe("Phase 7.1 R1 Agent C — getHydratedRoutes filter", () => {
372
373
  });
373
374
  });
374
375
 
375
- describe("Phase 7.1 R1 Agent C — per-island scan skips non-hydrated routes", () => {
376
+ describe("Phase 7.1 R1 Agent C — per-island scan skips non-hydrated routes", () => {
376
377
  let project: ReturnType<typeof createProject>;
377
378
 
378
379
  beforeEach(() => {
@@ -500,5 +501,32 @@ describe("Phase 7.1 R1 Agent C — per-island scan skips non-hydrated routes", (
500
501
  ];
501
502
  const result = await _testOnly_scanIslandFiles(routes, project.rootDir);
502
503
  expect(result).toEqual([]);
503
- });
504
- });
504
+ });
505
+ });
506
+
507
+ describe("partial bundle scan", () => {
508
+ let project: ReturnType<typeof createProject>;
509
+
510
+ beforeEach(() => {
511
+ project = createProject();
512
+ });
513
+
514
+ afterEach(() => {
515
+ try {
516
+ rmSync(project.rootDir, { recursive: true, force: true });
517
+ } catch {
518
+ /* Windows lock tolerance */
519
+ }
520
+ });
521
+
522
+ it("discovers *.partial.tsx files outside .mandu and node_modules", async () => {
523
+ project.writeIslandFile("app", "Home.partial.tsx");
524
+ project.writeIslandFile(".mandu/client", "Stale.partial.tsx");
525
+ project.writeIslandFile("node_modules/pkg", "Ignored.partial.tsx");
526
+
527
+ const result = await _testOnly_scanPartialFiles(project.rootDir);
528
+
529
+ expect(result.map((entry) => entry.name)).toEqual(["Home"]);
530
+ expect(result[0].priority).toBe("visible");
531
+ });
532
+ });
@@ -341,18 +341,26 @@ export async function analyzeBundle(
341
341
  deps: entry.dependencies ?? [],
342
342
  });
343
343
  }
344
- for (const [islandName, entry] of Object.entries(manifest.islands ?? {})) {
345
- // Avoid double-counting: if a per-island chunk shares its route id with
346
- // a route-level bundle, we prefer the island entry (finer granularity).
347
- const existing = islandSources.findIndex((s) => s.name === islandName);
344
+ for (const [islandName, entry] of Object.entries(manifest.islands ?? {})) {
345
+ // Avoid double-counting: if a per-island chunk shares its route id with
346
+ // a route-level bundle, we prefer the island entry (finer granularity).
347
+ const existing = islandSources.findIndex((s) => s.name === islandName);
348
348
  if (existing !== -1) islandSources.splice(existing, 1);
349
349
  islandSources.push({
350
350
  name: islandName,
351
351
  url: entry.js,
352
352
  priority: entry.priority,
353
- deps: [],
354
- });
355
- }
353
+ deps: [],
354
+ });
355
+ }
356
+ for (const [partialName, entry] of Object.entries(manifest.partials ?? {})) {
357
+ islandSources.push({
358
+ name: `partial:${partialName}`,
359
+ url: entry.js,
360
+ priority: entry.priority,
361
+ deps: [],
362
+ });
363
+ }
356
364
 
357
365
  const islands: AnalyzeIsland[] = [];
358
366
  for (const src of islandSources) {
@@ -171,9 +171,16 @@ describe("buildClientBundles vendor shims", () => {
171
171
  expect(runtimeSource).toContain("function hasHydratableMarkup");
172
172
  expect(runtimeSource).toContain("function shouldHydrateCompiledIsland");
173
173
  expect(runtimeSource).toContain("onRecoverableError");
174
- expect(runtimeSource).toContain("data-mandu-hydrating");
175
- expect(runtimeSource).toContain("data-mandu-render-mode");
176
- expect(runtimeSource).toContain("data-mandu-recoverable-error");
177
- expect(runtimeSource).toContain("pointerdown");
178
- });
179
- });
174
+ expect(runtimeSource).toContain("data-mandu-hydrating");
175
+ expect(runtimeSource).toContain("data-mandu-render-mode");
176
+ expect(runtimeSource).toContain("data-mandu-recoverable-error");
177
+ expect(runtimeSource).toContain("pointerdown");
178
+ });
179
+
180
+ test("runtime parses SSR data script before island setup", async () => {
181
+ const runtimeSource = await readFile(path.join(rootDir, ".mandu", "client", "_runtime.js"), "utf-8");
182
+ expect(runtimeSource).toContain("function readManduData");
183
+ expect(runtimeSource).toContain("document.getElementById(\"__MANDU_DATA__\")");
184
+ expect(runtimeSource).toContain("JSON.parse");
185
+ });
186
+ });