Highlight.js のバンドルサイズを削減する
2020/02/16 02:02:272020/06/07 21:51:39
理想.ts
["bash"].map(language => {return [language, require(`highlight.js/lib/languages/${language}`)]})
使いそう.ts
export const languages: { [k: string]: any } = {
sh: bash,
bash: bash,
coffee: coffeescript,
cpp: cpp,
cs: cs,
crystal: crystal,
css: css,
d: d,
dockerfile: dockerfile,
el: elixir,
elixir: elixir,
elm: elm,
er: erlang,
erlang: erlang,
glsl: glsl,
go: go,
hs: haskell,
java: java,
js: javascript,
javascript: javascript,
jsx: javascript,
json: json,
kt: kotlin,
kotlin: kotlin,
less: less,
lua: lua,
md: markdown,
markdown: markdown,
m: matlab,
matlab: matlab,
perl: perl,
php: php,
powershell: powershell,
python: python,
r: r,
rb: ruby,
ruby: ruby,
rs: rust,
rust: rust,
scala: scala,
scss: scss,
sql: sql,
stylus: stylus,
swift: swift,
tex: tex,
typescript: typescript,
ts: typescript,
tsx: javascript,
yml: yaml,
vue: vue,
xml: xml,
}
languages[ext]
してやると存在判定ができて便利です 取り込みたい.ts
import hljs from "highlight.js"
import vue from "highlightjs-vue"
hljs.registerLanguage("vue", vue)
正答例.ts
// @ts-ignore /lib/highlight には定義がありません
import hljs from "highlight.js/lib/highlight"
import vue from "highlightjs-vue"
hljs.registerLanguage("vue", vue)
highlightjs-vue の場合の正答例.ts
// @ts-ignore
import hljs from "highlight.js/lib/highlight"
// @ts-ignore
import { definer as vue } from "highlightjs-vue/dist/highlightjs-vue.esm"
hljs.registerLanguage("vue", vue)
登録.ts
Object.entries(languages).map(([language, parser]) => {
hljs.registerLanguage(language, parser)
})
Generated from
Highlight.js のバンドルサイズを削減する