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.

HLJS compiled from website does not work with browserify

See original GitHub issue

The compiled packs tag on:

hljs.registerLanguage(....)

to the build code, for each language you ask for. However, hljs is undefined at this point if we are using browserify (commonJS) to require it. Can be fixed specifically in this case by adding var hljs = module.exports before the registerLanguage lines. Personally I’d fix by returning the hljs object from the initial function that decided where to put it, and using the returned ref when registerLanguage-ing

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
myst729commented, Jan 7, 2017

The full npm package is quite big indeed.

In my project, I create a trimmed copy of /node_modules/highlight.js/lib/index.js. This allows me to choose a potion of all supported languages.

// This is a trimmed version of highlight.js main file
// See '/node_modules/highlight.js/lib/index.js' for all supported languages
// For more details, visit https://highlightjs.org/
var hljs = require('highlight.js/lib/highlight')
hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'))
hljs.registerLanguage('css', require('highlight.js/lib/languages/css'))
hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'))
module.exports = hljs

Then require this one instead of the npm package.

// var hljs = require('highlight.js')
var hljs = require('../assets/scripts/hljs.trimmed')

My production bundle file decreases for about 480KB.

1reaction
timacdonaldcommented, Jan 7, 2017

Yea, i’m just doing this along with all my other requires.

/**
 * Highlight.js
 */
var hljs = require('../node_modules/highlight.js/lib/highlight');
hljs.registerLanguage('php', require('../node_modules/highlight.js/lib/languages/php'));
hljs.initHighlightingOnLoad();

Not really hacking, just skipping the include every language file. Works well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use highlight.js
You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code. In...
Read more >
Home / Analyzing browserify bundles to minimize JavaScript ...
The problem was that while core of highlight.js is small, it supports 168 languages and doing import 'highlight.js' would bundle all of.
Read more >
Browserify - Bundle loads, but doesn't work afterwards
The problem is that after the code gets compiled the function isn't attached to the window object but instead is executed within a...
Read more >
brief-highlightjs | Yarn - Package Manager
Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server. It works with pretty...
Read more >
Properly shim AngularJS applications using Browserify
Pretty simple huh? But as you might expect, this doesn't work. Open index.html in a web browser and take a look at the...
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