Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic import example #219

Closed
metonym opened this issue Jul 1, 2022 · 2 comments · Fixed by #234
Closed

Add dynamic import example #219

metonym opened this issue Jul 1, 2022 · 2 comments · Fixed by #234
Labels
documentation Improvements or additions to documentation

Comments

@metonym
Copy link
Owner

metonym commented Jul 1, 2022

This is less of an issue for SvelteKit which will already code-split your app.

However, for other SPA set-ups, it would be nice to have a quick primer on dynamically importing a language grammar, which can be quite heavy.

@metonym metonym added the documentation Improvements or additions to documentation label Jul 1, 2022
@mcndt
Copy link

mcndt commented Aug 6, 2022

+1 on this. Currently trying to solve this issue for a project where codeblocks are rendered client-side after the code is decrypted.

@metonym
Copy link
Owner Author

metonym commented Aug 7, 2022

A recipe for code-splitting is now documented in the README. In addition, a DynamicImport component has been added to each example set-up.

The nice thing about SvelteKit/Vite/Webpack is that code-split the build with zero-configuration. The Rollup example needed a few tweaks to the rollup.config.js.

<script>
  import { onMount } from "svelte";

  let component;
  let styles;

  onMount(async () => {
    component = (await import("svelte-highlight")).HighlightAuto;
    styles = (await import("svelte-highlight/styles/github")).default;
  });
</script>

<svelte:head>
  {#if styles}
    {@html styles}
  {/if}
</svelte:head>

<svelte:component
  this={component}
  langtag
  code={`body {\n  padding: 0;\n  color: red;\n}`}
/>

As a result, the code is split into chunks, which can improve performance.

dynamic-import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants