Skip to content

Import Prism without creating the global instance - #4168

Open
DmitrySharabin wants to merge 1 commit into
v2from
self-registration
Open

DmitrySharabin wants to merge 1 commit into
v2from
self-registration

Conversation

@DmitrySharabin

@DmitrySharabin DmitrySharabin commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Nothing pulls in the global instance on import anymore: the Prism class no longer imports it, and languages and plugins add themselves to a registry that imports only ComponentRegistry (#4156, #4158). Only the IIFE build (prism.js) creates globalThis.Prism.

<!-- IIFE build, then languages and plugins as modules (with #4156, #4158) -->
<script src="dist/prism.js"></script>
<script type="module" src="dist/languages/css.js"></script>
<script type="module" src="dist/plugins/line-numbers.js"></script>
<!-- Modules only: no global variable -->
<script type="module" src="dist/index.js"></script>
<script type="module" src="dist/languages/css.js"></script>
<script type="module" src="dist/plugins/line-numbers.js"></script>
<!-- Page settings, read by the IIFE build and by dist/index.js alike -->
<script>
	window.Prism = { languages: ["css"], plugins: ["line-numbers"], silent: true };
</script>
<script src="dist/prism.js"></script>
// Node: the global instance, without automatic highlighting
import Prism from "prismjs/global.js";
import "prismjs/languages/javascript.js";

let html = Prism.highlight("let a = 1", "javascript");
// Own instance: gets only what register() passes
import { Prism } from "prismjs/core.js";
import css from "prismjs/languages/css.js";

let prism = new Prism();
prism.register(css);
src/registry.js     globalThis.Prism?.registry ?? new ComponentRegistry()   ← languages and plugins add themselves (#4156, #4158)
src/core/prism.js   the global instance: reuses the one the IIFE build created, or creates it and registers everything from the registry
src/core.js         prismjs/core.js: the Prism and Token classes, no instance
src/global.js       prismjs/global.js: the global instance, without Autoloader and automatic highlighting
Changed lines
Code: +100 −73 + −
src/registry.js (new) 2 0
src/core/prism.js 9 1
src/auto-start.js 8 1
src/global.js 1 2
src/config.js 3 3
src/core.js 1 1
src/core/classes/prism.js 12 10
src/core/classes/component-registry.js 20 4
src/core/classes/plugin.js 1 6
src/core/highlight-element.js 10 12
src/core/highlight.js 5 7
src/core/highlight-all.js 3 5
src/core/tokenize/match.js 6 8
src/core/tokenize/tokenize.js 3 5
src/core/tokenize/util.js 3 5
src/load-languages.js 1 1
src/plugins/autoloader/autoloader.js 1 1
src/types.d.ts 0 1
package.json 10 0
scripts/build.js 1 0
Tests: +150 −0 + −
tests/core/global.js (new) 51 0
tests/fixtures/global-instance/ (new, 11 files) 82 0
tests/core/registry.js 17 0

🤖 Generated with Claude Code

@netlify

netlify Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit 5133b65
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6af5e893a3bf6b3767833939
😎 Deploy Preview https://deploy-preview-4168--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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Initialization-order bugs can trigger highlighting before DOMContentLoaded and leave components added during cached-plugin replay unregistered with the global instance.

2 open issues
What changed in this PR

Separates the Prism class from singleton initialization, allowing applications to create isolated instances without automatic highlighting. A shared component registry supports the global instance and reuse of the classic-script build.

Changes:

  • Exposes dedicated core and global package entries.
  • Moves page configuration and automatic highlighting out of the constructor.
  • Adds tests for registration order, instance isolation, and mixed classic-script/module usage.
File Description
tests/​fixtures/​global-instance/​window-config.js Checks global-variable configuration.
tests/​fixtures/​global-instance/​page-config.js Checks page settings and instance isolation.
tests/​fixtures/​global-instance/​no-global.js Checks modules avoid creating a global variable.
tests/​fixtures/​global-instance/​iife.js Simulates loading the classic-script build.
tests/​fixtures/​global-instance/​iife-reused.js Checks instance reuse and component registration.
tests/​fixtures/​global-instance/​iife-hooks.js Isolates simulated build modules.
tests/​fixtures/​global-instance/​iife-highlighted-once.js Counts automatic highlighting passes.
tests/​fixtures/​global-instance/​core-entry.js Checks core imports avoid singleton creation.
tests/​fixtures/​global-instance/​added-before.js Checks registration before singleton creation.
tests/​fixtures/​global-instance/​added-after.js Checks registration after singleton creation.
tests/​core/​registry.js Tests unsubscribe and missing-path errors.
tests/​core/​global.js Adds isolated-process singleton tests.
src/​types.d.ts Removes the instance-level manual option.
src/​registry.js Introduces the shared component registry.
src/​plugins/​autoloader/​autoloader.js Uses unified instance registration.
src/​load-languages.js Uses unified instance registration.
src/​global.js Re-exports the non-auto-starting singleton.
src/​core/​tokenize/​util.js Removes singleton fallback from grammar resolution.
src/​core/​tokenize/​tokenize.js Uses the calling instance for tokenization.
src/​core/​tokenize/​match.js Removes singleton fallback from grammar matching.
src/​core/​prism.js Creates or reuses the singleton and connects its registry.
src/​core/​highlight.js Uses the calling instance for highlighting.
src/​core/​highlight-element.js Uses the calling instance for element highlighting.
src/​core/​highlight-all.js Uses the calling instance for page highlighting.
src/​core/​classes/​prism.js Isolates constructor settings and adds register().
src/​core/​classes/​plugin.js Routes dependencies through unified registration.
src/​core/​classes/​component-registry.js Adds subscriptions and optional loading paths.
src/​core.js Exports the class without importing the singleton.
src/​config.js Separates automatic-highlighting settings from instance config.
src/​auto-start.js Centralizes automatic highlighting.
scripts/​build.js Adds the core build entry.
package.json Exposes core and global package exports.

Review effort: Balanced

Comment thread src/auto-start.js Resolved
Comment thread src/core/classes/component-registry.js Resolved
@DmitrySharabin
DmitrySharabin force-pushed the self-registration branch 3 times, most recently from 7dd8caa to 34f7931 Compare November 12, 2026 16:10
- `new Prism()` no longer highlights the page, and the `manual` option is gone: call `highlightAll()` yourself
Closes #4149

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Should plugins register themselves, and how do we import the Prism class without side effects?

2 participants