Skip to content

Commit 5ad4e78

Browse files
ritwik-gclaude
andcommitted
UN-3185 [FIX] Install global Prism unconditionally + correct rationale (review)
Address PR review: - silent-failure-hunter: a `!globalThis.Prism` guard could leave a different, pre-existing Prism in place, so the add-ons extend one instance while JsonView's highlightAll() reads another -> JSON silently unhighlighted. Assign unconditionally so the global is provably our core instance. - comment-analyzer: the prior comment blamed evaluation order yet relied on it to justify the fix. Rewrite: relying on prismjs core's self-install is unreliable under code-splitting; the explicit globalThis assignment (from first-party code imported before the add-ons) is the deterministic fix. Rebuilt: emitted chunk runs `globalThis.Prism = <core>` immediately before `Prism.languages.json = ...`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3798e91 commit 5ad4e78

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

frontend/src/helpers/prismSetup.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import Prism from "prismjs";
22

3-
// prismjs language/plugin add-ons (`prismjs/components/*`, `prismjs/plugins/*`)
4-
// reference a *bare global* `Prism` and carry no import edge back to prismjs
5-
// core. Under the Vite (ESM + code-split) build there is no guarantee the plain
6-
// side-effect `import "prismjs"` evaluates — and installs that global — before
7-
// those add-ons run, so they threw `ReferenceError: Prism is not defined`,
8-
// crashing Combined Output (Prompt Studio detail page + HITL review).
3+
// The prismjs add-ons imported alongside this module (`prismjs/components/*`,
4+
// `prismjs/plugins/*`) register their grammars onto a *bare global* `Prism` and
5+
// carry no import edge back to prismjs core. Leaving that global to prismjs
6+
// core's own self-install is unreliable under Vite's code-split production
7+
// build: the add-ons evaluated before anything installed `Prism`, throwing
8+
// `ReferenceError: Prism is not defined` and blanking Combined Output (Prompt
9+
// Studio detail page + HITL review).
910
//
10-
// Import THIS module before any add-on: it is a real dependency edge whose
11-
// `Prism` binding is used below (so it survives tree-shaking, unlike a bare
12-
// side-effect import), and its body pins core on the global. A sibling
13-
// dependency is fully evaluated — body included — before the next one, so the
14-
// global is guaranteed to exist by the time the add-on modules evaluate.
15-
if (!globalThis.Prism) {
16-
globalThis.Prism = Prism;
17-
}
11+
// Make it deterministic: the used `Prism` binding forces the bundler to retain
12+
// and evaluate core, and this module — imported before the add-ons — pins it on
13+
// the global so it exists by the time they register. Assign unconditionally:
14+
// the global must be THIS instance, the one the add-ons extend and that
15+
// JsonView's `Prism.highlightAll()` reads. A `!globalThis.Prism` guard could
16+
// leave a different, pre-existing Prism in place and silently drop highlighting.
17+
globalThis.Prism = Prism;
1818

1919
export default Prism;

0 commit comments

Comments
 (0)