deictic 0.10.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 +7 -0
- 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 +12 -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/init.js +13 -132
- package/dist/shared/init.js.map +1 -1
- package/dist/shared/integration.js +3 -2
- 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.js +5 -1
- package/dist/shared/vite.js.map +1 -1
- package/dist/webpack.d.ts +5 -0
- package/dist/webpack.d.ts.map +1 -1
- package/dist/webpack.js +7 -2
- package/dist/webpack.js.map +1 -1
- package/package.json +8 -1
- package/src/cli/edit.ts +14 -0
- package/src/cli/init.ts +14 -118
- package/src/index.ts +5 -0
- package/src/setup.ts +132 -0
- package/src/tools/endpoints.ts +23 -4
- package/src/tools/plugins.ts +7 -2
- package/src/vite.ts +7 -2
- package/src/webpack.ts +14 -9
package/src/tools/endpoints.ts
CHANGED
|
@@ -46,14 +46,20 @@ const scopeOf = (plugin: string, { file, line, column }: Loc): Scope => ({ plugi
|
|
|
46
46
|
const scopeIn = ({ plugin, target }: Partial<Body>) =>
|
|
47
47
|
plugin === undefined || target === undefined ? undefined : scopeOf(plugin, target.loc);
|
|
48
48
|
|
|
49
|
-
/**
|
|
50
|
-
export
|
|
49
|
+
/** deictic's requests answered, and nothing for any other path; `close` runs what the plugins' setups left to run on close. */
|
|
50
|
+
export interface Endpoints {
|
|
51
|
+
(req: Request): Promise<Response | undefined>;
|
|
52
|
+
close(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function endpoints({ root, plugins, base = '/', open, history = new History(root, { plugins }) }: EndpointOptions): Endpoints {
|
|
51
56
|
const byName = new Map(plugins.map((p) => [p.name, p]));
|
|
52
57
|
const prefix = `${base}__deictic/`;
|
|
53
58
|
// What goes wrong that nothing in the page caused: said, and handed to the plugins that hear of it once set up.
|
|
54
59
|
const report = reporter(plugins, () => ready);
|
|
55
60
|
// Started now, awaited by every request: a plugin's API is there before anything asks for it.
|
|
56
|
-
const
|
|
61
|
+
const closing: (() => unknown)[] = [];
|
|
62
|
+
const ready = setUp(root, plugins, report, (callback) => void closing.push(callback));
|
|
57
63
|
const gone = missingProblem(root, plugins);
|
|
58
64
|
if (gone !== undefined) report(gone);
|
|
59
65
|
|
|
@@ -104,7 +110,7 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
|
|
|
104
110
|
return next;
|
|
105
111
|
};
|
|
106
112
|
|
|
107
|
-
|
|
113
|
+
const handle = async (req: Request): Promise<Response | undefined> => {
|
|
108
114
|
const url = new URL(req.url);
|
|
109
115
|
if (!url.pathname.startsWith(prefix)) return undefined;
|
|
110
116
|
const route = url.pathname.slice(prefix.length);
|
|
@@ -162,6 +168,19 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
|
|
|
162
168
|
return Response.json({ problem: e instanceof Error ? (e.stack ?? e.message) : String(e) }, { status: 500 });
|
|
163
169
|
}
|
|
164
170
|
};
|
|
171
|
+
// Once, however many times the dev server says it closes (Vite, once per environment); the last set up, first.
|
|
172
|
+
let closed: Promise<void> | undefined;
|
|
173
|
+
const close = () =>
|
|
174
|
+
(closed ??= ready.then(async () => {
|
|
175
|
+
for (const callback of closing.toReversed()) {
|
|
176
|
+
try {
|
|
177
|
+
await callback();
|
|
178
|
+
} catch (e) {
|
|
179
|
+
console.error(`deictic: a plugin failed as the dev server closed: ${messageOf(e)}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}));
|
|
183
|
+
return Object.assign(handle, { close });
|
|
165
184
|
}
|
|
166
185
|
|
|
167
186
|
/** A redirect within the page's origin, to a launcher the dev server answers itself. */
|
package/src/tools/plugins.ts
CHANGED
|
@@ -32,9 +32,14 @@ export function checkPlugins(plugins: readonly ServerPlugin[], own?: Shortcut |
|
|
|
32
32
|
* op, never in `setup` itself. One that fails is reported and has no API: the others go on, so one of them
|
|
33
33
|
* can tell of it (tasks, on the board).
|
|
34
34
|
*/
|
|
35
|
-
export async function setUp(
|
|
35
|
+
export async function setUp(
|
|
36
|
+
root: string,
|
|
37
|
+
plugins: readonly ServerPlugin[],
|
|
38
|
+
report: Report,
|
|
39
|
+
onClose: ServerContext['onClose'],
|
|
40
|
+
): Promise<ServerContext> {
|
|
36
41
|
const apis = new Map<string, unknown>();
|
|
37
|
-
const context: ServerContext = { root, use: (name) => apis.get(name) as never, report };
|
|
42
|
+
const context: ServerContext = { root, use: (name) => apis.get(name) as never, report, onClose };
|
|
38
43
|
await Promise.all(
|
|
39
44
|
plugins.map(async (p) => {
|
|
40
45
|
if (p.setup === undefined) return;
|
package/src/vite.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { resolve } from 'node:path';
|
|
|
15
15
|
|
|
16
16
|
import type { Plugin } from 'vite';
|
|
17
17
|
|
|
18
|
-
import { endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
18
|
+
import { type Endpoints, endpoints, nodeMiddleware, redirect } from './tools/endpoints.ts';
|
|
19
19
|
import { clientModule, type DeicticOptions, mark, markAstro, merge, prepare } from './tools/integration.ts';
|
|
20
20
|
|
|
21
21
|
export { clientModule, type DeicticOptions };
|
|
@@ -46,6 +46,7 @@ export function deictic(given: ViteOptions = {}): Plugin {
|
|
|
46
46
|
let options = given;
|
|
47
47
|
let { plugins, shortcut } = prepare(given);
|
|
48
48
|
let leading = true;
|
|
49
|
+
let handle: Endpoints | undefined;
|
|
49
50
|
let root = '';
|
|
50
51
|
let base = '/';
|
|
51
52
|
let building = false;
|
|
@@ -68,7 +69,7 @@ export function deictic(given: ViteOptions = {}): Plugin {
|
|
|
68
69
|
},
|
|
69
70
|
configureServer(server) {
|
|
70
71
|
if (!leading) return;
|
|
71
|
-
|
|
72
|
+
handle = endpoints({
|
|
72
73
|
root,
|
|
73
74
|
plugins,
|
|
74
75
|
base,
|
|
@@ -76,6 +77,10 @@ export function deictic(given: ViteOptions = {}): Plugin {
|
|
|
76
77
|
});
|
|
77
78
|
server.middlewares.use(nodeMiddleware(handle));
|
|
78
79
|
},
|
|
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
|
+
},
|
|
79
84
|
resolveId: (source) => (leading && source === CLIENT ? CLIENT_ID : undefined),
|
|
80
85
|
load(module) {
|
|
81
86
|
if (!leading || module !== CLIENT_ID) return undefined;
|
package/src/webpack.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
|
18
18
|
|
|
19
19
|
import type { LoaderOptions } from './loader.ts';
|
|
20
20
|
import { openInEditor } from './tools/editor.ts';
|
|
21
|
-
import { endpoints, nodeMiddleware } from './tools/endpoints.ts';
|
|
21
|
+
import { type Endpoints, endpoints, nodeMiddleware } from './tools/endpoints.ts';
|
|
22
22
|
import { beside, type DeicticOptions, merge, prepare, writeClient } from './tools/integration.ts';
|
|
23
23
|
import { JSX } from './tools/locate.ts';
|
|
24
24
|
|
|
@@ -35,6 +35,7 @@ type Middlewares = ({ name: string; middleware: Middleware } | Middleware)[];
|
|
|
35
35
|
/** What the plugin reads and sets of a webpack or Rspack compiler. */
|
|
36
36
|
interface Compiler {
|
|
37
37
|
context: string;
|
|
38
|
+
hooks: { shutdown: { tapPromise: (name: string, run: () => Promise<void>) => void } };
|
|
38
39
|
options: {
|
|
39
40
|
mode?: string;
|
|
40
41
|
target?: string | string[] | false;
|
|
@@ -61,6 +62,7 @@ export class DeicticPlugin {
|
|
|
61
62
|
readonly #options: DeicticWebpackOptions;
|
|
62
63
|
#applied: Applied | undefined;
|
|
63
64
|
#middleware: Middleware | undefined;
|
|
65
|
+
#endpoints: Endpoints | undefined;
|
|
64
66
|
#root = '';
|
|
65
67
|
|
|
66
68
|
constructor(options: DeicticWebpackOptions = {}) {
|
|
@@ -86,14 +88,13 @@ export class DeicticPlugin {
|
|
|
86
88
|
return (this.#middleware = (req, res, next) => {
|
|
87
89
|
if (handle === undefined) {
|
|
88
90
|
const merged = this.#merged();
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
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);
|
|
97
98
|
}
|
|
98
99
|
handle(req, res, next);
|
|
99
100
|
});
|
|
@@ -112,6 +113,10 @@ export class DeicticPlugin {
|
|
|
112
113
|
const browser = forBrowser(compiler.options.target);
|
|
113
114
|
const client = browser ? writeClient(this.#root, plugins, merged.base ?? '/', shortcut) : undefined;
|
|
114
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
|
+
});
|
|
115
120
|
const options: LoaderOptions = { root: this.#root, ...(client === undefined ? {} : { client }) };
|
|
116
121
|
// First, and `pre`, so it sees the source as written whatever the app's own rules do.
|
|
117
122
|
compiler.options.module.rules.unshift({
|