Last active
March 21, 2019 10:41
-
-
Save daliborgogic/0c578a0d8aeff76138a02b83f5af7919 to your computer and use it in GitHub Desktop.
From ~729KB to ~24.2KB (~11.2KB Gzip)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Core import | |
import hljs from 'highlight.js/lib/highlight' | |
addEventListener('message', async event => { | |
const { language, code } = event.data | |
// Language import | |
const x = await (await import(`highlight.js/lib/languages/${language}`)).default | |
// Register language | |
hljs.registerLanguage(language, x) | |
const result = hljs.highlightAuto(code) | |
postMessage(result.value) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webpack from 'webpack' | |
import WorkerPlugin from 'worker-plugin' | |
export default { | |
build: { | |
plugins: [ | |
new WorkerPlugin() | |
], | |
extend: ({ output }) => { | |
// output.globalObject is set to "window". It must be set to "self" to support HMR in Workers. | |
output.globalObject = 'self' | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<pre v-html="highlight" /> | |
</template> | |
<script> | |
export default { | |
props: { | |
content: { | |
type: String, | |
default: '' | |
}, | |
language: { | |
type: String, | |
default: 'javascript' | |
} | |
}, | |
data () { | |
return { | |
highlight: this.content | |
} | |
}, | |
beforeMount () { | |
if (process.browser) { | |
const worker = new Worker('@/assets/workers/highlight.worker.js', { type: 'module' }) | |
worker.postMessage({ | |
language: this.language, | |
code: this.highlight | |
}) | |
worker.onmessage = event => this.highlight = event.data | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment