deictic 0.10.0 → 0.12.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/client/index.d.ts +1 -1
- package/dist/client/index.js +25 -4
- package/dist/client/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- 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 +17 -1
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index10.d.ts +1 -1
- package/dist/shared/index8.d.ts +1 -1
- package/dist/shared/init.js +13 -132
- package/dist/shared/init.js.map +1 -1
- package/dist/shared/integration.d.ts +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/client/index.ts +15 -1
- package/src/client/lighting.ts +11 -3
- package/src/index.ts +17 -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/setup.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic/setup`: a bundler's config edited as `deictic init` edits it, for a tool that sets a project up beside
|
|
3
|
+
* deictic. Build-time only, like the CLI.
|
|
4
|
+
*/
|
|
5
|
+
import MagicString from 'magic-string';
|
|
6
|
+
import { type Expression, type ObjectExpression, parseSync, type Program } from 'oxc-parser';
|
|
7
|
+
|
|
8
|
+
import { addFirst, addImports, addLast } from './cli/edit.ts';
|
|
9
|
+
|
|
10
|
+
type Node = Expression | ObjectExpression;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The object an expression comes to: through `defineConfig(…)`, `(…)`, `satisfies`, a function's return and a top-level
|
|
14
|
+
* `const`. Not through another call: in `mergeConfig(base, {…})` or webpack-merge's `merge(common, {…})` the first
|
|
15
|
+
* argument is a base the project may build with too, and the plugin isn't the base's.
|
|
16
|
+
*/
|
|
17
|
+
function objectOf(node: Node | null | undefined, program: Program, depth = 0): ObjectExpression | undefined {
|
|
18
|
+
if (node == null || depth > 8) return undefined;
|
|
19
|
+
const next = (n: Node | null | undefined) => objectOf(n, program, depth + 1);
|
|
20
|
+
if (node.type === 'ObjectExpression') return node;
|
|
21
|
+
if (node.type === 'ParenthesizedExpression' || node.type === 'TSAsExpression' || node.type === 'TSSatisfiesExpression')
|
|
22
|
+
return next(node.expression);
|
|
23
|
+
if (node.type === 'CallExpression') {
|
|
24
|
+
const { callee } = node;
|
|
25
|
+
const named = (callee.type === 'Identifier' ? callee : callee.type === 'MemberExpression' ? callee.property : undefined) as
|
|
26
|
+
{ type: string; name?: string } | undefined;
|
|
27
|
+
const [first] = node.arguments;
|
|
28
|
+
if (named?.name !== 'defineConfig' || first === undefined || first.type === 'SpreadElement') return undefined;
|
|
29
|
+
return next(first);
|
|
30
|
+
}
|
|
31
|
+
if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
|
|
32
|
+
const body = node.body;
|
|
33
|
+
if (body === null) return undefined;
|
|
34
|
+
if (body.type !== 'BlockStatement') return next(body);
|
|
35
|
+
const returned = body.body.findLast((s) => s.type === 'ReturnStatement');
|
|
36
|
+
return returned?.type === 'ReturnStatement' ? next(returned.argument) : undefined;
|
|
37
|
+
}
|
|
38
|
+
if (node.type !== 'Identifier') return undefined;
|
|
39
|
+
for (const statement of program.body) {
|
|
40
|
+
const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
|
|
41
|
+
if (declaration?.type !== 'VariableDeclaration') continue;
|
|
42
|
+
const found = declaration.declarations.find((d) => d.id.type === 'Identifier' && d.id.name === node.name);
|
|
43
|
+
if (found !== undefined) return next(found.init);
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The config's exported value: `export default …` or `module.exports = …`, and whether it's CommonJS. */
|
|
49
|
+
function exportedOf(program: Program): { value: Expression; commonjs: boolean } | undefined {
|
|
50
|
+
for (const statement of program.body) {
|
|
51
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
52
|
+
const value = statement.declaration;
|
|
53
|
+
return value.type.endsWith('Declaration') && value.type !== 'FunctionDeclaration'
|
|
54
|
+
? undefined
|
|
55
|
+
: { value: value as Expression, commonjs: false };
|
|
56
|
+
}
|
|
57
|
+
if (statement.type !== 'ExpressionStatement' || statement.expression.type !== 'AssignmentExpression') continue;
|
|
58
|
+
const { left, right } = statement.expression;
|
|
59
|
+
const exports =
|
|
60
|
+
left.type === 'MemberExpression' &&
|
|
61
|
+
left.object.type === 'Identifier' &&
|
|
62
|
+
left.object.name === 'module' &&
|
|
63
|
+
left.property.type === 'Identifier' &&
|
|
64
|
+
left.property.name === 'exports';
|
|
65
|
+
if (exports) return { value: right, commonjs: true };
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What a tool adds to a bundler's config: its imports, and a value in one of the config's lists or around it whole. */
|
|
71
|
+
export interface ConfigEntry {
|
|
72
|
+
/** Each import, as `[what, from]`: `['{ deictic }', 'deictic/vite']`. */
|
|
73
|
+
imports: [string, string][];
|
|
74
|
+
/** The value added to `list`, or, with `wrap`, what follows the config in the call: `{ plugins: [] }`, or nothing. */
|
|
75
|
+
value: string;
|
|
76
|
+
/** The exported object's list it goes in: `plugins`, `integrations`. */
|
|
77
|
+
list?: string;
|
|
78
|
+
/** The function the whole of the exported config is passed to instead (Next's `withDeictic`). */
|
|
79
|
+
wrap?: string;
|
|
80
|
+
/** After the list's others, where it goes first by default. */
|
|
81
|
+
last?: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The config with the entry added, or nothing where it can't tell where: its imports after the config's, and its
|
|
86
|
+
* value in the list or around the whole config, written in the config's quotes, semicolons and line endings. What
|
|
87
|
+
* `deictic init` adds deictic with, for a tool setting a project up beside it (prosebook's `init`).
|
|
88
|
+
*/
|
|
89
|
+
export function addToConfig(file: string, code: string, entry: ConfigEntry): string | undefined {
|
|
90
|
+
const { program, errors } = parseSync(file, code, { preserveParens: false });
|
|
91
|
+
if (errors.length > 0) return undefined;
|
|
92
|
+
const exported = exportedOf(program);
|
|
93
|
+
if (exported === undefined) return undefined;
|
|
94
|
+
const out = new MagicString(code);
|
|
95
|
+
|
|
96
|
+
if (entry.wrap !== undefined) {
|
|
97
|
+
out.appendLeft(exported.value.start, `${entry.wrap}(`);
|
|
98
|
+
out.appendRight(exported.value.end, entry.value === '' ? ')' : `, ${entry.value})`);
|
|
99
|
+
} else {
|
|
100
|
+
const object = objectOf(exported.value, program);
|
|
101
|
+
if (object === undefined || entry.list === undefined || !addTo(out, code, object, entry)) return undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
addImports(out, program, code, entry.imports, exported.commonjs);
|
|
105
|
+
return out.toString();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Adds the entry's value to the object's array `list`, or the list with it, as the object is laid out. False where `list` isn't an array. */
|
|
109
|
+
function addTo(out: MagicString, code: string, object: ObjectExpression, { list, value, last }: ConfigEntry): boolean {
|
|
110
|
+
const property = object.properties.find(
|
|
111
|
+
(p) =>
|
|
112
|
+
p.type === 'Property' &&
|
|
113
|
+
!p.computed &&
|
|
114
|
+
((p.key.type === 'Identifier' && p.key.name === list) || (p.key.type === 'Literal' && p.key.value === list)),
|
|
115
|
+
);
|
|
116
|
+
if (property !== undefined) {
|
|
117
|
+
if (property.type !== 'Property') return false;
|
|
118
|
+
let array = property.value;
|
|
119
|
+
while (array.type === 'TSAsExpression' || array.type === 'TSSatisfiesExpression') array = array.expression;
|
|
120
|
+
if (array.type !== 'ArrayExpression') return false;
|
|
121
|
+
const elements = array.elements.filter((e) => e !== null);
|
|
122
|
+
if (elements.length === 0) {
|
|
123
|
+
out.appendLeft(array.start + 1, value);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (last) addLast(out, code, elements.at(-1)!, array.end - 1, value);
|
|
127
|
+
else addFirst(out, code, array.start, elements[0], value);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
addFirst(out, code, object.start, object.properties[0], `${list}: [${value}]`);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
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({
|