deictic 0.0.1 → 0.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/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/annotations/client.d.ts +4 -0
- package/dist/annotations/client.d.ts.map +1 -0
- package/dist/annotations/client.js +497 -0
- package/dist/annotations/client.js.map +1 -0
- package/dist/annotations/index.d.ts +110 -0
- package/dist/annotations/index.d.ts.map +1 -0
- package/dist/annotations/index.js +22 -0
- package/dist/annotations/index.js.map +1 -0
- package/dist/astro.d.ts +29 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +24 -0
- package/dist/astro.js.map +1 -0
- package/dist/bun.d.ts +12 -0
- package/dist/bun.d.ts.map +1 -0
- package/dist/bun.js +67 -0
- package/dist/bun.js.map +1 -0
- package/dist/client/index.d.ts +93 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +1329 -0
- package/dist/client/index.js.map +1 -0
- package/dist/endpoints.js +354 -0
- package/dist/endpoints.js.map +1 -0
- package/dist/index.d.ts +220 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/index2.d.ts +23 -0
- package/dist/index3.d.ts +98 -0
- package/dist/index3.d.ts.map +1 -0
- package/dist/index4.d.ts +74 -0
- package/dist/index4.d.ts.map +1 -0
- package/dist/index5.d.ts +70 -0
- package/dist/index5.d.ts.map +1 -0
- package/dist/integration.d.ts +19 -0
- package/dist/integration.d.ts.map +1 -0
- package/dist/integration.js +277 -0
- package/dist/integration.js.map +1 -0
- package/dist/kept.js +23 -0
- package/dist/kept.js.map +1 -0
- package/dist/loader.d.ts +15 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +17 -0
- package/dist/loader.js.map +1 -0
- package/dist/marks.js +14 -0
- package/dist/marks.js.map +1 -0
- package/dist/next.d.ts +17 -0
- package/dist/next.d.ts.map +1 -0
- package/dist/next.js +150 -0
- package/dist/next.js.map +1 -0
- package/dist/playwright.js +105 -0
- package/dist/playwright.js.map +1 -0
- package/dist/recommended/index.d.ts +25 -0
- package/dist/recommended/index.d.ts.map +1 -0
- package/dist/recommended/index.js +19 -0
- package/dist/recommended/index.js.map +1 -0
- package/dist/recordings/client.d.ts +4 -0
- package/dist/recordings/client.d.ts.map +1 -0
- package/dist/recordings/client.js +376 -0
- package/dist/recordings/client.js.map +1 -0
- package/dist/recordings/index.d.ts +2 -0
- package/dist/recordings/index.js +3 -0
- package/dist/recordings.js +235 -0
- package/dist/recordings.js.map +1 -0
- package/dist/screenshots/client.d.ts +23 -0
- package/dist/screenshots/client.d.ts.map +1 -0
- package/dist/screenshots/client.js +76 -0
- package/dist/screenshots/client.js.map +1 -0
- package/dist/screenshots/dom.d.ts +4 -0
- package/dist/screenshots/dom.d.ts.map +1 -0
- package/dist/screenshots/dom.js +92 -0
- package/dist/screenshots/dom.js.map +1 -0
- package/dist/screenshots/index.d.ts +2 -0
- package/dist/screenshots/index.js +98 -0
- package/dist/screenshots/index.js.map +1 -0
- package/dist/screenshots/rrweb.d.ts +9 -0
- package/dist/screenshots/rrweb.d.ts.map +1 -0
- package/dist/screenshots/rrweb.js +60 -0
- package/dist/screenshots/rrweb.js.map +1 -0
- package/dist/shortcut.js +63 -0
- package/dist/shortcut.js.map +1 -0
- package/dist/tailwind/client.d.ts +4 -0
- package/dist/tailwind/client.d.ts.map +1 -0
- package/dist/tailwind/client.js +613 -0
- package/dist/tailwind/client.js.map +1 -0
- package/dist/tailwind/index.d.ts +2 -0
- package/dist/tailwind/index.js +557 -0
- package/dist/tailwind/index.js.map +1 -0
- package/dist/tasks/client.d.ts +6 -0
- package/dist/tasks/client.d.ts.map +1 -0
- package/dist/tasks/client.js +642 -0
- package/dist/tasks/client.js.map +1 -0
- package/dist/tasks/index.d.ts +159 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +348 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/vite.d.ts +17 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +44 -0
- package/dist/vite.js.map +1 -0
- package/dist/voice/client.d.ts +4 -0
- package/dist/voice/client.d.ts.map +1 -0
- package/dist/voice/client.js +66 -0
- package/dist/voice/client.js.map +1 -0
- package/dist/voice/index.d.ts +47 -0
- package/dist/voice/index.d.ts.map +1 -0
- package/dist/voice/index.js +74 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/waveform.js +93 -0
- package/dist/waveform.js.map +1 -0
- package/dist/webpack.d.ts +50 -0
- package/dist/webpack.d.ts.map +1 -0
- package/dist/webpack.js +78 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +307 -7
- package/skills/deictic/SKILL.md +47 -0
- package/skills/deictic/references/install.md +81 -0
- package/skills/deictic/references/recordings.md +83 -0
- package/skills/deictic/references/tasks.md +79 -0
- package/skills/deictic/references/watch.md +43 -0
- package/skills/deictic/scripts/frame.mjs +62 -0
- package/skills/deictic/scripts/lib.mjs +193 -0
- package/skills/deictic/scripts/recording.mjs +186 -0
- package/skills/deictic/scripts/task.mjs +84 -0
- package/skills/deictic/scripts/transcribe.mjs +93 -0
- package/skills/deictic/scripts/voice.mjs +50 -0
- package/skills/deictic/scripts/watch.mjs +51 -0
- package/src/astro.ts +34 -0
- package/src/bun.ts +79 -0
- package/src/client/index.ts +833 -0
- package/src/client/lightbox.ts +256 -0
- package/src/client/style.ts +243 -0
- package/src/client/tooltip.ts +78 -0
- package/src/client/waveform.ts +98 -0
- package/src/index.ts +240 -0
- package/src/loader.ts +28 -0
- package/src/next.ts +133 -0
- package/src/plugins/annotations/client.ts +301 -0
- package/src/plugins/annotations/draw.ts +169 -0
- package/src/plugins/annotations/index.ts +105 -0
- package/src/plugins/annotations/svg.ts +61 -0
- package/src/plugins/recommended/index.ts +35 -0
- package/src/plugins/recordings/client.ts +315 -0
- package/src/plugins/recordings/index.ts +174 -0
- package/src/plugins/recordings/marks.ts +11 -0
- package/src/plugins/recordings/ours.ts +70 -0
- package/src/plugins/recordings/replay.ts +139 -0
- package/src/plugins/screenshots/client.ts +63 -0
- package/src/plugins/screenshots/dom.ts +79 -0
- package/src/plugins/screenshots/index.ts +194 -0
- package/src/plugins/screenshots/playwright.ts +119 -0
- package/src/plugins/screenshots/rrweb.ts +45 -0
- package/src/plugins/tailwind/catalog.ts +396 -0
- package/src/plugins/tailwind/client.ts +669 -0
- package/src/plugins/tailwind/index.ts +267 -0
- package/src/plugins/tasks/client.ts +622 -0
- package/src/plugins/tasks/index.ts +513 -0
- package/src/plugins/voice/client.ts +57 -0
- package/src/plugins/voice/index.ts +114 -0
- package/src/shortcut.ts +83 -0
- package/src/tools/astro.ts +81 -0
- package/src/tools/editor.ts +22 -0
- package/src/tools/endpoints.ts +163 -0
- package/src/tools/history.ts +203 -0
- package/src/tools/integration.ts +106 -0
- package/src/tools/kept.ts +21 -0
- package/src/tools/locate.ts +94 -0
- package/src/tools/plugins.ts +41 -0
- package/src/tools/source.ts +72 -0
- package/src/vite.ts +62 -0
- package/src/webpack.ts +107 -0
- package/index.js +0 -1
package/src/index.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic`: what a plugin is. Every plugin is optional: deictic itself is the glue (the
|
|
3
|
+
* transform that ties elements to source, the dev server's endpoints, the history, shortcuts and the
|
|
4
|
+
* overlay's panel), and each plugin adds what it does on top.
|
|
5
|
+
*
|
|
6
|
+
* A plugin has two halves, each optional that meet over the dev server's endpoints (`tools/endpoints.ts`). The
|
|
7
|
+
* server half runs beside the bundler and is the only one that reads source: it turns the element
|
|
8
|
+
* under the pointer into a model its panel can show, and an op from that panel into text edits. It
|
|
9
|
+
* never writes files; deictic does, so every plugin's changes land in one history and undo the
|
|
10
|
+
* same way. A change that isn't to a file (a task added to a board) the plugin makes itself and
|
|
11
|
+
* answers with what it did (`Done`): the history keeps that beside the files' changes and hands it
|
|
12
|
+
* back to the plugin's own `undo` and `redo`.
|
|
13
|
+
* The client half is a module the page loads, named by the server half's `client`, which draws the
|
|
14
|
+
* panel and previews an op on the page before it is applied.
|
|
15
|
+
*
|
|
16
|
+
* A plugin may have no panel at all, only an API for others (screenshots, a recording) and routes of
|
|
17
|
+
* its own on the dev server. Each half's `setup` answers its API, and `use(name)` reaches another
|
|
18
|
+
* plugin's, on the same side; `needs` names the plugins it can't do without.
|
|
19
|
+
*
|
|
20
|
+
* Both halves import from here, so the parser is imported for its types only: the browser must not load it.
|
|
21
|
+
*/
|
|
22
|
+
import type { JSXOpeningElement, Program } from 'oxc-parser';
|
|
23
|
+
|
|
24
|
+
import type { Shortcut } from './shortcut.ts';
|
|
25
|
+
|
|
26
|
+
export type { Modifier, Shortcut } from './shortcut.ts';
|
|
27
|
+
|
|
28
|
+
/** A place in the source: the file from the project's root, with `/`, and the line and column, from 1. */
|
|
29
|
+
export interface Loc {
|
|
30
|
+
file: string;
|
|
31
|
+
line: number;
|
|
32
|
+
column: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** An element the pointer picked: where its opening tag starts, and the tag, which the server checks is still there. */
|
|
36
|
+
export interface Target {
|
|
37
|
+
loc: Loc;
|
|
38
|
+
tag: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Text in a file to replace, by UTF-16 offsets. `file` is the target's unless named, from the project's root. */
|
|
42
|
+
export interface TextEdit {
|
|
43
|
+
file?: string;
|
|
44
|
+
start: number;
|
|
45
|
+
end: number;
|
|
46
|
+
text: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What a server half reads: the target's file, parsed, and its element. */
|
|
50
|
+
export interface Source {
|
|
51
|
+
/** The target's file, from the project's root. */
|
|
52
|
+
file: string;
|
|
53
|
+
code: string;
|
|
54
|
+
/** The file's module; for an `.astro` component, its frontmatter's. */
|
|
55
|
+
program: Program;
|
|
56
|
+
/** The target's opening tag. */
|
|
57
|
+
element: JSXOpeningElement;
|
|
58
|
+
/** Another file under the project's root, for plugins whose edits go elsewhere (a locale's messages). */
|
|
59
|
+
read(file: string): string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** An op a plugin won't make. The message says why and what to do instead; the panel shows it. */
|
|
63
|
+
export class Refusal extends Error {
|
|
64
|
+
override name = 'Refusal';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A change the plugin made itself, not to a file: what its `undo` and `redo` need to take it back and
|
|
69
|
+
* make it again. JSON, since the history keeps it on disk across restarts of the dev server.
|
|
70
|
+
*/
|
|
71
|
+
export interface Done<Record = unknown> {
|
|
72
|
+
done: Record;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Maybe<T> = T | Promise<T>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Each plugin's API to the others on the server, by name. A plugin adds its own, so `use` knows its type:
|
|
79
|
+
* `declare module 'deictic' { interface ServerApis { screenshots: Screenshots } }`.
|
|
80
|
+
*/
|
|
81
|
+
// oxlint-disable-next-line typescript/no-empty-interface -- filled by declaration merging
|
|
82
|
+
export interface ServerApis {}
|
|
83
|
+
|
|
84
|
+
/** The same, in the page: `declare module 'deictic' { interface ClientApis { screenshots: Capture } }`. */
|
|
85
|
+
// oxlint-disable-next-line typescript/no-empty-interface -- filled by declaration merging
|
|
86
|
+
export interface ClientApis {}
|
|
87
|
+
|
|
88
|
+
/** A plugin's API, by its name: what its package declared, or `unknown` for one that declared none. */
|
|
89
|
+
export type ApiOf<Apis, Name extends string> = Name extends keyof Apis ? Apis[Name] : unknown;
|
|
90
|
+
|
|
91
|
+
export interface ServerContext {
|
|
92
|
+
/** The project's root, as an absolute path. */
|
|
93
|
+
root: string;
|
|
94
|
+
/**
|
|
95
|
+
* Another plugin's API, as its `setup` answered it, or nothing when it isn't loaded. One the plugin
|
|
96
|
+
* `needs` always is; any other may not be, so check.
|
|
97
|
+
*/
|
|
98
|
+
use: <Name extends string>(name: Name) => ApiOf<ServerApis, Name> | undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A route of the plugin's own, at `/__deictic/plugins/<name>/<route>`, and below it. A `GET` must
|
|
103
|
+
* change nothing: anything else reaches it only from the page itself, which deictic checks.
|
|
104
|
+
*/
|
|
105
|
+
export type Route = (request: Request, context: ServerContext) => Maybe<Response>;
|
|
106
|
+
|
|
107
|
+
export interface ServerPlugin<Model = unknown, Op = unknown, Record = unknown, Api = unknown> {
|
|
108
|
+
/** Ties the halves together; also the label of its changes in the history, and its routes' path. Lowercase, with `-`. */
|
|
109
|
+
name: string;
|
|
110
|
+
/** The module the page loads for this plugin's half there, which default-exports its `ClientPlugin`. */
|
|
111
|
+
client?: string;
|
|
112
|
+
/** What the page half gets as `context.options`: the user's options it needs there, as JSON. */
|
|
113
|
+
clientOptions?: unknown;
|
|
114
|
+
/** What opens its panel: its own default, which its options let the user change. A plugin with no panel has none. */
|
|
115
|
+
shortcut?: Shortcut;
|
|
116
|
+
/** The plugins it can't do without, by name: the dev server won't start without them. */
|
|
117
|
+
needs?: string[];
|
|
118
|
+
/** Runs once, as the dev server starts and before it answers anything. Answers the plugin's API to the others. */
|
|
119
|
+
setup?(context: ServerContext): Maybe<Api>;
|
|
120
|
+
/** Routes of its own, by their first segment under `/__deictic/plugins/<name>/`. */
|
|
121
|
+
routes?: { [route: string]: Route };
|
|
122
|
+
/** The model its panel shows for an element. Without it, the panel gets none. */
|
|
123
|
+
read?(source: Source, target: Target): Maybe<Model>;
|
|
124
|
+
/** Text edits for deictic to write, or a change the plugin made itself. */
|
|
125
|
+
apply?(source: Source, target: Target, op: Op): Maybe<TextEdit[] | Done<Record>>;
|
|
126
|
+
/**
|
|
127
|
+
* Takes back a change `apply` made itself, or throws a `Refusal` saying why it can't any more. It may
|
|
128
|
+
* answer what the next redo needs, where that differs (what it put back has a new ID).
|
|
129
|
+
*/
|
|
130
|
+
undo?(record: Record): Maybe<void | Record>;
|
|
131
|
+
/** Makes it again after an undo, and answers what the next undo needs, which may differ (a new ID). */
|
|
132
|
+
redo?(record: Record): Maybe<Record>;
|
|
133
|
+
/** Says what an op does, for the history: `className: px-2 → px-4`. */
|
|
134
|
+
describe?(op: Op, before: Model): string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const definePlugin = <Model, Op, Record = unknown, Api = unknown>(
|
|
138
|
+
plugin: ServerPlugin<Model, Op, Record, Api>,
|
|
139
|
+
): ServerPlugin<Model, Op, Record, Api> => plugin;
|
|
140
|
+
|
|
141
|
+
export interface ClientContext {
|
|
142
|
+
/** The dev server's base URL, ending in `/`. */
|
|
143
|
+
base: string;
|
|
144
|
+
/** deictic's shadow root, to draw in: neither the page's styles nor the overlay's cross over. */
|
|
145
|
+
root: ShadowRoot;
|
|
146
|
+
/** The server half's `clientOptions`, or nothing. */
|
|
147
|
+
options: unknown;
|
|
148
|
+
/** Another plugin's API in the page, as its `setup` answered it, or nothing when it isn't loaded. */
|
|
149
|
+
use: <Name extends string>(name: Name) => ApiOf<ClientApis, Name> | undefined;
|
|
150
|
+
/** Asks one of this plugin's own routes, from the page: `request('shots', { method: 'POST', body: blob })`. */
|
|
151
|
+
request: (route: string, init?: RequestInit) => Promise<Response>;
|
|
152
|
+
/**
|
|
153
|
+
* Opens the popup on an element at a plugin's tab, as its shortcut would: after a recording, say. An
|
|
154
|
+
* element rendered again since stands in for one no longer on the page. False when there's no such tab
|
|
155
|
+
* or no such element any more. Not from `setup`, which runs before the popup can open.
|
|
156
|
+
*/
|
|
157
|
+
open: (plugin: string, element: Element) => boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Shows `items` in the lightbox over everything, at `at` (the first by default), stepping between them with
|
|
160
|
+
* ← and →. With `from`, it grows out of that element (the chip clicked) and shrinks back into it on close.
|
|
161
|
+
*/
|
|
162
|
+
lightbox: (items: LightboxItem[], options?: LightboxOptions) => { close: () => void };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** What the lightbox shows: a picture, a page in a frame (a recording's replay), or a sound with its waveform. */
|
|
166
|
+
export type LightboxItem = {
|
|
167
|
+
/** Its name, over it. */
|
|
168
|
+
title: string;
|
|
169
|
+
/** Buttons beside the name: each closes the lightbox and runs. */
|
|
170
|
+
actions?: LightboxAction[];
|
|
171
|
+
} & ({ kind: 'image'; url: string } | { kind: 'page'; url: string } | { kind: 'audio'; url: string });
|
|
172
|
+
|
|
173
|
+
export interface LightboxAction {
|
|
174
|
+
label: string;
|
|
175
|
+
title?: string;
|
|
176
|
+
run: () => void;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface LightboxOptions {
|
|
180
|
+
/** The item it opens at. */
|
|
181
|
+
at?: number;
|
|
182
|
+
/** The element it grows out of, and goes back into. */
|
|
183
|
+
from?: Element;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface PanelContext<Model, Op> extends ClientContext {
|
|
187
|
+
/** The element clicked: the one the source marked, under the pointer or around what is. */
|
|
188
|
+
element: Element;
|
|
189
|
+
/**
|
|
190
|
+
* What was under the pointer, where it isn't `element` but inside it: markup nothing marked, as a
|
|
191
|
+
* dependency's, or in an open shadow root, as the popup's own on a page that shows it (`popupIn`).
|
|
192
|
+
*/
|
|
193
|
+
pointed?: Element;
|
|
194
|
+
/** Every element on the page from the same place in the source, the clicked one among them: the rows of a list. */
|
|
195
|
+
elements: Element[];
|
|
196
|
+
target: Target;
|
|
197
|
+
model: Model;
|
|
198
|
+
/** Applies an op on disk, and reads the model again. Rejects with the server's reason, which the panel also shows. */
|
|
199
|
+
apply(op: Op): Promise<Model>;
|
|
200
|
+
close: () => void;
|
|
201
|
+
/**
|
|
202
|
+
* Says there's something here that leaving would lose (words typed, a change tried on the page), and the button
|
|
203
|
+
* that keeps it: a click outside the popup then shakes that button, where it would have closed the popup.
|
|
204
|
+
* `undefined` once there's nothing to lose. Forgotten as the panel unmounts.
|
|
205
|
+
*/
|
|
206
|
+
unsaved: (action: HTMLElement | undefined) => void;
|
|
207
|
+
/**
|
|
208
|
+
* Leaves the popup to do something on the page, then comes back: closes it, runs `task` (a recording, a
|
|
209
|
+
* pick of another element), and opens it again at this tab on this element. The next mount gets
|
|
210
|
+
* `resumed`: the `state` kept now (what was typed, what's attached), and what `task` answered, or why
|
|
211
|
+
* it failed. The state stays in the page, as it is: no need for JSON.
|
|
212
|
+
*/
|
|
213
|
+
away: <T>(state: unknown, task: () => Promise<T>) => void;
|
|
214
|
+
/** Set when this mount is the panel coming back from `away`. */
|
|
215
|
+
resumed?: Resumed;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** What a panel coming back from `away` gets: the state it kept, and the task's result or problem. */
|
|
219
|
+
export interface Resumed {
|
|
220
|
+
state: unknown;
|
|
221
|
+
result?: unknown;
|
|
222
|
+
/** Why the task failed: its error's message. */
|
|
223
|
+
problem?: string;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface ClientPlugin<Model = unknown, Op = unknown, Api = unknown> {
|
|
227
|
+
/** The server half's `name`. */
|
|
228
|
+
name: string;
|
|
229
|
+
/** Shown over the element while its shortcut's keys are held, and atop the panel. */
|
|
230
|
+
label?: string;
|
|
231
|
+
/**
|
|
232
|
+
* Runs once, as the page starts, before any panel opens (a recording starts here). Answers the
|
|
233
|
+
* plugin's API to the others in the page, at once: one that needs time keeps its promises inside it.
|
|
234
|
+
*/
|
|
235
|
+
setup?(context: ClientContext): Api;
|
|
236
|
+
/** Draws into `panel`, inside deictic's shadow root. Returns what to undo on the page when the panel closes. */
|
|
237
|
+
mount?: (panel: HTMLElement, context: PanelContext<Model, Op>) => (() => void) | void;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export const defineClientPlugin = <Model, Op, Api = unknown>(plugin: ClientPlugin<Model, Op, Api>): ClientPlugin<Model, Op, Api> => plugin;
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loader `deictic/webpack` sets for webpack and Rspack, and `deictic/next` for Turbopack: it marks a
|
|
3
|
+
* JSX module's host elements (`tools/locate.ts`), and in the browser appends the import of the client
|
|
4
|
+
* module written to `.deictic/client.js`. It runs `pre`, before the app's own loaders, so lines and
|
|
5
|
+
* columns are the file's as written. Its options are JSON, as Turbopack's must be.
|
|
6
|
+
*/
|
|
7
|
+
import { importOf, mark } from './tools/integration.ts';
|
|
8
|
+
|
|
9
|
+
export interface LoaderOptions {
|
|
10
|
+
/** The project's root, which a mark's path is from. */
|
|
11
|
+
root: string;
|
|
12
|
+
/** The written client module, to import in the browser; none on the server. */
|
|
13
|
+
client?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Context {
|
|
17
|
+
getOptions(): LoaderOptions;
|
|
18
|
+
resourcePath: string;
|
|
19
|
+
sourceMap?: boolean;
|
|
20
|
+
callback(error: Error | null, code?: string, map?: object): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function loader(this: Context, source: string): void {
|
|
24
|
+
const { root, client } = this.getOptions();
|
|
25
|
+
const marked = mark(source, this.resourcePath, root, client === undefined ? '' : importOf(client, this.resourcePath));
|
|
26
|
+
if (marked === undefined) return this.callback(null, source);
|
|
27
|
+
this.callback(null, marked.code, this.sourceMap === false ? undefined : marked.map);
|
|
28
|
+
}
|
package/src/next.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic/next`: deictic in `next dev`, on Turbopack or webpack (`--webpack`). Nothing runs in `next build`.
|
|
3
|
+
*
|
|
4
|
+
* Turbopack has no transform hook and no virtual modules, so a loader rule (`loader.ts`) marks each JSX
|
|
5
|
+
* module, on the server too, which marks server components' elements in the HTML they render; in the
|
|
6
|
+
* browser it also imports the client module, written to `.deictic/client.js`. Only code the browser
|
|
7
|
+
* loads can start the overlay, so a page with no client component at all has none. Under webpack the
|
|
8
|
+
* same loader is a `pre` rule, as in `webpack.ts`.
|
|
9
|
+
*
|
|
10
|
+
* The endpoints can't be a route of the app without a file in it, so a server of their own answers them
|
|
11
|
+
* on a local port, and a rewrite sends `/__deictic/*` there, before any of the app's own; Next's proxy
|
|
12
|
+
* forwards the page's host, so writes are still checked against its origin. A place opens through
|
|
13
|
+
* Next's own `/__nextjs_launch-editor`.
|
|
14
|
+
*
|
|
15
|
+
* Both bundlers' rules are always set, as the config can't tell which will run: Next refuses a Turbopack
|
|
16
|
+
* build whose config has a `webpack` key only where it has no `turbopack` one, and ours always does.
|
|
17
|
+
*/
|
|
18
|
+
import { createServer } from 'node:http';
|
|
19
|
+
import type { AddressInfo } from 'node:net';
|
|
20
|
+
import { resolve } from 'node:path';
|
|
21
|
+
|
|
22
|
+
import type { ServerPlugin } from './index.ts';
|
|
23
|
+
import type { LoaderOptions } from './loader.ts';
|
|
24
|
+
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
25
|
+
import { beside, type DeicticOptions, prepare, writeClient } from './tools/integration.ts';
|
|
26
|
+
import { JSX } from './tools/locate.ts';
|
|
27
|
+
|
|
28
|
+
export interface DeicticNextOptions extends DeicticOptions {
|
|
29
|
+
/** The project's root, which marks and edits are from: the working directory `next dev` runs in by default. */
|
|
30
|
+
root?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A Turbopack rule: ours, or whatever the app has. */
|
|
34
|
+
type Rule = { condition?: unknown; loaders: { loader: string; options: LoaderOptions }[] } | object;
|
|
35
|
+
type Rules = Record<string, Rule | Rule[]>;
|
|
36
|
+
type Rewrite = { source: string; destination: string };
|
|
37
|
+
type Rewrites = Rewrite[] | { beforeFiles?: Rewrite[]; afterFiles?: Rewrite[]; fallback?: Rewrite[] };
|
|
38
|
+
/** What Next's `webpack` option reads and answers, as far as it's used here. Not async: Next doesn't wait for one. */
|
|
39
|
+
type Webpack = (config: { module: { rules: unknown[] } }, context: { isServer: boolean }) => { module: { rules: unknown[] } };
|
|
40
|
+
type ConfigFunction<C> = (phase: string, context: { defaultConfig: C }) => C | Promise<C>;
|
|
41
|
+
|
|
42
|
+
const DEV = 'phase-development-server';
|
|
43
|
+
const loaderPath = beside(import.meta.url, 'loader');
|
|
44
|
+
const GLOBS = ['*.tsx', '*.jsx', '*.js'];
|
|
45
|
+
|
|
46
|
+
// Next loads its config more than once in a process: one endpoints server per project is enough.
|
|
47
|
+
const serving = new Map<string, Promise<number>>();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Wraps the Next config: `export default withDeictic({ ... }, { plugins: [tailwind()] })`. Takes the
|
|
51
|
+
* config as an object or as the function Next calls with the phase, and returns the function.
|
|
52
|
+
*/
|
|
53
|
+
export function withDeictic<C extends object>(
|
|
54
|
+
next: C | ConfigFunction<C> = {} as C,
|
|
55
|
+
options: DeicticNextOptions = {},
|
|
56
|
+
): (phase: string, context: { defaultConfig: C }) => Promise<C> {
|
|
57
|
+
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
58
|
+
const { plugins, shortcut } = prepare(options);
|
|
59
|
+
return async (phase, context) => {
|
|
60
|
+
const resolved = typeof next === 'function' ? await next(phase, context) : next;
|
|
61
|
+
if (phase !== DEV) return resolved;
|
|
62
|
+
const root = resolve(options.root ?? process.cwd());
|
|
63
|
+
const base = `${(resolved as { basePath?: string }).basePath ?? ''}/`;
|
|
64
|
+
const client = writeClient(root, plugins, base, shortcut);
|
|
65
|
+
const rules: [string, Rule][] = GLOBS.flatMap((glob) => [
|
|
66
|
+
[glob, { condition: { all: ['browser', { not: 'foreign' }] }, loaders: [{ loader: loaderPath, options: { root, client } }] }],
|
|
67
|
+
[glob, { condition: { all: [{ not: 'browser' }, { not: 'foreign' }] }, loaders: [{ loader: loaderPath, options: { root } }] }],
|
|
68
|
+
]);
|
|
69
|
+
return withRewrite(withWebpack(withRules(resolved, rules), root, client), await serve(root, plugins, base));
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The app's rules and ours: where both have one for a glob, theirs first, then ours, in a list. */
|
|
74
|
+
function withRules<C extends object>(resolved: C, ours: [string, Rule][]): C {
|
|
75
|
+
const turbopack = (resolved as { turbopack?: { rules?: Rules } }).turbopack;
|
|
76
|
+
const rules: Rules = { ...turbopack?.rules };
|
|
77
|
+
for (const [glob, rule] of ours) rules[glob] = glob in rules ? [rules[glob], rule].flat() : rule;
|
|
78
|
+
return { ...resolved, turbopack: { ...turbopack, rules } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** For `next dev --webpack`: the app's own `webpack`, then the loader, `pre`, with the client in the browser's build. */
|
|
82
|
+
function withWebpack<C extends object>(resolved: C, root: string, client: string): C {
|
|
83
|
+
const theirs = (resolved as { webpack?: Webpack | null }).webpack;
|
|
84
|
+
const webpack: Webpack = (config, context) => {
|
|
85
|
+
const made = theirs?.(config, context) ?? config;
|
|
86
|
+
const options: LoaderOptions = context.isServer ? { root } : { root, client };
|
|
87
|
+
made.module.rules.unshift({ test: JSX, exclude: /[\\/]node_modules[\\/]/, enforce: 'pre', use: [{ loader: loaderPath, options }] });
|
|
88
|
+
return made;
|
|
89
|
+
};
|
|
90
|
+
return { ...resolved, webpack };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Sends `/__deictic/*` to the endpoints' server, before any of the app's own rewrites. */
|
|
94
|
+
function withRewrite<C extends object>(resolved: C, port: number): C {
|
|
95
|
+
const theirs = (resolved as { rewrites?: () => Promise<Rewrites> }).rewrites;
|
|
96
|
+
const ours = [{ source: '/__deictic/:path*', destination: `http://127.0.0.1:${port}/__deictic/:path*` }];
|
|
97
|
+
const rewrites = async (): Promise<Rewrites> => {
|
|
98
|
+
const r = (await theirs?.()) ?? [];
|
|
99
|
+
return Array.isArray(r) ? { beforeFiles: ours, afterFiles: r } : { ...r, beforeFiles: [...ours, ...(r.beforeFiles ?? [])] };
|
|
100
|
+
};
|
|
101
|
+
return { ...resolved, rewrites };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Serves the endpoints on a free local port, for as long as the process lives. */
|
|
105
|
+
function serve(root: string, plugins: ServerPlugin[], base: string): Promise<number> {
|
|
106
|
+
const running = serving.get(root);
|
|
107
|
+
if (running !== undefined) return running;
|
|
108
|
+
const handle = endpoints({
|
|
109
|
+
root,
|
|
110
|
+
plugins,
|
|
111
|
+
// Rewritten from under the base, but served at the root of their own server.
|
|
112
|
+
base: '/',
|
|
113
|
+
open: (path, { line, column }) =>
|
|
114
|
+
redirect(`${base}__nextjs_launch-editor?${new URLSearchParams({ file: path, line1: String(line), column1: String(column) })}`),
|
|
115
|
+
});
|
|
116
|
+
const middleware = nodeMiddleware(handle);
|
|
117
|
+
const server = createServer((req, res) =>
|
|
118
|
+
middleware(req, res, () => {
|
|
119
|
+
res.statusCode = 404;
|
|
120
|
+
res.end();
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
const port = new Promise<number>((done, fail) => {
|
|
124
|
+
server.once('error', fail);
|
|
125
|
+
server.listen(0, '127.0.0.1', () => done((server.address() as AddressInfo).port));
|
|
126
|
+
});
|
|
127
|
+
// Not keeping the process alive past Next's own server.
|
|
128
|
+
server.unref();
|
|
129
|
+
serving.set(root, port);
|
|
130
|
+
return port;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export default withDeictic;
|