# Loading syntax highlighting on demand?

**URL:** <https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840>\
**Category:** v6\
**Created:** [February 9, 2024, 5:54pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840 "2024-02-09T17:54:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjtorikian](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/gjtorikian/32/3442_2.png) [@gjtorikian](https://discuss.codemirror.net/u/gjtorikian)\
**Post date:** [February 9, 2024, 5:54pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840/1 "2024-02-09T17:54:18Z")

</div>

Hiya! I’ve noticed that when loading CM, a bevy of language parsers, from `@lezer` to `@codemirror/legacy-modes`.

I can use something like Vite to chunk the languages and load them separately. But what I can’t figure out is how to load the parser on demand. For example, if I allow a user to write markdown, and the write

```auto
backtick-backtick-backtick javascript

```

Is there an efficient (and, hopefully, prebuilt) way to load the JS highlighting once there’s a `keyup` on that final `t`?

Many thanks for any help.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [February 9, 2024, 7:25pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840/2 "2024-02-09T19:25:26Z")

</div>

All the language descriptions in @codemirror/language-data use dynamic imports. So if your bundler supports code splitting, they should just magically get loaded on demand.

---

<div class="post-metadata">

**Author:** ![gjtorikian](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/gjtorikian/32/3442_2.png) [@gjtorikian](https://discuss.codemirror.net/u/gjtorikian)\
**Post date:** [February 9, 2024, 9:01pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840/3 "2024-02-09T21:01:06Z")

</div>

Hm, I must be doing something wrong then. Is [CodeMirror Bundling Example](https://codemirror.net/examples/bundle/) still the best documentation for learning more about this?

---

<div class="post-metadata">

**Author:** ![gjtorikian](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/gjtorikian/32/3442_2.png) [@gjtorikian](https://discuss.codemirror.net/u/gjtorikian)\
**Post date:** [February 9, 2024, 10:10pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840/4 "2024-02-09T22:10:58Z")

</div>

To expand on the original question: we’re doing this within a Markdown context. We `import { languages } from "@codemirror/language-data";` and pass that into `codeLanguages`. But we don’t necessarily know which of those imported languages ought to be loaded, until a user begins writing a `pre` block with syntax highlighting.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [February 9, 2024, 10:38pm UTC](https://discuss.codemirror.net/t/loading-syntax-highlighting-on-demand/7840/5 "2024-02-09T22:38:31Z")

</div>

Yes, that’s the idea. Each of these holds a function that, when called, runs an `import(...)` expression that fetches the actual language mode. If you set up Rollup or whatever you’re using to split your bundle on dynamic imports, they shouldn’t be part of the ‘main’ bundle.
