write-language 0.1.209 → 0.1.211

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "write-language",
3
- "version": "0.1.209",
3
+ "version": "0.1.211",
4
4
  "description": "Multi-provider language generation toolkit using Vercel AI SDK - generate responses with 10+ LLM providers including OpenAI, Anthropic, Google, and more.",
5
5
  "author": "vtempest <grokthiscontact@gmail.com>",
6
6
  "license": "rights.institute/PROSPER",
@@ -1,21 +1,81 @@
1
1
  /**
2
- * @fileoverview Publishes Prism on `globalThis` so the `prismjs/components/*`
3
- * grammar scripts can find it.
2
+ * @fileoverview Owns the Prism instance: publishes it on `globalThis`, then
3
+ * registers the language grammars that prismjs' own entry point leaves out —
4
+ * without ever depending on module evaluation order.
4
5
  *
5
- * Those grammar files are plain browser scripts, not modules — `prism-markup.js`
6
- * literally opens with `Prism.languages.markup = {...}`, resolving `Prism` as a
7
- * free variable off the global object. Prism's core only publishes that global
8
- * when it can see `window` (browser), a `WorkerGlobalScope` `self`, or Node's
9
- * `global`. On Cloudflare Workers / edge runtimes none of the three exist, so
10
- * the first grammar file throws `ReferenceError: Prism is not defined` while the
11
- * module graph is still evaluating, taking the whole SSR render down with it.
6
+ * The `prismjs/components/*` files are plain browser scripts, not modules:
7
+ * `prism-markup.js` literally opens with `Prism.languages.markup = {...}`,
8
+ * resolving `Prism` as a free variable off the global object. Someone has to
9
+ * put it there first. prismjs' own entry point does that for `window`
10
+ * (browser) and `global` (Node); on Cloudflare Workers / edge runtimes it sees
11
+ * neither, which is what the assignment below is for.
12
12
  *
13
- * Import this module *before* any `prismjs/components/*` import. ES modules are
14
- * evaluated in import order, so this file's body runs — and the global lands —
15
- * before the grammars reference it.
13
+ * Publishing it is only half the job, though: it has to happen *before* the
14
+ * grammar scripts run, and a static `import "prismjs/components/..."` cannot
15
+ * promise that once a bundler is in the loop. Those files declare no
16
+ * dependency on Prism — reading a global is invisible to the module graph — so
17
+ * nothing pins them after whoever publishes it, and a bundler is free to hoist
18
+ * them, split them into another chunk, or drop the publishing statement as a
19
+ * dead `globalThis` write. When that happens the chunk dies on load with
20
+ * `ReferenceError: Prism is not defined`, which takes down the whole route
21
+ * that lazily imported it rather than just the syntax highlighting.
22
+ *
23
+ * So the grammars are loaded with `import()` from inside `loadPrismGrammars()`
24
+ * instead. The global is published by a statement earlier in that same
25
+ * function body, which makes the ordering a runtime fact rather than a promise
26
+ * the bundler has to keep.
16
27
  */
17
28
  import Prism from "prismjs";
18
29
 
19
- (globalThis as typeof globalThis & { Prism?: unknown }).Prism ??= Prism;
30
+ /**
31
+ * The grammars to register, in order — several extend an earlier one, so this
32
+ * list is loaded sequentially rather than in parallel (`tsx` needs `jsx` and
33
+ * `typescript`; `cpp` needs `c`). Markup, CSS, C-like and JavaScript are
34
+ * absent because prismjs' entry point already bundles them.
35
+ */
36
+ const GRAMMARS: ReadonlyArray<() => Promise<unknown>> = [
37
+ () => import("prismjs/components/prism-typescript.js"),
38
+ () => import("prismjs/components/prism-jsx.js"),
39
+ () => import("prismjs/components/prism-tsx.js"),
40
+ () => import("prismjs/components/prism-python.js"),
41
+ () => import("prismjs/components/prism-bash.js"),
42
+ () => import("prismjs/components/prism-json.js"),
43
+ () => import("prismjs/components/prism-yaml.js"),
44
+ () => import("prismjs/components/prism-markdown.js"),
45
+ () => import("prismjs/components/prism-sql.js"),
46
+ () => import("prismjs/components/prism-rust.js"),
47
+ () => import("prismjs/components/prism-go.js"),
48
+ () => import("prismjs/components/prism-java.js"),
49
+ () => import("prismjs/components/prism-c.js"),
50
+ () => import("prismjs/components/prism-cpp.js"),
51
+ ];
52
+
53
+ /** Puts Prism where the grammar scripts look for it. Idempotent. */
54
+ function publishPrismGlobal(): void {
55
+ (globalThis as typeof globalThis & { Prism?: typeof Prism }).Prism ??= Prism;
56
+ }
57
+
58
+ publishPrismGlobal();
59
+
60
+ let loading: Promise<void> | undefined;
61
+
62
+ /**
63
+ * Registers the grammars above, once per runtime. Cheap to call repeatedly —
64
+ * callers are meant to invoke it wherever they are about to highlight, so that
65
+ * the work does not hinge on a module-level statement surviving tree-shaking.
66
+ *
67
+ * Never rejects: a grammar that fails to load just leaves its language out of
68
+ * `Prism.languages`, which every caller already treats as "render this block
69
+ * unhighlighted".
70
+ */
71
+ export function loadPrismGrammars(): Promise<void> {
72
+ loading ??= (async () => {
73
+ publishPrismGlobal();
74
+ for (const load of GRAMMARS) {
75
+ await load();
76
+ }
77
+ })().catch(() => {});
78
+ return loading;
79
+ }
20
80
 
21
81
  export default Prism;
@@ -1,30 +1,17 @@
1
1
  /**
2
- * @fileoverview Configures Prism.js with a fixed set of language grammars and exposes
3
- * a small `highlightCode` helper used by markdown-to-html.ts for code-block syntax
4
- * highlighting.
2
+ * @fileoverview Exposes a small `highlightCode` helper used by
3
+ * markdown-to-html.ts for code-block syntax highlighting. The Prism instance
4
+ * and its language grammars are owned by `prism-global.ts`.
5
5
  */
6
- // Must precede every `prismjs/components/*` import: the grammar scripts read
7
- // `Prism` off the global object, which this module publishes (see its docs).
8
- import Prism from "./prism-global";
9
- import "prismjs/components/prism-markup";
10
- import "prismjs/components/prism-css";
11
- import "prismjs/components/prism-javascript";
12
- import "prismjs/components/prism-typescript";
13
- import "prismjs/components/prism-jsx";
14
- import "prismjs/components/prism-tsx";
15
- import "prismjs/components/prism-python";
16
- import "prismjs/components/prism-bash";
17
- import "prismjs/components/prism-json";
18
- import "prismjs/components/prism-yaml";
19
- import "prismjs/components/prism-markdown";
20
- import "prismjs/components/prism-sql";
21
- import "prismjs/components/prism-rust";
22
- import "prismjs/components/prism-go";
23
- import "prismjs/components/prism-java";
24
- import "prismjs/components/prism-c";
25
- import "prismjs/components/prism-cpp";
6
+ import Prism, { loadPrismGrammars } from "./prism-global";
7
+
8
+ // Start the grammars loading now, and again from `highlightCode` below — the
9
+ // second call is what guarantees they are requested at all, since a bundler is
10
+ // free to drop this one as a side effect of a module it thinks is pure.
11
+ void loadPrismGrammars();
26
12
 
27
13
  export function highlightCode(code: string, lang: string): string | null {
14
+ void loadPrismGrammars();
28
15
  const grammar = Prism.languages[lang];
29
16
  if (!grammar) return null;
30
17
  return Prism.highlight(code, grammar, lang);