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
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where deictic keeps what it makes for a project: `.deictic/` at its root, beside `.board/` and the like,
|
|
3
|
+
* rather than in `node_modules`, which an install may clear. The history is there, and the shots,
|
|
4
|
+
* recordings and voice clips plugins keep. It ignores itself in git: the history holds whole files, and
|
|
5
|
+
* the rest is the page as it looked, neither of which belongs in a commit.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
const IGNORE = "# deictic's own: the history, and the shots, recordings and voice clips it keeps.\n*\n";
|
|
11
|
+
|
|
12
|
+
/** A path under the project's `.deictic/`, made with its `.gitignore` the first time. */
|
|
13
|
+
export function keptIn(root: string, ...parts: string[]): string {
|
|
14
|
+
const dir = join(root, '.deictic');
|
|
15
|
+
const ignore = join(dir, '.gitignore');
|
|
16
|
+
if (!existsSync(ignore)) {
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
writeFileSync(ignore, IGNORE);
|
|
19
|
+
}
|
|
20
|
+
return join(dir, ...parts);
|
|
21
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks each host element in a JSX module with where its opening tag starts:
|
|
3
|
+
* `<div data-deictic="src/App.tsx:12:5">`. The page reads it back to know what was clicked.
|
|
4
|
+
*
|
|
5
|
+
* At build time and not from React's fibers at runtime: it works for any framework that compiles JSX,
|
|
6
|
+
* for server components (which have no fiber in the browser), and it names the element's own tag
|
|
7
|
+
* rather than the component that rendered it. Only host elements (`div`, `button`) are marked, since a
|
|
8
|
+
* component may not pass the attribute on; a component's call site is reached from its elements.
|
|
9
|
+
*
|
|
10
|
+
* The transform must run on the source as written (`enforce: 'pre'` under Vite), so lines and columns
|
|
11
|
+
* are the file's. It runs alike on the server and in the browser, so a hydrated page matches its HTML.
|
|
12
|
+
*/
|
|
13
|
+
import MagicString from 'magic-string';
|
|
14
|
+
import { type JSXOpeningElement, parseSync, type Program, Visitor } from 'oxc-parser';
|
|
15
|
+
|
|
16
|
+
export const ATTRIBUTE = 'data-deictic';
|
|
17
|
+
/** Modules that may hold JSX: `.jsx` and `.tsx`, and plain `.js`, as many React projects write theirs. */
|
|
18
|
+
export const JSX = /\.[cm]?(?:[jt]sx|js)$/;
|
|
19
|
+
|
|
20
|
+
/** A module parsed, with JSX read in a `.js` file too, which the parser reads there only when told. */
|
|
21
|
+
export const parse = (file: string, code: string) => parseSync(file, code, /\.[cm]?js$/.test(file) ? { lang: 'jsx' } : undefined);
|
|
22
|
+
|
|
23
|
+
export interface Located {
|
|
24
|
+
code: string;
|
|
25
|
+
map: ReturnType<MagicString['generateMap']>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Whether an opening tag is a host element's, `<div>` or `<svg>`, not a component's or a namespaced one. */
|
|
29
|
+
export const isHost = (el: JSXOpeningElement) => el.name.type === 'JSXIdentifier' && /^[a-z]/.test(el.name.name);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `code` with every host element marked, `file` being its path from the project's root, and `append`
|
|
33
|
+
* after it (the client's import, in the browser); nothing when there is nothing to mark, or the module
|
|
34
|
+
* doesn't parse, which the framework's own transform reports better.
|
|
35
|
+
*/
|
|
36
|
+
export function locate(code: string, file: string, append = ''): Located | undefined {
|
|
37
|
+
// No tag, no parse: most `.js` modules have none, and every one of the project's comes through here.
|
|
38
|
+
if (!/<[a-z]/.test(code)) return undefined;
|
|
39
|
+
const { program, errors } = parse(file, code);
|
|
40
|
+
if (errors.length > 0) return undefined;
|
|
41
|
+
return marked(code, file, openingsIn(program), append);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Every opening tag in a module, in its order. */
|
|
45
|
+
export function openingsIn(program: Program): JSXOpeningElement[] {
|
|
46
|
+
const elements: JSXOpeningElement[] = [];
|
|
47
|
+
new Visitor({ JSXOpeningElement: (el) => void elements.push(el) }).visit(program);
|
|
48
|
+
return elements;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `code` with a mark in each of `elements` that is a host element's and has none yet, and `append` after
|
|
53
|
+
* it; nothing with none to mark.
|
|
54
|
+
*/
|
|
55
|
+
export function marked(code: string, file: string, elements: JSXOpeningElement[], append = ''): Located | undefined {
|
|
56
|
+
const lines = lineStarts(code);
|
|
57
|
+
const s = new MagicString(code);
|
|
58
|
+
for (const el of elements) {
|
|
59
|
+
if (!isHost(el) || el.attributes.some((a) => a.type === 'JSXAttribute' && nameOf(a.name) === ATTRIBUTE)) continue;
|
|
60
|
+
const { line, column } = position(lines, el.start);
|
|
61
|
+
s.appendLeft(el.name.end, ` ${ATTRIBUTE}="${file}:${line}:${column}"`);
|
|
62
|
+
}
|
|
63
|
+
if (!s.hasChanged()) return undefined;
|
|
64
|
+
if (append !== '') s.append(append);
|
|
65
|
+
// A map's source is read from the module's own folder, so it's the file's name, not its path from the root.
|
|
66
|
+
return { code: s.toString(), map: s.generateMap({ source: file.slice(file.lastIndexOf('/') + 1), hires: 'boundary' }) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const nameOf = (name: { type: string; name?: unknown }) => (name.type === 'JSXIdentifier' ? name.name : undefined);
|
|
70
|
+
|
|
71
|
+
/** Where each line starts, by offset. */
|
|
72
|
+
export function lineStarts(code: string): number[] {
|
|
73
|
+
const starts = [0];
|
|
74
|
+
for (let i = code.indexOf('\n'); i !== -1; i = code.indexOf('\n', i + 1)) starts.push(i + 1);
|
|
75
|
+
return starts;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** An offset's line and column, from 1. */
|
|
79
|
+
export function position(lines: number[], offset: number): { line: number; column: number } {
|
|
80
|
+
let lo = 0;
|
|
81
|
+
let hi = lines.length - 1;
|
|
82
|
+
while (lo < hi) {
|
|
83
|
+
const mid = (lo + hi + 1) >> 1;
|
|
84
|
+
if (lines[mid]! <= offset) lo = mid;
|
|
85
|
+
else hi = mid - 1;
|
|
86
|
+
}
|
|
87
|
+
return { line: lo + 1, column: offset - lines[lo]! + 1 };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A line and column's offset, or nothing past the file's end. */
|
|
91
|
+
export function offsetOf(lines: number[], line: number, column: number): number | undefined {
|
|
92
|
+
const start = lines[line - 1];
|
|
93
|
+
return start === undefined ? undefined : start + column - 1;
|
|
94
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugins an integration was given: checked as it starts, so a wrong set fails the dev server with
|
|
3
|
+
* what to write instead, and set up once before the endpoints answer anything.
|
|
4
|
+
*/
|
|
5
|
+
import type { ServerContext, ServerPlugin } from '#src/index.ts';
|
|
6
|
+
import { checkShortcuts, OWN, ownShortcut, type Shortcut } from '#src/shortcut.ts';
|
|
7
|
+
|
|
8
|
+
const NAME = /^[a-z][a-z0-9-]*$/;
|
|
9
|
+
|
|
10
|
+
/** Throws what to fix in a set of plugins: names, what they need, and their shortcuts, beside deictic's own. */
|
|
11
|
+
export function checkPlugins(plugins: readonly ServerPlugin[], own?: Shortcut | false): void {
|
|
12
|
+
const names = new Set<string>();
|
|
13
|
+
for (const { name, client, shortcut } of plugins) {
|
|
14
|
+
if (!NAME.test(name)) throw new Error(`deictic: a plugin is named "${name}": name it in lowercase letters, digits and -`);
|
|
15
|
+
if (names.has(name)) throw new Error(`deictic: two plugins are named ${name}: load each once`);
|
|
16
|
+
if (shortcut !== undefined && client === undefined)
|
|
17
|
+
throw new Error(`deictic: ${name} has a shortcut but no \`client\` to draw its panel: give it one, or leave out its shortcut`);
|
|
18
|
+
names.add(name);
|
|
19
|
+
}
|
|
20
|
+
for (const { name, needs = [] } of plugins) {
|
|
21
|
+
const missing = needs.find((other) => !names.has(other));
|
|
22
|
+
if (missing !== undefined) throw new Error(`deictic: ${name} needs the ${missing} plugin: add it to \`plugins\``);
|
|
23
|
+
}
|
|
24
|
+
checkShortcuts([{ name: OWN, shortcut: ownShortcut(own) }, ...plugins]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Runs every plugin's `setup`, all at once, and answers the context they share: `use` reads an API
|
|
29
|
+
* when it's asked for, so setups may `use` each other's only after they've all run, from a route or an
|
|
30
|
+
* op, never in `setup` itself.
|
|
31
|
+
*/
|
|
32
|
+
export async function setUp(root: string, plugins: readonly ServerPlugin[]): Promise<ServerContext> {
|
|
33
|
+
const apis = new Map<string, unknown>();
|
|
34
|
+
const context: ServerContext = { root, use: (name) => apis.get(name) as never };
|
|
35
|
+
await Promise.all(
|
|
36
|
+
plugins.map(async (p) => {
|
|
37
|
+
if (p.setup !== undefined) apis.set(p.name, await p.setup(context));
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
return context;
|
|
41
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The source a server half reads: a target's file, parsed, and the opening tag the page's attribute
|
|
3
|
+
* points at. Files are only ever read and written under the project's root, outside `node_modules`.
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
7
|
+
|
|
8
|
+
import { Refusal, type Source, type Target, type TextEdit } from '#src/index.ts';
|
|
9
|
+
|
|
10
|
+
import { ASTRO, parseAstro, type Parsed } from './astro.ts';
|
|
11
|
+
import { isHost, lineStarts, offsetOf, openingsIn, parse } from './locate.ts';
|
|
12
|
+
|
|
13
|
+
/** A file from the project's root, as an absolute path; refused anywhere the page has no business writing. */
|
|
14
|
+
export function resolveFile(root: string, file: string): string {
|
|
15
|
+
const path = resolve(root, file);
|
|
16
|
+
const from = relative(root, path);
|
|
17
|
+
// `relative` gives an absolute path for another drive.
|
|
18
|
+
if (from === '' || from.startsWith('..') || isAbsolute(from) || from.split(sep).includes('node_modules'))
|
|
19
|
+
throw new Refusal(`${file} is not a file of this project's own`);
|
|
20
|
+
return path;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function readSource(root: string, file: string): string {
|
|
24
|
+
try {
|
|
25
|
+
return readFileSync(resolveFile(root, file), 'utf8');
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (e instanceof Refusal) throw e;
|
|
28
|
+
throw new Refusal(`can't read ${file}: has it moved since the page loaded?`, { cause: e });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The target's file and element, as they are on disk now. */
|
|
33
|
+
export function sourceAt(root: string, target: Target): Source {
|
|
34
|
+
const { file, line, column } = target.loc;
|
|
35
|
+
const code = readSource(root, file);
|
|
36
|
+
const offset = offsetOf(lineStarts(code), line, column);
|
|
37
|
+
const { program, elements } = ASTRO.test(file) ? astro(root, file, code) : jsx(file, code);
|
|
38
|
+
const element = elements.find((el) => el.start === offset && isHost(el));
|
|
39
|
+
if (element === undefined || (element.name.type === 'JSXIdentifier' && element.name.name !== target.tag))
|
|
40
|
+
throw new Refusal(`no <${target.tag}> at ${file}:${line}:${column} any more: the file changed since the page loaded, so reload it`);
|
|
41
|
+
return { file, code, program, element, read: (other) => readSource(root, other) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function jsx(file: string, code: string): Parsed {
|
|
45
|
+
const { program, errors } = parse(file, code);
|
|
46
|
+
if (errors.length > 0) throw new Refusal(`${file} doesn't parse: fix it first (${errors[0]!.message})`);
|
|
47
|
+
return { program, elements: openingsIn(program) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function astro(root: string, file: string, code: string): Parsed {
|
|
51
|
+
const parsed = parseAstro(root, code);
|
|
52
|
+
if ('problem' in parsed) throw new Refusal(`${file} can't be read: ${parsed.problem}`);
|
|
53
|
+
return parsed;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Each file's text after its edits, which must not overlap. */
|
|
57
|
+
export function applyEdits(source: Source, edits: TextEdit[]): Map<string, string> {
|
|
58
|
+
const byFile = Map.groupBy(edits, (e) => e.file ?? source.file);
|
|
59
|
+
const out = new Map<string, string>();
|
|
60
|
+
for (const [file, list] of byFile) {
|
|
61
|
+
const sorted = list.toSorted((a, b) => b.start - a.start);
|
|
62
|
+
let code = file === source.file ? source.code : source.read(file);
|
|
63
|
+
let floor = Infinity;
|
|
64
|
+
for (const edit of sorted) {
|
|
65
|
+
if (edit.end > floor) throw new Error(`deictic: edits to ${file} overlap at ${edit.end}`);
|
|
66
|
+
code = code.slice(0, edit.start) + edit.text + code.slice(edit.end);
|
|
67
|
+
floor = edit.start;
|
|
68
|
+
}
|
|
69
|
+
out.set(file, code);
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
package/src/vite.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic/vite`: deictic in Vite's dev server. Nothing runs in a build.
|
|
3
|
+
*
|
|
4
|
+
* Its transform runs `pre`, so each JSX module's host elements are marked with their place in the
|
|
5
|
+
* source as written (`tools/locate.ts`). In the browser each marked module also imports the client
|
|
6
|
+
* module, which starts the overlay with every plugin's panel: imports run first and only once, so no
|
|
7
|
+
* line moves and frameworks that render their own HTML get it too. The endpoints are middleware on
|
|
8
|
+
* Vite's server, and a place opens through Vite's own `/__open-in-editor`, so the editor is whichever
|
|
9
|
+
* Vite already launches. A write reaches the page through Vite's usual update for the file.
|
|
10
|
+
*/
|
|
11
|
+
import type { Plugin } from 'vite';
|
|
12
|
+
|
|
13
|
+
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
14
|
+
import { clientModule, type DeicticOptions, mark, markAstro, prepare } from './tools/integration.ts';
|
|
15
|
+
|
|
16
|
+
export { clientModule, type DeicticOptions };
|
|
17
|
+
|
|
18
|
+
/** The client module's ID, which a page can import itself where nothing it bundles is JSX (Astro's pages). */
|
|
19
|
+
export const CLIENT = 'virtual:deictic/client';
|
|
20
|
+
const CLIENT_ID = '\0deictic/client';
|
|
21
|
+
|
|
22
|
+
export function deictic(options: DeicticOptions = {}): Plugin {
|
|
23
|
+
const { plugins, shortcut } = prepare(options);
|
|
24
|
+
let root = '';
|
|
25
|
+
let base = '/';
|
|
26
|
+
return {
|
|
27
|
+
name: 'deictic',
|
|
28
|
+
enforce: 'pre',
|
|
29
|
+
apply: 'serve',
|
|
30
|
+
// The client is the app's to bundle as it changes, not a dependency to prebundle once.
|
|
31
|
+
config: () => ({ optimizeDeps: { exclude: ['deictic'] } }),
|
|
32
|
+
configResolved(config) {
|
|
33
|
+
root = config.root;
|
|
34
|
+
base = config.base;
|
|
35
|
+
},
|
|
36
|
+
configureServer(server) {
|
|
37
|
+
const handle = endpoints({
|
|
38
|
+
root,
|
|
39
|
+
plugins,
|
|
40
|
+
base,
|
|
41
|
+
open: (path, { line, column }) => redirect(`${base}__open-in-editor?${new URLSearchParams({ file: `${path}:${line}:${column}` })}`),
|
|
42
|
+
});
|
|
43
|
+
server.middlewares.use(nodeMiddleware(handle));
|
|
44
|
+
},
|
|
45
|
+
resolveId: (id) => (id === CLIENT ? CLIENT_ID : undefined),
|
|
46
|
+
load: (id) => (id === CLIENT_ID ? clientModule(plugins, base, shortcut) : undefined),
|
|
47
|
+
transform: {
|
|
48
|
+
// First of all, not only first of the `pre` plugins: the React Compiler (`react({ compiler: true })`) is
|
|
49
|
+
// `pre` too and compiles the JSX away, so after it there'd be no element to mark, wherever deictic is listed.
|
|
50
|
+
order: 'pre',
|
|
51
|
+
handler(code, id) {
|
|
52
|
+
const [path = ''] = id.split('?');
|
|
53
|
+
// An `.astro` component itself, not the styles and scripts Astro loads from it by a query.
|
|
54
|
+
if (path.endsWith('.astro')) return id === path ? markAstro(code, path, root) : undefined;
|
|
55
|
+
const browser = this.environment.config.consumer === 'client';
|
|
56
|
+
return mark(code, path, root, browser ? `\nimport ${JSON.stringify(CLIENT)};\n` : '');
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default deictic;
|
package/src/webpack.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic/webpack`: deictic in webpack's dev server, or Rspack's (`@rspack/core` takes the same
|
|
3
|
+
* plugin). Nothing runs out of development mode.
|
|
4
|
+
*
|
|
5
|
+
* A loader (`loader.ts`) runs `pre` on the project's JSX modules, marking each host element with its
|
|
6
|
+
* place in the source as written, and in a build for the browser appending the import of the client
|
|
7
|
+
* module, which webpack has no virtual module for, so it's written to `.deictic/client.js`. The
|
|
8
|
+
* endpoints go in front of the dev server's own middleware (`devServer.setupMiddlewares`, which
|
|
9
|
+
* webpack-dev-server and Rspack's both read), and a place opens with `launch-editor`, as webpack has no
|
|
10
|
+
* launcher of its own. Where the dev server is started by hand, or by Rsbuild, `middleware` is the
|
|
11
|
+
* endpoints to mount there.
|
|
12
|
+
*/
|
|
13
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
14
|
+
|
|
15
|
+
import type { ServerPlugin } from './index.ts';
|
|
16
|
+
import type { LoaderOptions } from './loader.ts';
|
|
17
|
+
import type { Shortcut } from './shortcut.ts';
|
|
18
|
+
import { openInEditor } from './tools/editor.ts';
|
|
19
|
+
import { endpoints, nodeMiddleware } from './tools/endpoints.ts';
|
|
20
|
+
import { beside, type DeicticOptions, prepare, writeClient } from './tools/integration.ts';
|
|
21
|
+
import { JSX } from './tools/locate.ts';
|
|
22
|
+
|
|
23
|
+
export interface DeicticWebpackOptions extends DeicticOptions {
|
|
24
|
+
/** The project's root, which marks and edits are from: the compiler's `context` by default. */
|
|
25
|
+
root?: string;
|
|
26
|
+
/** The path the page is served under, ending in `/`: `/` by default. */
|
|
27
|
+
base?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type Middleware = (req: IncomingMessage, res: ServerResponse, next: () => void) => void;
|
|
31
|
+
type Middlewares = ({ name: string; middleware: Middleware } | Middleware)[];
|
|
32
|
+
|
|
33
|
+
/** What the plugin reads and sets of a webpack or Rspack compiler. */
|
|
34
|
+
interface Compiler {
|
|
35
|
+
context: string;
|
|
36
|
+
options: {
|
|
37
|
+
mode?: string;
|
|
38
|
+
target?: string | string[] | false;
|
|
39
|
+
module: { rules: unknown[] };
|
|
40
|
+
devServer?: { setupMiddlewares?: (middlewares: Middlewares, server: unknown) => Middlewares } & Record<string, unknown>;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const loaderPath = beside(import.meta.url, 'loader');
|
|
45
|
+
|
|
46
|
+
/** Whether a target runs in a browser: webpack's default does, as do `web` and `browserslist`; Node's and Electron's main don't. */
|
|
47
|
+
const forBrowser = (target: Compiler['options']['target']) =>
|
|
48
|
+
target === undefined || [target].flat().some((t) => t !== false && !/^(async-)?node|^electron-main|^electron-preload/.test(t));
|
|
49
|
+
|
|
50
|
+
export class DeicticPlugin {
|
|
51
|
+
readonly #options: DeicticWebpackOptions;
|
|
52
|
+
readonly #plugins: ServerPlugin[];
|
|
53
|
+
readonly #shortcut: Shortcut | false | undefined;
|
|
54
|
+
#middleware: Middleware | undefined;
|
|
55
|
+
#root = '';
|
|
56
|
+
|
|
57
|
+
constructor(options: DeicticWebpackOptions = {}) {
|
|
58
|
+
this.#options = options;
|
|
59
|
+
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
60
|
+
({ plugins: this.#plugins, shortcut: this.#shortcut } = prepare(options));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The endpoints as Node middleware, once the plugin is applied: for a dev server started by hand. */
|
|
64
|
+
get middleware(): Middleware {
|
|
65
|
+
if (this.#middleware !== undefined) return this.#middleware;
|
|
66
|
+
if (this.#root === '') throw new Error('deictic: give the plugin to the compiler before mounting its middleware');
|
|
67
|
+
const root = this.#root;
|
|
68
|
+
const handle = endpoints({
|
|
69
|
+
root,
|
|
70
|
+
plugins: this.#plugins,
|
|
71
|
+
base: this.#options.base ?? '/',
|
|
72
|
+
open: (path, loc) => openInEditor(path, loc),
|
|
73
|
+
});
|
|
74
|
+
return (this.#middleware = nodeMiddleware(handle));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
apply(compiler: Compiler): void {
|
|
78
|
+
// Development only: a build without a mode is webpack's production one.
|
|
79
|
+
if (compiler.options.mode !== 'development') return;
|
|
80
|
+
this.#root = this.#options.root ?? compiler.context;
|
|
81
|
+
const browser = forBrowser(compiler.options.target);
|
|
82
|
+
const client = browser ? writeClient(this.#root, this.#plugins, this.#options.base ?? '/', this.#shortcut) : undefined;
|
|
83
|
+
const options: LoaderOptions = { root: this.#root, ...(client === undefined ? {} : { client }) };
|
|
84
|
+
// First, and `pre`, so it sees the source as written whatever the app's own rules do.
|
|
85
|
+
compiler.options.module.rules.unshift({
|
|
86
|
+
test: JSX,
|
|
87
|
+
exclude: /[\\/]node_modules[\\/]/,
|
|
88
|
+
enforce: 'pre',
|
|
89
|
+
use: [{ loader: loaderPath, options }],
|
|
90
|
+
});
|
|
91
|
+
if (!browser) return;
|
|
92
|
+
|
|
93
|
+
const devServer = (compiler.options.devServer ??= {});
|
|
94
|
+
const theirs = devServer.setupMiddlewares;
|
|
95
|
+
devServer.setupMiddlewares = (middlewares, server) => {
|
|
96
|
+
const all = theirs === undefined ? middlewares : theirs(middlewares, server);
|
|
97
|
+
// In front of the dev server's own, which would answer `/__deictic/` with the page or a 404.
|
|
98
|
+
all.unshift({ name: 'deictic', middleware: this.middleware });
|
|
99
|
+
return all;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The plugin, as a function: `plugins: [deictic({ plugins: [tailwind()] })]`. */
|
|
105
|
+
export const deictic = (options: DeicticWebpackOptions = {}) => new DeicticPlugin(options);
|
|
106
|
+
|
|
107
|
+
export default deictic;
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.error("Package isn't ready yet. Please wait for the next release.");
|