Skip to content

Build dist/prism.js as IIFE for synchronous globalThis.Prism - #4087

Merged
DmitrySharabin merged 2 commits into
v2from
iife-global-build
Jun 17, 2026
Merged

Build dist/prism.js as IIFE for synchronous globalThis.Prism#4087
DmitrySharabin merged 2 commits into
v2from
iife-global-build

Conversation

@DmitrySharabin

@DmitrySharabin DmitrySharabin commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

  • prism.global.js used import() to dynamically load index.js, making globalThis.Prism available only asynchronously — breaking backward compatibility with v1 where Prism was available immediately after the <script> tag
  • Replace it with an IIFE build target that bundles auto-start.js (core + autoloader) into a single self-contained file (~20KB)
  • Rollup's name: 'Prism' assigns the default export to var Prism, making it synchronously available as a global
  • Languages are still loaded dynamically by the autoloader via import()
  • global.js now bridges the IIFE and ESM singletons: when globalThis.Prism is already a Prism instance (set by the IIFE), ESM plugins reuse it instead of creating a second one

Live demo: https://codepen.io/dmitrysharabin/pen/xbbdXZb

Test plan

  • Load dist/prism.js via <script> and verify globalThis.Prism is defined synchronously in the next <script> block
  • Verify autoloaded language highlighting still works (e.g. <code class="language-javascript">)
  • Verify ESM (import Prism from 'prismjs') and CJS (require('prismjs')) still work
  • Verify pre-load config (window.Prism = { manual: true }) is still read correctly
  • Verify ESM plugins (line-numbers, inline-color, show-invisibles) loaded via <script type="module"> work alongside the IIFE build
  • Verify Node.js (no IIFE) — globalThis.Prism stays undefined, ESM/CJS return a fresh instance

🤖 Generated with Claude Code

…is.Prism

The previous prism.global.js used a dynamic import() to load index.js, which
made globalThis.Prism available only asynchronously — breaking backward
compatibility with v1 where Prism was available immediately after the
<script> tag.

Replace prism.global.js with an IIFE build target that bundles auto-start.js
(core + autoloader) into a single file. Rollup's `name: 'Prism'` option
assigns the default export to `var Prism`, making it synchronously available
as a global. Languages are still loaded dynamically by the autoloader.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@netlify

netlify Bot commented Jun 15, 2026

Copy link
Copy Markdown

Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit 0a21097
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6a2fe2822cba360009ca72ba
😎 Deploy Preview https://deploy-preview-4087--dev-prismjs-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown

No JS Changes

Generated by 🚫 dangerJS against 0a21097

ESM plugins import the Prism singleton from global.js. When the IIFE
build has already set globalThis.Prism, global.js now reuses that
instance instead of creating a second one. This ensures plugins loaded
via <script type="module"> register on the same Prism instance as
the one exposed by <script src="dist/prism.js">.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@LeaVerou LeaVerou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only concern is that this will cause trouble if v1 Prism and v2 Prism co-exist in the same page. But if you do that, you'll probably have more issues anyway.

@DmitrySharabin

Copy link
Copy Markdown
Member Author

But if you do that, you'll probably have more issues anyway.

Exactly!

@DmitrySharabin
DmitrySharabin merged commit 1ef5c67 into v2 Jun 17, 2026
13 of 14 checks passed
@DmitrySharabin
DmitrySharabin deleted the iife-global-build branch June 17, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants