deictic 0.9.0 → 0.10.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 +10 -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/shared/endpoints.js +14 -3
- package/dist/shared/endpoints.js.map +1 -1
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.d.ts.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 +28 -1
- package/dist/shared/integration.js.map +1 -1
- package/dist/shared/package.js +1 -1
- 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 +28 -10
- package/dist/shared/vite.js.map +1 -1
- package/dist/webpack.d.ts +4 -0
- package/dist/webpack.d.ts.map +1 -1
- package/dist/webpack.js +37 -15
- package/dist/webpack.js.map +1 -1
- package/package.json +1 -1
- package/src/bun.ts +32 -8
- package/src/client/index.ts +4 -0
- package/src/index.ts +5 -0
- package/src/next.ts +34 -15
- package/src/tools/integration.ts +33 -0
- package/src/tools/source.ts +14 -2
- package/src/vite.ts +31 -10
- package/src/webpack.ts +48 -16
package/src/bun.ts
CHANGED
|
@@ -15,13 +15,18 @@
|
|
|
15
15
|
* // server.ts
|
|
16
16
|
* import tools from './deictic.ts';
|
|
17
17
|
* Bun.serve({ routes: { ...tools.routes, '/*': index }, development: true });
|
|
18
|
+
*
|
|
19
|
+
* bunfig may name deictic more than once: the app's, and a package's that adds its own plugin (`defaults`), as
|
|
20
|
+
* prosebook's does. Each made in the process for a root is kept (`made`), the first set up marks the modules, and
|
|
21
|
+
* the client and the endpoints, made when first needed, load all of their options merged (`merge`); any of them
|
|
22
|
+
* has the routes.
|
|
18
23
|
*/
|
|
19
24
|
import { resolve } from 'node:path';
|
|
20
25
|
|
|
21
26
|
import type { BunPlugin } from 'bun';
|
|
22
27
|
|
|
23
28
|
import { endpoints } from './tools/endpoints.ts';
|
|
24
|
-
import { type DeicticOptions, importOf, mark, prepare, writeClient } from './tools/integration.ts';
|
|
29
|
+
import { type DeicticOptions, importOf, mark, merge, prepare, writeClient } from './tools/integration.ts';
|
|
25
30
|
import { JSX } from './tools/locate.ts';
|
|
26
31
|
|
|
27
32
|
export interface DeicticBunOptions extends DeicticOptions {
|
|
@@ -34,15 +39,28 @@ export type DeicticBunPlugin = BunPlugin & {
|
|
|
34
39
|
routes: Record<string, (req: Request) => Promise<Response>>;
|
|
35
40
|
};
|
|
36
41
|
|
|
42
|
+
/** Every deictic made in the process, by its root: what it was given, whether one set up its marking, and its endpoints. */
|
|
43
|
+
interface Made {
|
|
44
|
+
given: DeicticBunOptions[];
|
|
45
|
+
marking: boolean;
|
|
46
|
+
handle?: (req: Request) => Promise<Response | undefined>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const made = new Map<string, Made>();
|
|
50
|
+
|
|
37
51
|
export function deictic(options: DeicticBunOptions = {}): DeicticBunPlugin {
|
|
38
52
|
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
39
|
-
|
|
53
|
+
prepare(options);
|
|
40
54
|
const root = resolve(options.root ?? process.cwd());
|
|
41
|
-
let
|
|
55
|
+
let all = made.get(root);
|
|
56
|
+
if (all === undefined) made.set(root, (all = { given: [], marking: false }));
|
|
57
|
+
all.given.push(options);
|
|
58
|
+
const shared = all;
|
|
59
|
+
const merged = () => prepare(shared.given.length === 1 ? shared.given[0]! : merge(shared.given));
|
|
42
60
|
const route = async (req: Request) => {
|
|
43
|
-
handle ??= endpoints({
|
|
61
|
+
shared.handle ??= endpoints({
|
|
44
62
|
root,
|
|
45
|
-
plugins,
|
|
63
|
+
plugins: merged().plugins,
|
|
46
64
|
open: (path, { line, column }) => {
|
|
47
65
|
try {
|
|
48
66
|
Bun.openInEditor(path, { line, column });
|
|
@@ -54,7 +72,7 @@ export function deictic(options: DeicticBunOptions = {}): DeicticBunPlugin {
|
|
|
54
72
|
}
|
|
55
73
|
},
|
|
56
74
|
});
|
|
57
|
-
return (await handle(req)) ?? new Response(null, { status: 404 });
|
|
75
|
+
return (await shared.handle(req)) ?? new Response(null, { status: 404 });
|
|
58
76
|
};
|
|
59
77
|
|
|
60
78
|
return {
|
|
@@ -62,14 +80,20 @@ export function deictic(options: DeicticBunOptions = {}): DeicticBunPlugin {
|
|
|
62
80
|
routes: { '/__deictic/*': route },
|
|
63
81
|
setup(build) {
|
|
64
82
|
// A build names its entry points; the dev server's bundler doesn't.
|
|
65
|
-
if ('entrypoints' in build.config) return;
|
|
66
|
-
|
|
83
|
+
if ('entrypoints' in build.config || shared.marking) return;
|
|
84
|
+
shared.marking = true;
|
|
85
|
+
let client: string | undefined;
|
|
67
86
|
// Bun names the side it loads a module for (undocumented, and not in Bun's types); the dev server's is the browser's.
|
|
68
87
|
const forBrowser = (args: object) => !('side' in args) || args.side === 'client';
|
|
69
88
|
build.onLoad({ filter: JSX }, async (args) => {
|
|
70
89
|
// A dependency's is left to Bun, unread.
|
|
71
90
|
if (/[\\/]node_modules[\\/]/.test(args.path)) return undefined;
|
|
72
91
|
const code = await Bun.file(args.path).text();
|
|
92
|
+
// Written as the first module loads, once bunfig's every deictic has been made.
|
|
93
|
+
if (client === undefined) {
|
|
94
|
+
const { plugins, shortcut } = merged();
|
|
95
|
+
client = writeClient(root, plugins, '/', shortcut);
|
|
96
|
+
}
|
|
73
97
|
const marked = mark(code, args.path, root, forBrowser(args) ? importOf(client, args.path) : '');
|
|
74
98
|
if (marked === undefined) return undefined;
|
|
75
99
|
return { contents: marked.code, loader: args.path.endsWith('tsx') ? 'tsx' : 'jsx' };
|
package/src/client/index.ts
CHANGED
|
@@ -231,6 +231,10 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
|
|
|
231
231
|
use: (other) => apis.get(other) as never,
|
|
232
232
|
request: (route, init) => send(`${base}__deictic/plugins/${name}/${route}`, init),
|
|
233
233
|
report: (problem) => report({ ...problem, plugin: name }),
|
|
234
|
+
edit: async ({ file, line, column }) => {
|
|
235
|
+
const res = await send(`${base}__deictic/open?${new URLSearchParams({ file, line: String(line), column: String(column) })}`);
|
|
236
|
+
if (!res.ok) throw new Error(await res.text());
|
|
237
|
+
},
|
|
234
238
|
// Not from under something unsaved, which a press on the page wouldn't close either: its button shakes.
|
|
235
239
|
open: (plugin, element, at) => panel?.guard() !== true && openAt(plugin, element, at),
|
|
236
240
|
lightbox: (items, options) => openLightbox(root, items, options),
|
package/src/index.ts
CHANGED
|
@@ -282,6 +282,11 @@ export interface ClientContext {
|
|
|
282
282
|
request: (route: string, init?: RequestInit) => Promise<Response>;
|
|
283
283
|
/** Says a problem of the plugin's own to the dev server, as the server half's `report` does: its `plugin` is this one. */
|
|
284
284
|
report: (problem: Problem) => void;
|
|
285
|
+
/**
|
|
286
|
+
* Opens a place in a file under the root in the editor, as the popup's own link opens the element's: through the
|
|
287
|
+
* dev server, or what answers in its place (`Standalone`). Rejects with why it couldn't.
|
|
288
|
+
*/
|
|
289
|
+
edit: (place: Loc) => Promise<void>;
|
|
285
290
|
/**
|
|
286
291
|
* Opens the popup on an element at a plugin's tab, as its shortcut would, or with `at` on a spot in it: the
|
|
287
292
|
* popup is then beside the spot, which is what's lit, and the tab's context has it. An element rendered again
|
package/src/next.ts
CHANGED
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Both bundlers' rules are always set, as the config can't tell which will run: Next refuses a Turbopack
|
|
16
16
|
* build whose config has a `webpack` key only where it has no `turbopack` one, and ours always does.
|
|
17
|
+
*
|
|
18
|
+
* A config may be wrapped more than once: by the app, and by a package that adds its own plugin (`defaults`), as
|
|
19
|
+
* `withProsebook` does. The config each returns carries the options it was given (`GIVEN`), so the next one out adds
|
|
20
|
+
* no rules of its own: it writes the client again with all of them merged (`merge`), and the endpoints, made at the
|
|
21
|
+
* first request, load them all.
|
|
17
22
|
*/
|
|
18
23
|
import { createServer } from 'node:http';
|
|
19
24
|
import type { AddressInfo } from 'node:net';
|
|
@@ -22,7 +27,7 @@ import { resolve } from 'node:path';
|
|
|
22
27
|
import type { ServerPlugin } from './index.ts';
|
|
23
28
|
import type { LoaderOptions } from './loader.ts';
|
|
24
29
|
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
25
|
-
import { beside, type DeicticOptions, prepare, writeClient } from './tools/integration.ts';
|
|
30
|
+
import { beside, type DeicticOptions, merge, prepare, writeClient } from './tools/integration.ts';
|
|
26
31
|
import { JSX } from './tools/locate.ts';
|
|
27
32
|
|
|
28
33
|
export interface DeicticNextOptions extends DeicticOptions {
|
|
@@ -45,6 +50,10 @@ const GLOBS = ['*.tsx', '*.jsx', '*.js'];
|
|
|
45
50
|
|
|
46
51
|
// Next loads its config more than once in a process: one endpoints server per project is enough.
|
|
47
52
|
const serving = new Map<string, Promise<number>>();
|
|
53
|
+
// Each project's plugins as its config last merged them, for its endpoints to load at their first request.
|
|
54
|
+
const loaded = new Map<string, ServerPlugin[]>();
|
|
55
|
+
/** On a config a `withDeictic` returned: the options of every one it went through. */
|
|
56
|
+
const GIVEN = Symbol.for('deictic.given');
|
|
48
57
|
|
|
49
58
|
/**
|
|
50
59
|
* Wraps the Next config: `export default withDeictic({ ... }, { plugins: [tailwind()] })`. Takes the
|
|
@@ -55,18 +64,25 @@ export function withDeictic<C extends object>(
|
|
|
55
64
|
options: DeicticNextOptions = {},
|
|
56
65
|
): (phase: string, context: { defaultConfig: C }) => Promise<C> {
|
|
57
66
|
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
58
|
-
|
|
67
|
+
prepare(options);
|
|
59
68
|
return async (phase, context) => {
|
|
60
69
|
const resolved = typeof next === 'function' ? await next(phase, context) : next;
|
|
61
70
|
if (phase !== DEV) return resolved;
|
|
62
|
-
const
|
|
71
|
+
const before = (resolved as { [GIVEN]?: DeicticNextOptions[] })[GIVEN];
|
|
72
|
+
const given = [...(before ?? []), options];
|
|
73
|
+
const merged = given.length === 1 ? options : merge(given);
|
|
74
|
+
const { plugins, shortcut } = prepare(merged);
|
|
75
|
+
const root = resolve(merged.root ?? process.cwd());
|
|
63
76
|
const base = `${(resolved as { basePath?: string }).basePath ?? ''}/`;
|
|
64
77
|
const client = writeClient(root, plugins, base, shortcut);
|
|
78
|
+
loaded.set(root, plugins);
|
|
79
|
+
// Wrapped already: the rules and the rewrite are there, and only the client and the plugins change.
|
|
80
|
+
if (before !== undefined) return { ...resolved, [GIVEN]: given };
|
|
65
81
|
const rules: [string, Rule][] = GLOBS.flatMap((glob) => [
|
|
66
82
|
[glob, { condition: { all: ['browser', { not: 'foreign' }] }, loaders: [{ loader: loaderPath, options: { root, client } }] }],
|
|
67
83
|
[glob, { condition: { all: [{ not: 'browser' }, { not: 'foreign' }] }, loaders: [{ loader: loaderPath, options: { root } }] }],
|
|
68
84
|
]);
|
|
69
|
-
return withRewrite(withWebpack(withRules(resolved, rules), root, client), await serve(root,
|
|
85
|
+
return { ...withRewrite(withWebpack(withRules(resolved, rules), root, client), await serve(root, base)), [GIVEN]: given };
|
|
70
86
|
};
|
|
71
87
|
}
|
|
72
88
|
|
|
@@ -102,20 +118,23 @@ function withRewrite<C extends object>(resolved: C, port: number): C {
|
|
|
102
118
|
}
|
|
103
119
|
|
|
104
120
|
/** Serves the endpoints on a free local port, for as long as the process lives. */
|
|
105
|
-
function serve(root: string,
|
|
121
|
+
function serve(root: string, base: string): Promise<number> {
|
|
106
122
|
const running = serving.get(root);
|
|
107
123
|
if (running !== undefined) return running;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
let middleware: ReturnType<typeof nodeMiddleware> | undefined;
|
|
125
|
+
const made = () =>
|
|
126
|
+
(middleware ??= nodeMiddleware(
|
|
127
|
+
endpoints({
|
|
128
|
+
root,
|
|
129
|
+
plugins: loaded.get(root) ?? [],
|
|
130
|
+
// Rewritten from under the base, but served at the root of their own server.
|
|
131
|
+
base: '/',
|
|
132
|
+
open: (path, { line, column }) =>
|
|
133
|
+
redirect(`${base}__nextjs_launch-editor?${new URLSearchParams({ file: path, line1: String(line), column1: String(column) })}`),
|
|
134
|
+
}),
|
|
135
|
+
));
|
|
117
136
|
const server = createServer((req, res) =>
|
|
118
|
-
|
|
137
|
+
made()(req, res, () => {
|
|
119
138
|
res.statusCode = 404;
|
|
120
139
|
res.end();
|
|
121
140
|
}),
|
package/src/tools/integration.ts
CHANGED
|
@@ -25,6 +25,39 @@ export interface DeicticOptions {
|
|
|
25
25
|
* `false` for none, leaving only plugins' own shortcuts.
|
|
26
26
|
*/
|
|
27
27
|
shortcut?: Shortcut | false;
|
|
28
|
+
/**
|
|
29
|
+
* For a package that adds its own plugin to the bundler with deictic (prosebook does): these options give way to
|
|
30
|
+
* those of any other deictic in the same build, which merges them in, so the app's own `deictic()` decides the
|
|
31
|
+
* shortcut and its plugin of the same name is the one loaded.
|
|
32
|
+
*/
|
|
33
|
+
defaults?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Every deictic one build was given, as one: the app's and those packages added (`defaults`). The plugins of all of
|
|
38
|
+
* them, each name once, the first given winning, those of an instance with `defaults` after the rest's; any other
|
|
39
|
+
* option from the first that sets it, the same way. Two that aren't `defaults` setting one differently is an error,
|
|
40
|
+
* as neither can be the one meant.
|
|
41
|
+
*/
|
|
42
|
+
export function merge<O extends DeicticOptions>(instances: readonly O[]): O {
|
|
43
|
+
const ordered = [...instances.filter((o) => o.defaults !== true), ...instances.filter((o) => o.defaults === true)];
|
|
44
|
+
const plugins = new Map<string, ServerPlugin>();
|
|
45
|
+
const merged: Record<string, unknown> = {};
|
|
46
|
+
const setBy: Record<string, O> = {};
|
|
47
|
+
for (const options of ordered) {
|
|
48
|
+
for (const plugin of options.plugins ?? []) if (!plugins.has(plugin.name)) plugins.set(plugin.name, plugin);
|
|
49
|
+
for (const [key, value] of Object.entries(options)) {
|
|
50
|
+
if (key === 'plugins' || key === 'defaults' || value === undefined) continue;
|
|
51
|
+
const first = setBy[key];
|
|
52
|
+
if (first === undefined) {
|
|
53
|
+
merged[key] = value;
|
|
54
|
+
setBy[key] = options;
|
|
55
|
+
} else if (first.defaults !== true && options.defaults !== true && JSON.stringify(merged[key]) !== JSON.stringify(value)) {
|
|
56
|
+
throw new Error(`deictic: the build has deictic twice, with ${key} set two ways: set it in one of them only`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { ...merged, plugins: [...plugins.values()] } as O;
|
|
28
61
|
}
|
|
29
62
|
|
|
30
63
|
/** The options with their defaults, checked now, so a wrong set stops the dev server as it starts, not the page. */
|
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
18
|
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
16
|
-
import { clientModule, type DeicticOptions, mark, markAstro, prepare } from './tools/integration.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,23 +34,40 @@ 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;
|
|
36
49
|
let root = '';
|
|
37
50
|
let base = '/';
|
|
38
51
|
let building = false;
|
|
39
52
|
return {
|
|
40
53
|
name: 'deictic',
|
|
41
54
|
enforce: 'pre',
|
|
42
|
-
apply: (_, { command }) => command === 'serve' ||
|
|
55
|
+
apply: (_, { command }) => command === 'serve' || given.standalone !== undefined,
|
|
56
|
+
api: { deictic: { options: given, id } } satisfies Carried,
|
|
43
57
|
// The client is the app's to bundle as it changes, not a dependency to prebundle once.
|
|
44
58
|
config: () => ({ optimizeDeps: { exclude: ['deictic'] } }),
|
|
45
59
|
configResolved(config) {
|
|
46
60
|
root = config.root;
|
|
47
61
|
base = config.base;
|
|
48
62
|
building = config.command === 'build';
|
|
63
|
+
const all = config.plugins.flatMap((p) => carriedBy(p) ?? []);
|
|
64
|
+
leading = all[0]?.id === id;
|
|
65
|
+
if (!leading || all.length === 1) return;
|
|
66
|
+
options = merge(all.map((c) => c.options));
|
|
67
|
+
({ plugins, shortcut } = prepare(options));
|
|
49
68
|
},
|
|
50
69
|
configureServer(server) {
|
|
70
|
+
if (!leading) return;
|
|
51
71
|
const handle = endpoints({
|
|
52
72
|
root,
|
|
53
73
|
plugins,
|
|
@@ -56,9 +76,9 @@ export function deictic(options: ViteOptions = {}): Plugin {
|
|
|
56
76
|
});
|
|
57
77
|
server.middlewares.use(nodeMiddleware(handle));
|
|
58
78
|
},
|
|
59
|
-
resolveId: (
|
|
60
|
-
load(
|
|
61
|
-
if (
|
|
79
|
+
resolveId: (source) => (leading && source === CLIENT ? CLIENT_ID : undefined),
|
|
80
|
+
load(module) {
|
|
81
|
+
if (!leading || module !== CLIENT_ID) return undefined;
|
|
62
82
|
// Only a build's page asks the module: the dev server is there to answer.
|
|
63
83
|
const server = building && options.standalone !== undefined ? resolve(root, options.standalone).replaceAll('\\', '/') : undefined;
|
|
64
84
|
return clientModule(plugins, base, shortcut, server);
|
|
@@ -67,10 +87,11 @@ export function deictic(options: ViteOptions = {}): Plugin {
|
|
|
67
87
|
// First of all, not only first of the `pre` plugins: the React Compiler (`react({ compiler: true })`) is
|
|
68
88
|
// `pre` too and compiles the JSX away, so after it there'd be no element to mark, wherever deictic is listed.
|
|
69
89
|
order: 'pre',
|
|
70
|
-
handler(code,
|
|
71
|
-
|
|
90
|
+
handler(code, module) {
|
|
91
|
+
if (!leading) return undefined;
|
|
92
|
+
const [path = ''] = module.split('?');
|
|
72
93
|
// An `.astro` component itself, not the styles and scripts Astro loads from it by a query.
|
|
73
|
-
if (path.endsWith('.astro')) return
|
|
94
|
+
if (path.endsWith('.astro')) return module === path ? markAstro(code, path, root) : undefined;
|
|
74
95
|
const browser = this.environment.config.consumer === 'client';
|
|
75
96
|
return mark(code, path, root, browser ? `\nimport ${JSON.stringify(CLIENT)};\n` : '');
|
|
76
97
|
},
|
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
21
|
import { endpoints, nodeMiddleware } from './tools/endpoints.ts';
|
|
20
|
-
import { beside, type DeicticOptions, prepare, writeClient } from './tools/integration.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 {
|
|
@@ -47,39 +49,69 @@ const loaderPath = beside(import.meta.url, 'loader');
|
|
|
47
49
|
const forBrowser = (target: Compiler['options']['target']) =>
|
|
48
50
|
target === undefined || [target].flat().some((t) => t !== false && !/^(async-)?node|^electron-main|^electron-preload/.test(t));
|
|
49
51
|
|
|
52
|
+
/** Every deictic one compiler was given, and the first of them, which serves the endpoints. */
|
|
53
|
+
interface Applied {
|
|
54
|
+
given: DeicticWebpackOptions[];
|
|
55
|
+
first: DeicticPlugin;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const appliedTo = new WeakMap<Compiler, Applied>();
|
|
59
|
+
|
|
50
60
|
export class DeicticPlugin {
|
|
51
61
|
readonly #options: DeicticWebpackOptions;
|
|
52
|
-
|
|
53
|
-
readonly #shortcut: Shortcut | false | undefined;
|
|
62
|
+
#applied: Applied | undefined;
|
|
54
63
|
#middleware: Middleware | undefined;
|
|
55
64
|
#root = '';
|
|
56
65
|
|
|
57
66
|
constructor(options: DeicticWebpackOptions = {}) {
|
|
58
67
|
this.#options = options;
|
|
59
68
|
// Now, so a wrong set of plugins stops the dev server as it starts, rather than the page.
|
|
60
|
-
|
|
69
|
+
prepare(options);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The options of every deictic the compiler was given, merged. */
|
|
73
|
+
#merged(): DeicticWebpackOptions {
|
|
74
|
+
const given = this.#applied?.given ?? [this.#options];
|
|
75
|
+
return given.length === 1 ? given[0]! : merge(given);
|
|
61
76
|
}
|
|
62
77
|
|
|
63
78
|
/** The endpoints as Node middleware, once the plugin is applied: for a dev server started by hand. */
|
|
64
79
|
get middleware(): Middleware {
|
|
80
|
+
const applied = this.#applied;
|
|
81
|
+
if (applied === undefined) throw new Error('deictic: give the plugin to the compiler before mounting its middleware');
|
|
82
|
+
if (applied.first !== this) return applied.first.middleware;
|
|
65
83
|
if (this.#middleware !== undefined) return this.#middleware;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
84
|
+
let handle: Middleware | undefined;
|
|
85
|
+
// Made at the first request, once every deictic of the compiler has been applied.
|
|
86
|
+
return (this.#middleware = (req, res, next) => {
|
|
87
|
+
if (handle === undefined) {
|
|
88
|
+
const merged = this.#merged();
|
|
89
|
+
handle = nodeMiddleware(
|
|
90
|
+
endpoints({
|
|
91
|
+
root: this.#root,
|
|
92
|
+
plugins: prepare(merged).plugins,
|
|
93
|
+
base: merged.base ?? '/',
|
|
94
|
+
open: (path, loc) => openInEditor(path, loc),
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
handle(req, res, next);
|
|
73
99
|
});
|
|
74
|
-
return (this.#middleware = nodeMiddleware(handle));
|
|
75
100
|
}
|
|
76
101
|
|
|
77
102
|
apply(compiler: Compiler): void {
|
|
78
103
|
// Development only: a build without a mode is webpack's production one.
|
|
79
104
|
if (compiler.options.mode !== 'development') return;
|
|
80
|
-
|
|
105
|
+
let applied = appliedTo.get(compiler);
|
|
106
|
+
if (applied === undefined) appliedTo.set(compiler, (applied = { given: [], first: this }));
|
|
107
|
+
applied.given.push(this.#options);
|
|
108
|
+
this.#applied = applied;
|
|
109
|
+
const merged = this.#merged();
|
|
110
|
+
const { plugins, shortcut } = prepare(merged);
|
|
111
|
+
this.#root = merged.root ?? compiler.context;
|
|
81
112
|
const browser = forBrowser(compiler.options.target);
|
|
82
|
-
const client = browser ? writeClient(this.#root,
|
|
113
|
+
const client = browser ? writeClient(this.#root, plugins, merged.base ?? '/', shortcut) : undefined;
|
|
114
|
+
if (applied.first !== this) return;
|
|
83
115
|
const options: LoaderOptions = { root: this.#root, ...(client === undefined ? {} : { client }) };
|
|
84
116
|
// First, and `pre`, so it sees the source as written whatever the app's own rules do.
|
|
85
117
|
compiler.options.module.rules.unshift({
|