@jxsuite/server 3.0.0 → 4.1.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 +5 -5
- package/package.json +16 -14
- package/src/import-api.ts +118 -3
- package/src/live-preview.ts +616 -0
- package/src/packages.ts +28 -10
- package/src/preview-client.ts +177 -0
- package/src/project-server.ts +16 -0
- package/src/refactor/apply.ts +31 -3
- package/src/refactor/find-refs.ts +0 -0
- package/src/refactor/mounts.ts +57 -0
- package/src/refactor/paths.ts +147 -13
- package/src/refactor/refs.ts +124 -4
- package/src/refactor/scan.ts +51 -5
- package/src/resolve.ts +17 -0
- package/src/server.ts +26 -1
- package/src/sse.ts +126 -0
- package/src/studio-api.ts +130 -10
- package/src/watch.ts +9 -97
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The script a live-preview page runs: reload when the tree changes, follow a retarget, and say so
|
|
3
|
+
* when the origin is gone.
|
|
4
|
+
*
|
|
5
|
+
* It ships as a string rather than a file because it is served, not imported — the page that runs
|
|
6
|
+
* it is on a different origin from anything in this package, and generating it is how the URLs it
|
|
7
|
+
* needs stay a fact of the server that serves it.
|
|
8
|
+
*
|
|
9
|
+
* Three behaviours are here rather than in the page because only this side can know them:
|
|
10
|
+
*
|
|
11
|
+
* **A reload, not an in-place re-render.** The runtime cannot be re-run over a live document:
|
|
12
|
+
* `Jx()` does not scope its effects, a custom element cannot be re-registered in a realm (so an
|
|
13
|
+
* edited component would keep its old class forever), the document cache has no invalidation,
|
|
14
|
+
* `$head` would be injected twice, and the shell renders the head server-side anyway. A document
|
|
15
|
+
* load is the only thing that is actually correct, and it is cheap here because composing is
|
|
16
|
+
* local.
|
|
17
|
+
*
|
|
18
|
+
* **Scroll is restored by hand.** The browser's own restoration runs against the document as it
|
|
19
|
+
* exists at that moment, and a client-rendered page has an empty body then — so it restores to a
|
|
20
|
+
* zero-height document and lands at the top. The shell turns it off; this puts it back after the
|
|
21
|
+
* render, and keeps re-applying briefly because content resolved after mount changes the height.
|
|
22
|
+
*
|
|
23
|
+
* **A dead origin is distinguished from a restarting one.** EventSource retries forever and cannot
|
|
24
|
+
* tell the two apart, so after enough consecutive failures this asks: one fetch that a live origin
|
|
25
|
+
* always answers. A network error means the project closed, and then the stream is stopped rather
|
|
26
|
+
* than left retrying every half-second for as long as the tab is open. The last render stays on
|
|
27
|
+
* screen, because it is still what the reader was reading.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** Where the client's own surfaces live, so the page and the server cannot disagree about them. */
|
|
31
|
+
export const LIVE_NAMESPACE = "/__jx_live__";
|
|
32
|
+
|
|
33
|
+
/** Consecutive reconnect failures before the client asks whether the origin is gone at all. */
|
|
34
|
+
const PROBE_AFTER_FAILURES = 20;
|
|
35
|
+
|
|
36
|
+
/** How long the restore keeps re-applying, for content that resolves after the render. */
|
|
37
|
+
const SCROLL_SETTLE_MS = 1500;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The reload client, as source.
|
|
41
|
+
*
|
|
42
|
+
* Written as one string with no build step: it is ~80 lines of DOM code that must run in a page
|
|
43
|
+
* this package does not otherwise touch, and a bundler in the path would be a second thing to keep
|
|
44
|
+
* working for no gain.
|
|
45
|
+
*/
|
|
46
|
+
export const PREVIEW_CLIENT_JS = `
|
|
47
|
+
const NS = ${JSON.stringify(LIVE_NAMESPACE)};
|
|
48
|
+
const KEY = "jx-preview-scroll";
|
|
49
|
+
let failures = 0;
|
|
50
|
+
let stopped = false;
|
|
51
|
+
let source = null;
|
|
52
|
+
|
|
53
|
+
/* The banner lives in a shadow root: the page around it is the project's own site, carrying the
|
|
54
|
+
project's own CSS, which is free to style every div on the page. */
|
|
55
|
+
function banner(text, tone) {
|
|
56
|
+
let host = document.getElementById("jx-preview-status");
|
|
57
|
+
if (!host) {
|
|
58
|
+
host = document.createElement("div");
|
|
59
|
+
host.id = "jx-preview-status";
|
|
60
|
+
document.documentElement.append(host);
|
|
61
|
+
host.attachShadow({ mode: "open" });
|
|
62
|
+
}
|
|
63
|
+
host.shadowRoot.innerHTML = text
|
|
64
|
+
? '<div style="position:fixed;left:0;right:0;bottom:0;z-index:2147483647;' +
|
|
65
|
+
"font:13px/1.4 system-ui,sans-serif;padding:8px 12px;color:#fff;background:" +
|
|
66
|
+
(tone === "dead" ? "#8a1c1c" : "#444") + ';">' + text + "</div>"
|
|
67
|
+
: "";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function saveScroll() {
|
|
71
|
+
try {
|
|
72
|
+
sessionStorage.setItem(
|
|
73
|
+
KEY,
|
|
74
|
+
JSON.stringify({ path: location.pathname, x: scrollX, y: scrollY }),
|
|
75
|
+
);
|
|
76
|
+
} catch {}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* Restored after the render resolves, then re-applied for a moment: a page whose height depends on
|
|
80
|
+
data fetched after mount is shorter at restore time than it will be a tick later. */
|
|
81
|
+
function restoreScroll() {
|
|
82
|
+
let saved = null;
|
|
83
|
+
try {
|
|
84
|
+
saved = JSON.parse(sessionStorage.getItem(KEY) || "null");
|
|
85
|
+
} catch {}
|
|
86
|
+
if (!saved || saved.path !== location.pathname || (saved.x === 0 && saved.y === 0)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const until = Date.now() + ${SCROLL_SETTLE_MS};
|
|
90
|
+
const apply = () => {
|
|
91
|
+
scrollTo(saved.x, saved.y);
|
|
92
|
+
if (Date.now() < until) {
|
|
93
|
+
requestAnimationFrame(apply);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
apply();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function reload() {
|
|
100
|
+
saveScroll();
|
|
101
|
+
location.reload();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function connect() {
|
|
105
|
+
if (stopped) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
source = new EventSource(NS + "/reload?route=" + encodeURIComponent(location.pathname));
|
|
109
|
+
source.onopen = () => {
|
|
110
|
+
failures = 0;
|
|
111
|
+
banner("");
|
|
112
|
+
};
|
|
113
|
+
source.onmessage = reload;
|
|
114
|
+
source.addEventListener("navigate", (event) => {
|
|
115
|
+
const message = JSON.parse(event.data);
|
|
116
|
+
/* Acknowledge BEFORE navigating: the navigation tears this stream down, so an ack sent after
|
|
117
|
+
it would never leave the tab, and the editor would open a second one. */
|
|
118
|
+
fetch(NS + "/ack", {
|
|
119
|
+
body: JSON.stringify({ gen: message.gen }),
|
|
120
|
+
headers: { "Content-Type": "application/json" },
|
|
121
|
+
keepalive: true,
|
|
122
|
+
method: "POST",
|
|
123
|
+
}).catch(() => {});
|
|
124
|
+
/* Best effort, and never advertised: a background tab cannot raise itself in Chrome, and
|
|
125
|
+
under Wayland the compositor arbitrates. It costs one line and occasionally works. */
|
|
126
|
+
try {
|
|
127
|
+
window.focus();
|
|
128
|
+
} catch {}
|
|
129
|
+
if (message.route === location.pathname) {
|
|
130
|
+
reload();
|
|
131
|
+
} else {
|
|
132
|
+
saveScroll();
|
|
133
|
+
location.assign(message.route);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
source.onerror = () => {
|
|
137
|
+
failures += 1;
|
|
138
|
+
if (failures === 1) {
|
|
139
|
+
/* A restart reconnects inside half a second, so saying anything now is noise. */
|
|
140
|
+
setTimeout(() => {
|
|
141
|
+
if (failures > 0 && !stopped) {
|
|
142
|
+
banner("Preview disconnected — reconnecting…", "warn");
|
|
143
|
+
}
|
|
144
|
+
}, 2000);
|
|
145
|
+
}
|
|
146
|
+
if (failures >= ${PROBE_AFTER_FAILURES}) {
|
|
147
|
+
probe();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* EventSource cannot tell "restarting" from "gone", so ask something a live origin always answers. */
|
|
153
|
+
function probe() {
|
|
154
|
+
fetch(NS + "/ping", { cache: "no-store" }).catch(() => {
|
|
155
|
+
stopped = true;
|
|
156
|
+
if (source) {
|
|
157
|
+
source.close();
|
|
158
|
+
}
|
|
159
|
+
banner(
|
|
160
|
+
"This preview is no longer live — Jx Studio closed this project. " +
|
|
161
|
+
"The page below is the last render.",
|
|
162
|
+
"dead",
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
addEventListener("pagehide", saveScroll);
|
|
168
|
+
/* A tab restored from the back/forward cache has a suspended stream and a stale document, and no
|
|
169
|
+
reload will arrive to fix either. */
|
|
170
|
+
addEventListener("pageshow", (event) => {
|
|
171
|
+
if (event.persisted) {
|
|
172
|
+
location.reload();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
restoreScroll();
|
|
176
|
+
connect();
|
|
177
|
+
`;
|
package/src/project-server.ts
CHANGED
|
@@ -345,6 +345,22 @@ export function createProjectServer(options: CreateProjectServerOptions): Projec
|
|
|
345
345
|
return;
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
+
/*
|
|
349
|
+
* The keepalive, answered AHEAD of the session lookup.
|
|
350
|
+
*
|
|
351
|
+
* A ping's whole job is to prove the socket is alive, and that is a fact about the socket,
|
|
352
|
+
* not about which project a window is bound to. Behind the lookup it would be refused —
|
|
353
|
+
* and close the socket — in exactly the states where staying connected matters most: a
|
|
354
|
+
* window mid-re-root, or one whose session was torn down and rebuilt. The shell sends one
|
|
355
|
+
* every 30s because this server's idle timeout is 120s and a studio can be quiet for far
|
|
356
|
+
* longer than that (a site import runs for minutes over a separate HTTP stream and touches
|
|
357
|
+
* this socket not at all).
|
|
358
|
+
*/
|
|
359
|
+
if (msg.method === "__ping") {
|
|
360
|
+
ws.send(JSON.stringify({ id: msg.id, result: null }));
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
348
364
|
// Re-resolve the session FRESH each message (fail-closed for Phase 7 multi-window).
|
|
349
365
|
const session = resolveSession(ws.data.winId);
|
|
350
366
|
if (!session) {
|
package/src/refactor/apply.ts
CHANGED
|
@@ -14,7 +14,9 @@ import { errorMessage } from "@jxsuite/schema/parse";
|
|
|
14
14
|
import { isUnder } from "./paths.ts";
|
|
15
15
|
import { rewriteDocRefs, rewriteTagName } from "./refs.ts";
|
|
16
16
|
import { documentGlob, fwd, loadDoc, skipScanPath } from "./scan.ts";
|
|
17
|
+
import { refactorMounts } from "./mounts.ts";
|
|
17
18
|
import type { RefChange } from "./refs.ts";
|
|
19
|
+
import type { AssetMount } from "@jxsuite/schema/asset-paths";
|
|
18
20
|
import type { FormatRegistry } from "@jxsuite/schema/format-registry";
|
|
19
21
|
|
|
20
22
|
export interface FileChange {
|
|
@@ -47,6 +49,12 @@ export interface ApplyRenameOptions {
|
|
|
47
49
|
absTo: string;
|
|
48
50
|
/** Format registry for the project (parse/serialize of non-JSON documents). */
|
|
49
51
|
registry: FormatRegistry;
|
|
52
|
+
/**
|
|
53
|
+
* Project-relative asset mounts, for rewriting rooted references through the mount lane. Omitted,
|
|
54
|
+
* the engine loads them from `root` — see `mounts.ts` for why that is the default and not the
|
|
55
|
+
* exception.
|
|
56
|
+
*/
|
|
57
|
+
mounts?: readonly AssetMount[];
|
|
50
58
|
}
|
|
51
59
|
|
|
52
60
|
/** Derive a custom-element tag from a component filename (strips `.class.json` or the extension). */
|
|
@@ -67,6 +75,7 @@ export async function applyRename(opts: ApplyRenameOptions): Promise<RenameRepor
|
|
|
67
75
|
const root = fwd(opts.root);
|
|
68
76
|
const absFrom = fwd(opts.absFrom);
|
|
69
77
|
const absTo = fwd(opts.absTo);
|
|
78
|
+
const mounts = opts.mounts ?? (await refactorMounts(opts.root));
|
|
70
79
|
|
|
71
80
|
let isDir = false;
|
|
72
81
|
try {
|
|
@@ -113,13 +122,14 @@ export async function applyRename(opts: ApplyRenameOptions): Promise<RenameRepor
|
|
|
113
122
|
}
|
|
114
123
|
const fp = fwd(resolve(opts.root, match));
|
|
115
124
|
try {
|
|
116
|
-
const { doc, serialize } = await loadDoc(fp, opts.registry);
|
|
125
|
+
const { doc, rewrite, serialize } = await loadDoc(fp, opts.registry);
|
|
117
126
|
const docNewDir = dirname(fp);
|
|
118
127
|
const docOldDir = isUnder(fp, absTo) ? dirname(absFrom + fp.slice(absTo.length)) : docNewDir;
|
|
119
128
|
|
|
120
129
|
const { changes } = rewriteDocRefs(doc, {
|
|
121
130
|
docNewDir,
|
|
122
131
|
docOldDir,
|
|
132
|
+
mounts,
|
|
123
133
|
newAbs: absTo,
|
|
124
134
|
oldAbs: absFrom,
|
|
125
135
|
root,
|
|
@@ -129,11 +139,29 @@ export async function applyRename(opts: ApplyRenameOptions): Promise<RenameRepor
|
|
|
129
139
|
if (changes.length === 0 && tagCount === 0) {
|
|
130
140
|
continue;
|
|
131
141
|
}
|
|
132
|
-
|
|
142
|
+
/*
|
|
143
|
+
* Two ways to write a document back, and the narrower one is not a fallback for a broken
|
|
144
|
+
* format — it is the only honest promise a load-only format can make. A CSV collection has a
|
|
145
|
+
* parser and deliberately no serializer, so a reference living in one used to be COUNTED and
|
|
146
|
+
* then reported as a remainder the author had to repair by hand (issue 246). `rewrite`
|
|
147
|
+
* replaces the authored values in the file's own bytes and changes nothing else, which is all
|
|
148
|
+
* a rename ever needed.
|
|
149
|
+
*
|
|
150
|
+
* `serialize` still wins where it exists: a full round trip can express a change a list of
|
|
151
|
+
* value edits cannot. A TAG rename is exactly such a change, so a document carrying one and
|
|
152
|
+
* no serializer is still named in the report rather than half-written.
|
|
153
|
+
*/
|
|
154
|
+
const written =
|
|
155
|
+
serialize === null
|
|
156
|
+
? tagCount === 0 && rewrite !== null
|
|
157
|
+
? await rewrite(changes)
|
|
158
|
+
: null
|
|
159
|
+
: await serialize(doc);
|
|
160
|
+
if (written === null) {
|
|
133
161
|
report.errors.push({ error: `No serializer for "${match}"`, path: fwd(match) });
|
|
134
162
|
continue;
|
|
135
163
|
}
|
|
136
|
-
await writeFile(fp,
|
|
164
|
+
await writeFile(fp, written);
|
|
137
165
|
|
|
138
166
|
if (changes.length > 0) {
|
|
139
167
|
report.references.files.push({ changes, count: changes.length, path: fwd(match) });
|
|
Binary file
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mounts.ts — the asset mounts the refactor engine resolves rooted references through.
|
|
3
|
+
*
|
|
4
|
+
* A rooted reference (`/images/hero.jpg`) is a site URL, and an extension may claim a slice of that
|
|
5
|
+
* URL space and serve it out of a directory of its own (specs/extensions.md §8.5). So "which file
|
|
6
|
+
* does this URL name?" cannot be answered from the project root alone, and neither the read pass
|
|
7
|
+
* nor the write pass can be correct without the mount list.
|
|
8
|
+
*
|
|
9
|
+
* **The engine loads them rather than taking them.** The format registry is passed in by every
|
|
10
|
+
* caller because every caller already has one; nobody has a mount list, and there are four hosts
|
|
11
|
+
* (two dev-server routes, the desktop project session, the cloud gateway). An option four hosts
|
|
12
|
+
* each have to remember is an option three of them eventually do not — and a lane that silently
|
|
13
|
+
* fails to resolve is precisely the defect this module exists to close (issue 239). `mounts` stays
|
|
14
|
+
* available on both option types for a host that already has the list, and for tests.
|
|
15
|
+
*
|
|
16
|
+
* **Project-relative, filtered.** `loadAssetMounts` hands back whatever directory an extension
|
|
17
|
+
* declared, which is normally absolute; `asset-paths.ts` documents its mount parameter as
|
|
18
|
+
* project-relative. Rebasing here is that conversion, and a mount resolving OUTSIDE the project is
|
|
19
|
+
* dropped: its files are not addressable by a project-relative path, so no rename can reach them
|
|
20
|
+
* and no reference to them can be counted. `projectPathsForSiteUrl` declines the same case by
|
|
21
|
+
* name.
|
|
22
|
+
*
|
|
23
|
+
* That drop is deliberate and is not the near half of a bug. A collection's `source` may point
|
|
24
|
+
* outside the project (site-architecture.md §9.3), and its media renders — the compiler and the dev
|
|
25
|
+
* server both publish it at the mount URL — but the editing host's file API is project-scoped by
|
|
26
|
+
* containment check, so no surface can list, open or ask about the file either. Nothing therefore
|
|
27
|
+
* reports a confident zero about it, which is the failure the lane math exists to prevent. Reaching
|
|
28
|
+
* such a file means giving it a mount-relative identity in that file API, not widening this filter;
|
|
29
|
+
* see §9.3, which records the decision (issue 244).
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { relative } from "node:path";
|
|
33
|
+
import { projectAssetMounts } from "../resolve.ts";
|
|
34
|
+
import { fwd } from "./scan.ts";
|
|
35
|
+
import type { AssetMount } from "@jxsuite/schema/asset-paths";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Project-relative asset mounts for a project root, or an empty list when there is no project here.
|
|
39
|
+
*
|
|
40
|
+
* Not memoised on its own account: `projectAssetMounts` is already cached against `project.json`'s
|
|
41
|
+
* mtime, and a second cache here would be a second thing to invalidate.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} root - Absolute project root
|
|
44
|
+
* @returns {Promise<AssetMount[]>} Mounts whose `dir` is relative to `root`
|
|
45
|
+
*/
|
|
46
|
+
export async function refactorMounts(root: string): Promise<AssetMount[]> {
|
|
47
|
+
const declared = await projectAssetMounts(root);
|
|
48
|
+
const out: AssetMount[] = [];
|
|
49
|
+
for (const mount of declared) {
|
|
50
|
+
const dir = fwd(relative(root, mount.dir));
|
|
51
|
+
if (dir === "" || dir.startsWith("../") || /^[A-Za-z]:/.test(dir)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
out.push({ dir, urlPrefix: mount.urlPrefix });
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
package/src/refactor/paths.ts
CHANGED
|
@@ -6,8 +6,43 @@
|
|
|
6
6
|
* them in. The relative-path style mirrors `computeRelativePath` in the studio
|
|
7
7
|
* (packages/studio/src/files/components.ts): "./" for same-dir/descendant targets, "../"-chains
|
|
8
8
|
* otherwise.
|
|
9
|
+
*
|
|
10
|
+
* **A rooted reference is a site URL, not a project path.** `/images/hero.jpg` names whichever file
|
|
11
|
+
* the host serves at that URL, and there are three places it can live: an extension's asset mount,
|
|
12
|
+
* the project root, and `public/`. This module does not re-derive that mapping — it calls
|
|
13
|
+
* `@jxsuite/schema/asset-paths`, the same lane math the canvas, the dev server and the build all
|
|
14
|
+
* use. Resolving a rooted ref against the project root alone was how a rename of any file under
|
|
15
|
+
* `public/` rewrote nothing while promising otherwise (issue 239).
|
|
16
|
+
*
|
|
17
|
+
* @docs studio/projects/pages-layouts-components
|
|
9
18
|
*/
|
|
10
19
|
|
|
20
|
+
import { BUILD_LANES, projectPathsForSiteUrl, siteUrlForPath } from "@jxsuite/schema/asset-paths";
|
|
21
|
+
import type { AssetLane, AssetMount } from "@jxsuite/schema/asset-paths";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The lanes a rewritten site URL is re-emitted through, most-publishable first.
|
|
25
|
+
*
|
|
26
|
+
* {@link BUILD_LANES} first, so a file that a deployed build publishes keeps the URL that build
|
|
27
|
+
* serves — `public/images/hero.jpg` re-emits as `/images/hero.jpg`, not the `/public/...` form the
|
|
28
|
+
* dev server would also answer. The `root` lane is the fallback for a file only the dev server can
|
|
29
|
+
* reach: that reference was already build-broken before the rename (the server says so at request
|
|
30
|
+
* time), and preserving it is strictly better than leaving it pointing at a file that has moved.
|
|
31
|
+
*
|
|
32
|
+
* The consequence is that this is total for any non-empty in-project path — a rename can always
|
|
33
|
+
* express its destination — so there is no "unrewritable reference" case to report.
|
|
34
|
+
*/
|
|
35
|
+
const AUTHORED_LANES: readonly AssetLane[] = [...BUILD_LANES, "root"];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The lanes a rooted reference is READ through: every place the dev server would look.
|
|
39
|
+
*
|
|
40
|
+
* Deliberately wider than {@link AUTHORED_LANES} and unordered in effect, because the reader checks
|
|
41
|
+
* all of them. Where two lanes both name an existing file, `/hero.jpg` genuinely is ambiguous, and
|
|
42
|
+
* counting both is the safe side of a question asked to warn someone before a delete.
|
|
43
|
+
*/
|
|
44
|
+
const READ_LANES: readonly AssetLane[] = ["mounts", "root", "public"];
|
|
45
|
+
|
|
11
46
|
/** Classification of a reference string value. */
|
|
12
47
|
export type RefClass =
|
|
13
48
|
| { kind: "none" }
|
|
@@ -127,6 +162,67 @@ export function remapTarget(targetOld: string, oldAbs: string, newAbs: string):
|
|
|
127
162
|
return targetOld;
|
|
128
163
|
}
|
|
129
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Every project-relative path a rooted (site-URL) reference could name, most-preferred first.
|
|
167
|
+
*
|
|
168
|
+
* @param {string} core - The reference with any `?query`/`#fragment` already split off
|
|
169
|
+
* @param {readonly AssetMount[]} mounts - Project-relative asset mounts
|
|
170
|
+
* @returns {string[]} Candidate project-relative paths
|
|
171
|
+
*/
|
|
172
|
+
export function projectPathsForRootedRef(core: string, mounts: readonly AssetMount[]): string[] {
|
|
173
|
+
return projectPathsForSiteUrl(core, mounts, READ_LANES);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** A final path segment must carry an extension, and the extension must contain a letter. */
|
|
177
|
+
const FILE_SEGMENT_RE = /^[^.][^/]*\.[A-Za-z0-9]{0,7}[A-Za-z][A-Za-z0-9]{0,7}$/;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* True when a string is shaped like a file a document could reference.
|
|
181
|
+
*
|
|
182
|
+
* The walker in refs.ts knows eleven keys that carry references by name. The commonest media
|
|
183
|
+
* reference in a real project is under none of them — a schema-typed component prop (`$props.bg`),
|
|
184
|
+
* a content entry's frontmatter (`cover:`), `project.json`'s `defaults.layout` — so the walker
|
|
185
|
+
* falls back to SHAPE for every other key, and this is the shape test.
|
|
186
|
+
*
|
|
187
|
+
* It is deliberately not schema-aware. `walkDocRefs` is pure and schema-blind by design, and the
|
|
188
|
+
* write side shares it; a schema oracle would have to be threaded through `applyRename` too, would
|
|
189
|
+
* need a project-wide `tagName → media prop` index from a second pass, and would still miss a prop
|
|
190
|
+
* whose component definition failed to parse.
|
|
191
|
+
*
|
|
192
|
+
* Precision does not come from here. It comes from the resolve-and-compare gate the callers already
|
|
193
|
+
* apply: a candidate only counts when it resolves to the exact file queried. This just has to be
|
|
194
|
+
* cheap and to refuse prose. The extension requirement is what does that — it rejects routes
|
|
195
|
+
* (`/pricing`), bare words, MIME types (`image/png`) and identifiers, and the letter-in-extension
|
|
196
|
+
* rule additionally rejects version strings (`1.2.3`), which are otherwise file-shaped.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} value
|
|
199
|
+
* @returns {boolean}
|
|
200
|
+
*/
|
|
201
|
+
export function looksLikeFileRef(value: string): boolean {
|
|
202
|
+
if (value.length === 0 || /\s/.test(value) || value.includes("${")) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
const cls = classifyRef(value);
|
|
206
|
+
if (cls.kind !== "path") {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
const last = cls.core.slice(cls.core.lastIndexOf("/") + 1);
|
|
210
|
+
return FILE_SEGMENT_RE.test(last);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Restore a trailing slash the path math dropped, when the author wrote one.
|
|
215
|
+
*
|
|
216
|
+
* `normalizeSegments` discards empty segments, so `./content/posts/` resolves and re-emits as
|
|
217
|
+
* `./content/posts`. For a file that is invisible; for a DIRECTORY reference — a content
|
|
218
|
+
* collection's `source` — it is a gratuitous edit to a line the rename had no business restyling,
|
|
219
|
+
* and `rewriteRef`'s contract is to preserve the authored style. A bare `/` is left alone: it is
|
|
220
|
+
* already only a slash.
|
|
221
|
+
*/
|
|
222
|
+
function keepTrailingSlash(out: string, core: string): string {
|
|
223
|
+
return core.endsWith("/") && core !== "/" && !out.endsWith("/") ? `${out}/` : out;
|
|
224
|
+
}
|
|
225
|
+
|
|
130
226
|
/** Context describing a single rename, shared by every reference rewrite within one document. */
|
|
131
227
|
export interface RemapCtx {
|
|
132
228
|
/** Absolute POSIX project root. */
|
|
@@ -142,6 +238,11 @@ export interface RemapCtx {
|
|
|
142
238
|
* the document itself is inside the moved subtree).
|
|
143
239
|
*/
|
|
144
240
|
docNewDir: string;
|
|
241
|
+
/**
|
|
242
|
+
* Project-relative asset mounts, for resolving rooted references through the mount lane. Absent
|
|
243
|
+
* is the same as none, which leaves the `root` and `public/` lanes.
|
|
244
|
+
*/
|
|
245
|
+
mounts?: readonly AssetMount[];
|
|
145
246
|
}
|
|
146
247
|
|
|
147
248
|
/**
|
|
@@ -158,30 +259,63 @@ export function rewriteRef(
|
|
|
158
259
|
rootRelativeBare = false,
|
|
159
260
|
): string | null {
|
|
160
261
|
const { core, rooted, suffix } = cls;
|
|
262
|
+
if (rooted) {
|
|
263
|
+
return rewriteSiteUrl(core, suffix, ctx);
|
|
264
|
+
}
|
|
161
265
|
const startsRel = core.startsWith("./") || core.startsWith("../");
|
|
162
|
-
const useRoot =
|
|
266
|
+
const useRoot = rootRelativeBare && !startsRel;
|
|
163
267
|
const baseOld = useRoot ? ctx.root : ctx.docOldDir;
|
|
164
268
|
const baseNew = useRoot ? ctx.root : ctx.docNewDir;
|
|
165
269
|
|
|
166
|
-
const targetOld = joinPosix(
|
|
270
|
+
const targetOld = joinPosix(baseOld, core);
|
|
167
271
|
const targetNew = remapTarget(targetOld, ctx.oldAbs, ctx.newAbs);
|
|
168
272
|
if (targetNew === targetOld && baseOld === baseNew) {
|
|
169
273
|
return null;
|
|
170
274
|
}
|
|
171
275
|
|
|
276
|
+
const rel = relativeChain(baseNew, targetNew);
|
|
172
277
|
let out: string;
|
|
173
|
-
if (
|
|
174
|
-
out =
|
|
278
|
+
if (rel.startsWith("..")) {
|
|
279
|
+
out = rel;
|
|
280
|
+
} else if (core.startsWith("./")) {
|
|
281
|
+
out = `./${rel}`;
|
|
175
282
|
} else {
|
|
176
|
-
|
|
177
|
-
if (rel.startsWith("..")) {
|
|
178
|
-
out = rel;
|
|
179
|
-
} else if (core.startsWith("./")) {
|
|
180
|
-
out = `./${rel}`;
|
|
181
|
-
} else {
|
|
182
|
-
out = rel;
|
|
183
|
-
}
|
|
283
|
+
out = rel;
|
|
184
284
|
}
|
|
185
|
-
out
|
|
285
|
+
out = keepTrailingSlash(out, core) + suffix;
|
|
186
286
|
return out === core + suffix ? null : out;
|
|
187
287
|
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Rewrite a rooted reference — a site URL — across a rename, through the lanes that give it
|
|
291
|
+
* meaning.
|
|
292
|
+
*
|
|
293
|
+
* Each lane is tried on its own so the MATCHED one is known: a lane is only a candidate when the
|
|
294
|
+
* file it names is the one that moved. The answer is then re-derived from the destination through
|
|
295
|
+
* {@link AUTHORED_LANES}, which is what turns a `public/` move into the right URL rather than a
|
|
296
|
+
* root-relative path that would 404 on the deployed site.
|
|
297
|
+
*
|
|
298
|
+
* Returns null when no lane names the moved file, which is the overwhelmingly common case — most
|
|
299
|
+
* references in a document have nothing to do with any given rename.
|
|
300
|
+
*/
|
|
301
|
+
function rewriteSiteUrl(core: string, suffix: string, ctx: RemapCtx): string | null {
|
|
302
|
+
const mounts = ctx.mounts ?? [];
|
|
303
|
+
for (const lane of READ_LANES) {
|
|
304
|
+
const [candidate] = projectPathsForSiteUrl(core, mounts, [lane]);
|
|
305
|
+
if (candidate === undefined) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const targetOld = joinPosix(ctx.root, candidate);
|
|
309
|
+
const targetNew = remapTarget(targetOld, ctx.oldAbs, ctx.newAbs);
|
|
310
|
+
if (targetNew === targetOld) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const url = siteUrlForPath(relativeChain(ctx.root, targetNew), mounts, AUTHORED_LANES);
|
|
314
|
+
if (url === null) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const out = keepTrailingSlash(url, core) + suffix;
|
|
318
|
+
return out === core + suffix ? null : out;
|
|
319
|
+
}
|
|
320
|
+
return null;
|
|
321
|
+
}
|