Lazy load highlight.js
This significantly reduces the bundle size(over 1MiB!) but it also uses some hacks to dynamically load browserify modules on runtime(see lazy-load-modules.jspull/24/head
parent
4d59b1a9ac
commit
20e94f05e7
@ -0,0 +1,2 @@
|
||||
const hljs = require("highlight.js")
|
||||
module.exports = hljs
|
@ -0,0 +1,19 @@
|
||||
// I hate this with passion
|
||||
async function lazyLoad(url) {
|
||||
const cache = window.lazy_load_cache || new Map()
|
||||
if (cache.get(url)) return cache.get(url)
|
||||
|
||||
const module = loadModuleWithoutCache(url)
|
||||
cache.set(url, module)
|
||||
return module
|
||||
}
|
||||
|
||||
// Loads the module without caching
|
||||
async function loadModuleWithoutCache(url) {
|
||||
const src = await fetch(url).then(r => r.text())
|
||||
let module = {}
|
||||
eval(src)
|
||||
return module.exports
|
||||
}
|
||||
|
||||
module.exports = {lazyLoad}
|
Loading…
Reference in New Issue