I try to create a plugin in nuxt3 that uses highligh.js and use it on my components. But I can’t do it
I make this :
// @/plugins/highlight.js
import 'highlight.js/styles/stackoverflow-light.css'
import hljs from 'highlight.js/lib/core';
import json from 'highlight.js/lib/languages/json';
hljs.registerLanguage('json', json);
import 'highlight.js/styles/vs.css'
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(hljs)
})
I want to use like this :
// @/components/JSONView.vue
<template>
<highlightjs
language="json"
:code="myCode"
/>
</template>
<script setup>
const code = ref('{ "done": true }')
</script>
Thanks in advance
2
Answers
An additional note
If you use nuxt3 in SSR, your highlight.js plugin must contain the word
client
, likehighlight.client.js
and nothighlight.js
.Nuxt 3 relies on a SSR package that is not compatible with ESM modules.
By using
.client
in the name of plugin, the module is imported only on the browser side and in ESM module.You can use the official Vue plugin from highlight Js
npm i @highlightjs/vue-plugin
~/plugins/highlight.client.ts
Component usage
Tested and it works.