clojurific 0.12.5 → 0.12.7
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/README.md +2 -2
- package/cljf/repl.js +24 -6
- package/package.json +1 -1
- package/vite-plugin-cljs/index.js +79 -24
package/README.md
CHANGED
|
@@ -16,8 +16,8 @@ counts Clojurific's releases, starting from 1.
|
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
|
-
`npm create clojurific@latest` scaffolds a project (Vanilla, Reagent or
|
|
20
|
-
like the one below, installs it and starts the dev server.
|
|
19
|
+
`npm create clojurific@latest` scaffolds a project (Vanilla, Reagent, UIx, or
|
|
20
|
+
a Hono backend) like the one below, installs it and starts the dev server.
|
|
21
21
|
|
|
22
22
|
Dependencies and source paths come from the project's `deps.edn`:
|
|
23
23
|
|
package/cljf/repl.js
CHANGED
|
@@ -99,14 +99,27 @@ export function complete(code) {
|
|
|
99
99
|
return depth <= 0;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* An nREPL connection: requests whose responses end with status done. Once the
|
|
104
|
+
* socket closes, pending and later requests reject.
|
|
105
|
+
*/
|
|
103
106
|
export class Connection {
|
|
104
107
|
#socket;
|
|
105
108
|
#ids = 0;
|
|
106
109
|
#pending = new Map();
|
|
110
|
+
#closed = null;
|
|
107
111
|
|
|
108
112
|
constructor(socket) {
|
|
109
113
|
this.#socket = socket;
|
|
114
|
+
const fail = e => {
|
|
115
|
+
if (this.#closed) return;
|
|
116
|
+
this.#closed = new LauncherError(`cljf repl: the nREPL server closed the connection${e ? `: ${e.message}` : ''}`);
|
|
117
|
+
for (const { reject } of this.#pending.values()) reject(this.#closed);
|
|
118
|
+
this.#pending.clear();
|
|
119
|
+
};
|
|
120
|
+
// an error is followed by close
|
|
121
|
+
socket.on('error', fail);
|
|
122
|
+
socket.on('close', () => fail());
|
|
110
123
|
const decoder = new Decoder();
|
|
111
124
|
socket.on('data', chunk => {
|
|
112
125
|
for (const msg of decoder.push(chunk)) {
|
|
@@ -132,8 +145,12 @@ export class Connection {
|
|
|
132
145
|
*/
|
|
133
146
|
request(msg, onResponse = () => {}) {
|
|
134
147
|
const id = msg.id ?? this.newId();
|
|
135
|
-
return new Promise(resolve => {
|
|
136
|
-
this.#
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
if (this.#closed) {
|
|
150
|
+
reject(this.#closed);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
this.#pending.set(id, { onResponse, resolve, reject });
|
|
137
154
|
this.#socket.write(bencode({ ...msg, id }));
|
|
138
155
|
});
|
|
139
156
|
}
|
|
@@ -247,7 +264,7 @@ export async function repl({
|
|
|
247
264
|
rl.close();
|
|
248
265
|
// the server forgets the session, unless it doesn't answer
|
|
249
266
|
const timeout = new Promise(r => setTimeout(r, 1000).unref());
|
|
250
|
-
Promise.race([conn.request({ op: 'close', session }), timeout]).finally(() => {
|
|
267
|
+
Promise.race([conn.request({ op: 'close', session }), timeout]).catch(() => {}).finally(() => {
|
|
251
268
|
conn.close();
|
|
252
269
|
resolve(code);
|
|
253
270
|
});
|
|
@@ -262,7 +279,7 @@ export async function repl({
|
|
|
262
279
|
|
|
263
280
|
rl.on('SIGINT', () => {
|
|
264
281
|
if (evaluating) {
|
|
265
|
-
conn.request({ op: 'interrupt', session, 'interrupt-id': evaluating });
|
|
282
|
+
conn.request({ op: 'interrupt', session, 'interrupt-id': evaluating }).catch(() => {});
|
|
266
283
|
} else if (pending || rl.line) {
|
|
267
284
|
pending = '';
|
|
268
285
|
interrupted = false;
|
|
@@ -307,12 +324,13 @@ export async function repl({
|
|
|
307
324
|
return;
|
|
308
325
|
}
|
|
309
326
|
evaluating = conn.newId();
|
|
327
|
+
// a lost connection rejects, closed (above) reports it
|
|
310
328
|
await conn.request({ op: 'eval', session, code, ns, id: evaluating }, msg => {
|
|
311
329
|
write(output, msg.out);
|
|
312
330
|
write(errorOutput, msg.err);
|
|
313
331
|
if (msg.value !== undefined) output.write(`${msg.value}\n`);
|
|
314
332
|
if (msg.ns) ns = msg.ns;
|
|
315
|
-
});
|
|
333
|
+
}).catch(() => {});
|
|
316
334
|
evaluating = null;
|
|
317
335
|
if (closing) return;
|
|
318
336
|
prompt();
|
package/package.json
CHANGED
|
@@ -247,6 +247,10 @@ export default module.exports;
|
|
|
247
247
|
* true
|
|
248
248
|
* @param {string[]} [options.entries] the :js-entries this build bundles,
|
|
249
249
|
* defaults to all; each is self-contained when it's the only one
|
|
250
|
+
* @param {string[]} [options.ssrEntries] :js-entries the dev server runs in
|
|
251
|
+
* its ssr environment once they're compiled, i.e. a server's (which a
|
|
252
|
+
* plugin like @hono/vite-dev-server imports from the same runner): the REPL
|
|
253
|
+
* evaluates in them before their first request
|
|
250
254
|
* @param {boolean} [options.replRemoteConsole] accept cljs_eval (forms the
|
|
251
255
|
* compiler compiles, running its macros) from pages on other hosts too,
|
|
252
256
|
* defaults to false: only from this machine. A proxy in front of the dev
|
|
@@ -686,36 +690,52 @@ export default function cljs(options) {
|
|
|
686
690
|
server.ws.send(problem);
|
|
687
691
|
};
|
|
688
692
|
server.ws.on('connection', socket => problem && socket.send(JSON.stringify(problem)));
|
|
693
|
+
// options.ssrEntries run in the ssr environment's runner once compiled,
|
|
694
|
+
// until they ran
|
|
695
|
+
let ssrEntries = options.ssrEntries ?? [];
|
|
696
|
+
const runSsrEntries = async () => {
|
|
697
|
+
if (!ssrEntries.length) return;
|
|
698
|
+
// created when it's first read
|
|
699
|
+
const runner = server.environments.ssr?.runner;
|
|
700
|
+
if (!runner) return;
|
|
701
|
+
const info = await buildInfo();
|
|
702
|
+
for (const name of ssrEntries) {
|
|
703
|
+
const file = info?.entries?.[name];
|
|
704
|
+
if (!file) {
|
|
705
|
+
config.logger.error(`[cljs] ssrEntries: no :js-entries ${name}`, { timestamp: true });
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
await runner.import(path.join(outputDir, file));
|
|
710
|
+
ssrEntries = ssrEntries.filter(n => n !== name);
|
|
711
|
+
} catch (e) {
|
|
712
|
+
config.logger.error(`[cljs] running ${name} (${file}): ${e.stack ?? e}`, { timestamp: true });
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
};
|
|
689
716
|
// The REPL (cljf.esm.repl): pages running the build (cljf.esm.repl-runtime)
|
|
690
|
-
// say hello over Vite's websocket,
|
|
691
|
-
//
|
|
692
|
-
//
|
|
717
|
+
// say hello over Vite's websocket, and modules the ssr environment's
|
|
718
|
+
// runner runs (a server's :js-entries) over its hot channel; each
|
|
719
|
+
// connection gets a runtime id. Their cljs:repl messages go to the
|
|
720
|
+
// compiler as `repl <json>` lines, the compiler's repl-send events to the
|
|
721
|
+
// runtime of their runtime id, runtimes[id](data).
|
|
693
722
|
const runtimes = new Map();
|
|
694
723
|
let lastRuntimeId = 0;
|
|
695
724
|
const toRepl = msg => proc.stdin.writable && proc.stdin.write(`repl ${JSON.stringify(msg)}\n`);
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
const consoleAllowed = options.replRemoteConsole || isLocalConnection(req);
|
|
725
|
+
// A runtime's connection: receive takes the data of its cljs:repl events
|
|
726
|
+
// for the REPL, close says bye. send replies to it, consoleAllowed whether
|
|
727
|
+
// it may cljs_eval.
|
|
728
|
+
const connectRuntime = (send, consoleAllowed) => {
|
|
701
729
|
let id;
|
|
702
|
-
|
|
703
|
-
let msg;
|
|
704
|
-
try {
|
|
705
|
-
msg = JSON.parse(String(raw));
|
|
706
|
-
} catch {
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
if (msg?.type !== 'custom' || msg.event !== 'cljs:repl' || !msg.data) return;
|
|
710
|
-
const data = msg.data;
|
|
730
|
+
const receive = data => {
|
|
711
731
|
if (data.op === 'hello' && id === undefined) {
|
|
712
732
|
id = ++lastRuntimeId;
|
|
713
|
-
runtimes.set(id,
|
|
733
|
+
runtimes.set(id, send);
|
|
714
734
|
}
|
|
715
735
|
if (id === undefined) return;
|
|
716
|
-
if (data.op === 'hello') send(
|
|
736
|
+
if (data.op === 'hello') send({ op: 'welcome', runtime: id });
|
|
717
737
|
if (data.op === 'console-eval' && !consoleAllowed) {
|
|
718
|
-
send(
|
|
738
|
+
send({
|
|
719
739
|
op: 'console-result',
|
|
720
740
|
rid: data.rid,
|
|
721
741
|
result: { error: 'cljs_eval only runs in pages on the dev server\'s machine, see the cljs plugin\'s replRemoteConsole' },
|
|
@@ -723,15 +743,49 @@ export default function cljs(options) {
|
|
|
723
743
|
return;
|
|
724
744
|
}
|
|
725
745
|
toRepl({ ...data, runtime: id });
|
|
726
|
-
}
|
|
727
|
-
|
|
746
|
+
};
|
|
747
|
+
const close = () => {
|
|
728
748
|
if (id !== undefined && runtimes.delete(id)) toRepl({ op: 'bye', runtime: id });
|
|
749
|
+
};
|
|
750
|
+
return { receive, close };
|
|
751
|
+
};
|
|
752
|
+
server.ws.on('connection', (socket, req) => {
|
|
753
|
+
// cljs_eval compiles forms in the compiler's JVM, which runs their macros:
|
|
754
|
+
// only for pages on this machine, unless replRemoteConsole
|
|
755
|
+
const runtime = connectRuntime(
|
|
756
|
+
data => socket.send(JSON.stringify({ type: 'custom', event: 'cljs:repl', data })),
|
|
757
|
+
options.replRemoteConsole || isLocalConnection(req));
|
|
758
|
+
socket.on('message', raw => {
|
|
759
|
+
let msg;
|
|
760
|
+
try {
|
|
761
|
+
msg = JSON.parse(String(raw));
|
|
762
|
+
} catch {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
if (msg?.type !== 'custom' || msg.event !== 'cljs:repl' || !msg.data) return;
|
|
766
|
+
runtime.receive(msg.data);
|
|
729
767
|
});
|
|
768
|
+
socket.on('close', runtime.close);
|
|
730
769
|
});
|
|
770
|
+
// the ssr runner's modules run in this process, each runner is a client
|
|
771
|
+
// of the environment's hot channel
|
|
772
|
+
const ssrHot = server.environments.ssr?.hot;
|
|
773
|
+
if (ssrHot) {
|
|
774
|
+
const ssrRuntimes = new Map();
|
|
775
|
+
const ssrRuntime = client => {
|
|
776
|
+
if (!ssrRuntimes.has(client)) ssrRuntimes.set(client, connectRuntime(data => client.send('cljs:repl', data), true));
|
|
777
|
+
return ssrRuntimes.get(client);
|
|
778
|
+
};
|
|
779
|
+
ssrHot.on('cljs:repl', (data, client) => data && ssrRuntime(client).receive(data));
|
|
780
|
+
ssrHot.on('vite:client:disconnect', (data, client) => {
|
|
781
|
+
ssrRuntimes.get(client)?.close();
|
|
782
|
+
ssrRuntimes.delete(client);
|
|
783
|
+
});
|
|
784
|
+
}
|
|
731
785
|
proc = run(compilerArgs('watch', { ':esm-hmr': true, ':exit-with-parent': true, ':watch-events': ':stdin' }), async event => {
|
|
732
786
|
if (event.type === 'repl-send') {
|
|
733
|
-
const
|
|
734
|
-
if (
|
|
787
|
+
const send = runtimes.get(event.runtime);
|
|
788
|
+
if (send) send(event.msg);
|
|
735
789
|
else toRepl({ op: 'bye', runtime: event.runtime });
|
|
736
790
|
return;
|
|
737
791
|
}
|
|
@@ -793,6 +847,7 @@ export default function cljs(options) {
|
|
|
793
847
|
for (const file of released) {
|
|
794
848
|
for (const mod of client.moduleGraph.getModulesByFile(file) ?? []) client.reloadModule(mod);
|
|
795
849
|
}
|
|
850
|
+
runSsrEntries();
|
|
796
851
|
ready();
|
|
797
852
|
} else if (event.type === 'error') {
|
|
798
853
|
config.logger.error(`[cljs] ${event.message}`, { timestamp: true });
|