deictic 0.9.0 → 0.11.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/API.md +17 -0
- package/dist/bun.d.ts.map +1 -1
- package/dist/bun.js +26 -8
- package/dist/bun.js.map +1 -1
- package/dist/client/index.js +8 -0
- package/dist/client/index.js.map +1 -1
- package/dist/next.d.ts.map +1 -1
- package/dist/next.js +45 -25
- package/dist/next.js.map +1 -1
- package/dist/setup.d.ts +20 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +1 -0
- package/dist/shared/endpoints.js +26 -6
- package/dist/shared/endpoints.js.map +1 -1
- package/dist/shared/index.d.ts +10 -0
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/init.js +13 -132
- package/dist/shared/init.js.map +1 -1
- package/dist/shared/integration.d.ts +6 -0
- package/dist/shared/integration.d.ts.map +1 -1
- package/dist/shared/integration.js +31 -3
- package/dist/shared/integration.js.map +1 -1
- package/dist/shared/package.js +1 -1
- package/dist/shared/setup.js +152 -0
- package/dist/shared/setup.js.map +1 -0
- package/dist/shared/src.js.map +1 -1
- package/dist/shared/vite.d.ts +1 -1
- package/dist/shared/vite.d.ts.map +1 -1
- package/dist/shared/vite.js +33 -11
- package/dist/shared/vite.js.map +1 -1
- package/dist/webpack.d.ts +9 -0
- package/dist/webpack.d.ts.map +1 -1
- package/dist/webpack.js +42 -15
- package/dist/webpack.js.map +1 -1
- package/package.json +8 -1
- package/src/bun.ts +32 -8
- package/src/cli/edit.ts +14 -0
- package/src/cli/init.ts +14 -118
- package/src/client/index.ts +4 -0
- package/src/index.ts +10 -0
- package/src/next.ts +34 -15
- package/src/setup.ts +132 -0
- package/src/tools/endpoints.ts +23 -4
- package/src/tools/integration.ts +33 -0
- package/src/tools/plugins.ts +7 -2
- package/src/tools/source.ts +14 -2
- package/src/vite.ts +38 -12
- package/src/webpack.ts +54 -17
package/src/tools/source.ts
CHANGED
|
@@ -73,13 +73,16 @@ function astro(root: string, file: string, code: string): Parsed {
|
|
|
73
73
|
return parsed;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
* Each file's text after its edits, which must not overlap. A file that isn't there is made where every edit to it is
|
|
78
|
+
* at its start (a locale's first translation, say); any other edit to one is refused.
|
|
79
|
+
*/
|
|
77
80
|
export function applyEdits(source: Source, edits: TextEdit[]): Map<string, string> {
|
|
78
81
|
const byFile = Map.groupBy(edits, (e) => e.file ?? source.file);
|
|
79
82
|
const out = new Map<string, string>();
|
|
80
83
|
for (const [file, list] of byFile) {
|
|
81
84
|
const sorted = list.toSorted((a, b) => b.start - a.start);
|
|
82
|
-
let code = file === source.file ? source.code : source
|
|
85
|
+
let code = file === source.file ? source.code : textOf(source, file, list);
|
|
83
86
|
let floor = Infinity;
|
|
84
87
|
for (const edit of sorted) {
|
|
85
88
|
if (edit.end > floor) throw new Error(`deictic: edits to ${file} overlap at ${edit.end}`);
|
|
@@ -90,3 +93,12 @@ export function applyEdits(source: Source, edits: TextEdit[]): Map<string, strin
|
|
|
90
93
|
}
|
|
91
94
|
return out;
|
|
92
95
|
}
|
|
96
|
+
|
|
97
|
+
function textOf(source: Source, file: string, edits: TextEdit[]): string {
|
|
98
|
+
try {
|
|
99
|
+
return source.read(file);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
if (e instanceof Refusal && e.cause !== undefined && edits.every((edit) => edit.start === 0 && edit.end === 0)) return '';
|
|
102
|
+
throw e;
|
|
103
|
+
}
|
|
104
|
+
}
|
package/src/vite.ts
CHANGED
|
@@ -7,13 +7,16 @@
|
|
|
7
7
|
* line moves and frameworks that render their own HTML get it too. The endpoints are middleware on
|
|
8
8
|
* Vite's server, and a place opens through Vite's own `/__open-in-editor`, so the editor is whichever
|
|
9
9
|
* Vite already launches. A write reaches the page through Vite's usual update for the file.
|
|
10
|
+
*
|
|
11
|
+
* A build may have deictic more than once: the app's, and one a package added with its own plugin (`defaults`).
|
|
12
|
+
* The first in Vite's list does the work with all of their options merged (`merge`); the others do nothing.
|
|
10
13
|
*/
|
|
11
14
|
import { resolve } from 'node:path';
|
|
12
15
|
|
|
13
16
|
import type { Plugin } from 'vite';
|
|
14
17
|
|
|
15
|
-
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
16
|
-
import { clientModule, type DeicticOptions, mark, markAstro, prepare } from './tools/integration.ts';
|
|
18
|
+
import { type Endpoints, endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
19
|
+
import { clientModule, type DeicticOptions, mark, markAstro, merge, prepare } from './tools/integration.ts';
|
|
17
20
|
|
|
18
21
|
export { clientModule, type DeicticOptions };
|
|
19
22
|
|
|
@@ -31,24 +34,42 @@ export interface ViteOptions extends DeicticOptions {
|
|
|
31
34
|
export const CLIENT = 'virtual:deictic/client';
|
|
32
35
|
const CLIENT_ID = '\0deictic/client';
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
/** What each deictic in Vite's list carries, for the first of them to find the others by. */
|
|
38
|
+
interface Carried {
|
|
39
|
+
deictic: { options: ViteOptions; id: symbol };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const carriedBy = (plugin: { api?: unknown }) => (plugin.api as Partial<Carried> | undefined)?.deictic;
|
|
43
|
+
|
|
44
|
+
export function deictic(given: ViteOptions = {}): Plugin {
|
|
45
|
+
const id = Symbol('deictic');
|
|
46
|
+
let options = given;
|
|
47
|
+
let { plugins, shortcut } = prepare(given);
|
|
48
|
+
let leading = true;
|
|
49
|
+
let handle: Endpoints | undefined;
|
|
36
50
|
let root = '';
|
|
37
51
|
let base = '/';
|
|
38
52
|
let building = false;
|
|
39
53
|
return {
|
|
40
54
|
name: 'deictic',
|
|
41
55
|
enforce: 'pre',
|
|
42
|
-
apply: (_, { command }) => command === 'serve' ||
|
|
56
|
+
apply: (_, { command }) => command === 'serve' || given.standalone !== undefined,
|
|
57
|
+
api: { deictic: { options: given, id } } satisfies Carried,
|
|
43
58
|
// The client is the app's to bundle as it changes, not a dependency to prebundle once.
|
|
44
59
|
config: () => ({ optimizeDeps: { exclude: ['deictic'] } }),
|
|
45
60
|
configResolved(config) {
|
|
46
61
|
root = config.root;
|
|
47
62
|
base = config.base;
|
|
48
63
|
building = config.command === 'build';
|
|
64
|
+
const all = config.plugins.flatMap((p) => carriedBy(p) ?? []);
|
|
65
|
+
leading = all[0]?.id === id;
|
|
66
|
+
if (!leading || all.length === 1) return;
|
|
67
|
+
options = merge(all.map((c) => c.options));
|
|
68
|
+
({ plugins, shortcut } = prepare(options));
|
|
49
69
|
},
|
|
50
70
|
configureServer(server) {
|
|
51
|
-
|
|
71
|
+
if (!leading) return;
|
|
72
|
+
handle = endpoints({
|
|
52
73
|
root,
|
|
53
74
|
plugins,
|
|
54
75
|
base,
|
|
@@ -56,9 +77,13 @@ export function deictic(options: ViteOptions = {}): Plugin {
|
|
|
56
77
|
});
|
|
57
78
|
server.middlewares.use(nodeMiddleware(handle));
|
|
58
79
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
80
|
+
// Vite's dev server runs it as it closes, once per environment, and a build at its end, with no endpoints.
|
|
81
|
+
async closeBundle() {
|
|
82
|
+
await handle?.close();
|
|
83
|
+
},
|
|
84
|
+
resolveId: (source) => (leading && source === CLIENT ? CLIENT_ID : undefined),
|
|
85
|
+
load(module) {
|
|
86
|
+
if (!leading || module !== CLIENT_ID) return undefined;
|
|
62
87
|
// Only a build's page asks the module: the dev server is there to answer.
|
|
63
88
|
const server = building && options.standalone !== undefined ? resolve(root, options.standalone).replaceAll('\\', '/') : undefined;
|
|
64
89
|
return clientModule(plugins, base, shortcut, server);
|
|
@@ -67,10 +92,11 @@ export function deictic(options: ViteOptions = {}): Plugin {
|
|
|
67
92
|
// First of all, not only first of the `pre` plugins: the React Compiler (`react({ compiler: true })`) is
|
|
68
93
|
// `pre` too and compiles the JSX away, so after it there'd be no element to mark, wherever deictic is listed.
|
|
69
94
|
order: 'pre',
|
|
70
|
-
handler(code,
|
|
71
|
-
|
|
95
|
+
handler(code, module) {
|
|
96
|
+
if (!leading) return undefined;
|
|
97
|
+
const [path = ''] = module.split('?');
|
|
72
98
|
// An `.astro` component itself, not the styles and scripts Astro loads from it by a query.
|
|
73
|
-
if (path.endsWith('.astro')) return
|
|
99
|
+
if (path.endsWith('.astro')) return module === path ? markAstro(code, path, root) : undefined;
|
|
74
100
|
const browser = this.environment.config.consumer === 'client';
|
|
75
101
|
return mark(code, path, root, browser ? `\nimport ${JSON.stringify(CLIENT)};\n` : '');
|
|
76
102
|
},
|
package/src/webpack.ts
CHANGED
|
@@ -9,15 +9,17 @@
|
|
|
9
9
|
* webpack-dev-server and Rspack's both read), and a place opens with `launch-editor`, as webpack has no
|
|
10
10
|
* launcher of its own. Where the dev server is started by hand, or by Rsbuild, `middleware` is the
|
|
11
11
|
* endpoints to mount there.
|
|
12
|
+
*
|
|
13
|
+
* A compiler may be given deictic more than once: the app's, and one a package applied with its own plugin
|
|
14
|
+
* (`defaults`). The first applied adds the rule and the middleware, and every one writes the client again with all
|
|
15
|
+
* of their options merged (`merge`), so the last applied writes it whole before the compiler runs.
|
|
12
16
|
*/
|
|
13
17
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
14
18
|
|
|
15
|
-
import type { ServerPlugin } from './index.ts';
|
|
16
19
|
import type { LoaderOptions } from './loader.ts';
|
|
17
|
-
import type { Shortcut } from './shortcut.ts';
|
|
18
20
|
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 { type Endpoints, endpoints, nodeMiddleware } from './tools/endpoints.ts';
|
|
22
|
+
import { beside, type DeicticOptions, merge, prepare, writeClient } from './tools/integration.ts';
|
|
21
23
|
import { JSX } from './tools/locate.ts';
|
|
22
24
|
|
|
23
25
|
export interface DeicticWebpackOptions extends DeicticOptions {
|
|
@@ -33,6 +35,7 @@ type Middlewares = ({ name: string; middleware: Middleware } | Middleware)[];
|
|
|
33
35
|
/** What the plugin reads and sets of a webpack or Rspack compiler. */
|
|
34
36
|
interface Compiler {
|
|
35
37
|
context: string;
|
|
38
|
+
hooks: { shutdown: { tapPromise: (name: string, run: () => Promise<void>) => void } };
|
|
36
39
|
options: {
|
|
37
40
|
mode?: string;
|
|
38
41
|
target?: string | string[] | false;
|
|
@@ -47,39 +50,73 @@ const loaderPath = beside(import.meta.url, 'loader');
|
|
|
47
50
|
const forBrowser = (target: Compiler['options']['target']) =>
|
|
48
51
|
target === undefined || [target].flat().some((t) => t !== false && !/^(async-)?node|^electron-main|^electron-preload/.test(t));
|
|
49
52
|
|
|
53
|
+
/** Every deictic one compiler was given, and the first of them, which serves the endpoints. */
|
|
54
|
+
interface Applied {
|
|
55
|
+
given: DeicticWebpackOptions[];
|
|
56
|
+
first: DeicticPlugin;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const appliedTo = new WeakMap<Compiler, Applied>();
|
|
60
|
+
|
|
50
61
|
export class DeicticPlugin {
|
|
51
62
|
readonly #options: DeicticWebpackOptions;
|
|
52
|
-
|
|
53
|
-
readonly #shortcut: Shortcut | false | undefined;
|
|
63
|
+
#applied: Applied | undefined;
|
|
54
64
|
#middleware: Middleware | undefined;
|
|
65
|
+
#endpoints: Endpoints | undefined;
|
|
55
66
|
#root = '';
|
|
56
67
|
|
|
57
68
|
constructor(options: DeicticWebpackOptions = {}) {
|
|
58
69
|
this.#options = options;
|
|
59
70
|
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
60
|
-
|
|
71
|
+
prepare(options);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The options of every deictic the compiler was given, merged. */
|
|
75
|
+
#merged(): DeicticWebpackOptions {
|
|
76
|
+
const given = this.#applied?.given ?? [this.#options];
|
|
77
|
+
return given.length === 1 ? given[0]! : merge(given);
|
|
61
78
|
}
|
|
62
79
|
|
|
63
80
|
/** The endpoints as Node middleware, once the plugin is applied: for a dev server started by hand. */
|
|
64
81
|
get middleware(): Middleware {
|
|
82
|
+
const applied = this.#applied;
|
|
83
|
+
if (applied === undefined) throw new Error('deictic: give the plugin to the compiler before mounting its middleware');
|
|
84
|
+
if (applied.first !== this) return applied.first.middleware;
|
|
65
85
|
if (this.#middleware !== undefined) return this.#middleware;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
86
|
+
let handle: Middleware | undefined;
|
|
87
|
+
// Made at the first request, once every deictic of the compiler has been applied.
|
|
88
|
+
return (this.#middleware = (req, res, next) => {
|
|
89
|
+
if (handle === undefined) {
|
|
90
|
+
const merged = this.#merged();
|
|
91
|
+
this.#endpoints = endpoints({
|
|
92
|
+
root: this.#root,
|
|
93
|
+
plugins: prepare(merged).plugins,
|
|
94
|
+
base: merged.base ?? '/',
|
|
95
|
+
open: (path, loc) => openInEditor(path, loc),
|
|
96
|
+
});
|
|
97
|
+
handle = nodeMiddleware(this.#endpoints);
|
|
98
|
+
}
|
|
99
|
+
handle(req, res, next);
|
|
73
100
|
});
|
|
74
|
-
return (this.#middleware = nodeMiddleware(handle));
|
|
75
101
|
}
|
|
76
102
|
|
|
77
103
|
apply(compiler: Compiler): void {
|
|
78
104
|
// Development only: a build without a mode is webpack's production one.
|
|
79
105
|
if (compiler.options.mode !== 'development') return;
|
|
80
|
-
|
|
106
|
+
let applied = appliedTo.get(compiler);
|
|
107
|
+
if (applied === undefined) appliedTo.set(compiler, (applied = { given: [], first: this }));
|
|
108
|
+
applied.given.push(this.#options);
|
|
109
|
+
this.#applied = applied;
|
|
110
|
+
const merged = this.#merged();
|
|
111
|
+
const { plugins, shortcut } = prepare(merged);
|
|
112
|
+
this.#root = merged.root ?? compiler.context;
|
|
81
113
|
const browser = forBrowser(compiler.options.target);
|
|
82
|
-
const client = browser ? writeClient(this.#root,
|
|
114
|
+
const client = browser ? writeClient(this.#root, plugins, merged.base ?? '/', shortcut) : undefined;
|
|
115
|
+
if (applied.first !== this) return;
|
|
116
|
+
// webpack's and Rspack's `close()`, which their dev servers call as they stop.
|
|
117
|
+
compiler.hooks.shutdown.tapPromise('deictic', async () => {
|
|
118
|
+
await this.#endpoints?.close();
|
|
119
|
+
});
|
|
83
120
|
const options: LoaderOptions = { root: this.#root, ...(client === undefined ? {} : { client }) };
|
|
84
121
|
// First, and `pre`, so it sees the source as written whatever the app's own rules do.
|
|
85
122
|
compiler.options.module.rules.unshift({
|