question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow injection of Highlight.js instance (module singleton not guaranteed across submodules)

See original GitHub issue

Any import of Highlight.js refers to the same singleton instance of the library, so configuring the library anywhere configures it everywhere.

The module singleton works most of the time but it’s not guaranteed across submodules in node.js.

A module “singleton” is based on the resolved file path of the imported module url (that’s not a “great article” but it does define the import process).

Normally, when version requirements match, all packages will collapse down to a single highlight.js in the parent package. It is perfectly acceptable for the main project code and every sub module to rely on a separate version of highlight.js though as they can each resolve to their own node_modules copy of the module and each have their own “singleton” (per resolved file path).

I haven’t dug into the new vue plugin completely so if there is some window.hljs magic happening that was briefly mentioned above ignore this…

It looks like with the current setup @highlightjs/vue-plugin has a dependency of "highlight.js": "^10.7.1". If the parent package that imports @highlightjs/vue-plugin had a lock file at 10.7.0 the project would end up structured like this:

my-super-project/
  node_modules/
    highlight.js/ (10.7.0 "singleton")
    @hightlightjs/
      vue-plugin/
        node_modules/
          highlight.js/ (10.7.1 "singleton")

Here the docco example hljs.registerLanguage('javascript', javascript) would not be seen by the hljs instance in the @highlightjs/vue-plugin module.

Barring the parent package injecting hljs into the plugin already suggested above, you start needing peer dependencies which have their own issues.

It’s a bit of a wat moment when you do run into the “not quite a singleton” issue in the wild, so personally I only rely on module singletons within a single module/package. The super esoteric version of this I’ve seen is when running on case insensitive file systems and one file in your project does an import on a differently cased name of a file : /

_Originally posted by @mhio in https://github.com/highlightjs/highlight.js/issues/2815#issuecomment-813751467_

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
joshgoebelcommented, Sep 19, 2021

I think this is closed with #17 also and 2.1.0.

1reaction
HelixHexagoncommented, Jun 11, 2021

Thanks for the speedy reply! 🙂 I’m not sure what I did wrong yesterday but from testing today things are starting to make a bit more sense. So, as you already know, the problem comes from installing @highlightjs/vue-plugin@next and highlight.js at a version other than ^10.7.1 (e.g. highlight.js@9, highlight.js@10.7.0, or highlight.js@11.)

In any of these cases, NPM will also install a separate copy of highlight.js@^10.7.1 within the vue plugin folder and hljs will not work as a singleton. This structure will be saved in the package-lock.json and I’m not sure how to fix it without deleting the lock file and node_modules then starting afresh with npm install highlight.js@10 @highlightjs/vue-plugin@next.

I’m still in favour of using injection to allow people to bring whatever version they please, but for the time being we could update the readme to specify ^10.7.1 should be used? I think I got in to this mess by allowing highlight.js to install @ latest, i.e. 11.0.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid singleton in node - Stack Overflow
Modules result in singletons in Node. If this is undesirable, a workaround always depends on specific package. A preferable way is to export ......
Read more >
How to use highlight.js
You can get highlight. js as a hosted, or custom-build, browser script or as a server module. Right out of the box the...
Read more >
ads.common package — ADS 2.7.0 documentation
Creates api keys configuration and signer with extra arguments necessary for creating clients. Signer constructed from the oci_config provided. If not ' ...
Read more >
github-stats-1.0.txt - IPython
:ghpull:`4136`: catch javascript errors in any output * :ghpull:`4171`: add ... not working in 1.0.0 * :ghissue:`3990`: Buitlin `input` doesn't work over ......
Read more >
list of all node.js modules created on June 24, 2013
JS style) or let it handle by a specialized handler =temsa 2012-09-18 17:12 ... npm package example =shawjia 2012-05-25 17:11 0.0.1 anotherpackage ERROR:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found