-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Line Numbers] Automatically attach to the global Prism instance #3984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Conversation
|
||
export default Self; | ||
|
||
if (typeof document !== 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this check, Rollup chokes on bundling the code (when splitting it into chunks): in documentReady
, it tries to access a property of an undefined document
.
Sorry, I meant to approve the other one! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment
void documentReady().then(() => { | ||
const Prism = (globalThis as any).Prism; | ||
if (Prism) { | ||
Prism.components.add(Self); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be inflicting all this boilerplate on plugin authors!
As a design principle, syntax should not be more than the minimum required to declare intent. Boilerplate should be handled by suitable abstractions.
Besides the length and maintainability, whoever is including a plugin (either a Prism user or the plugin author adding the plugin to the global instance) should not need to care about when the document is ready. The Prism method one calls to add a plugin should automatically take care of that.
Also, ESM automatically takes care of running JS when the document is ready, so when using ESM one rarely needs to use a documentReady()
event. Is this for the global Rollup build? If so, we should approach it differently (but still in an abstraction, not every single plugin). Additionally, not all plugins even need to wait for the document to be ready, since most just add code to hooks, and the hooks already run at the right time.
As a priority of constituencies, it's users > Prism users > Plugin users > Prism maintainers.
All a plugin should need to do is something like:
import prism from "/path/to/global/prism"
export default const Self = { /* plugin */ };
prism.components.add(Self);
or even
import prism from "/path/to/global/prism"
export default prism.components.add({ /* plugin */ });
0f2efb1
to
8562579
Compare
✅ Deploy Preview for dev-prismjs-com ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This is part 2 of 2 in a stack made with GitButler:
.prettierignore
#3983