Skip to content

Commit

Permalink
docs: add dynamic import example for code-splitting (#234)
Browse files Browse the repository at this point in the history
* chore(examples): add `DynamicImport` component

* docs: add section on code-splitting
  • Loading branch information
metonym authored Aug 7, 2022
1 parent 97c8e5c commit c179b55
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 12 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,38 @@ Refer to the highlight.js [language definition guide](https://highlightjs.readth
<Highlight {language} code="..." />
```

## Code-splitting

You can use the `dynamic import` syntax to code-split code.

In the example below, the `HighlightAuto` component and injected styles are dynamically loaded.

```svelte
<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}`}
/>
```

## API

### Props
Expand Down
2 changes: 1 addition & 1 deletion examples/rollup/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<script src="build/bundle.js"></script>
<script type="module" src="build/index.js"></script>
</body>
</html>
5 changes: 2 additions & 3 deletions examples/rollup/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export default {
input: "src/index.ts",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/bundle.js",
format: "es",
dir: 'public/build',
},
plugins: [
svelte({
Expand Down
5 changes: 3 additions & 2 deletions examples/rollup/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { Highlight, HighlightAuto } from "svelte-highlight";
import { Highlight } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
import DynamicImport from "./DynamicImport.svelte";
const code = "const add = (a: number, b: number) => a + b;";
</script>
Expand All @@ -12,4 +13,4 @@

<Highlight language={typescript} {code} />

<HighlightAuto langtag code={`body {\n padding: 0;\n color: red;\n}`} />
<DynamicImport />
23 changes: 23 additions & 0 deletions examples/rollup/src/DynamicImport.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { onMount } from "svelte";
let component;
let styles;
onMount(async () => {
component = (await import("svelte-highlight")).HighlightAuto;
styles = (await import("svelte-highlight/styles/atom-one-dark")).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}`}
/>
23 changes: 23 additions & 0 deletions examples/sveltekit/src/routes/DynamicImport.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { onMount } from "svelte";
let component;
let styles;
onMount(async () => {
component = (await import("svelte-highlight")).HighlightAuto;
styles = (await import("svelte-highlight/styles/atom-one-dark")).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}`}
/>
5 changes: 3 additions & 2 deletions examples/sveltekit/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { Highlight, HighlightAuto } from "svelte-highlight";
import { Highlight } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
import DynamicImport from "./DynamicImport.svelte";
const code = "const add = (a: number, b: number) => a + b;";
</script>
Expand All @@ -12,4 +13,4 @@

<Highlight language={typescript} {code} />

<HighlightAuto langtag code={`body {\n padding: 0;\n color: red;\n}`} />
<DynamicImport />
5 changes: 3 additions & 2 deletions examples/vite/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { Highlight, HighlightAuto } from "svelte-highlight";
import { Highlight } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
import DynamicImport from "./DynamicImport.svelte";
const code = "const add = (a: number, b: number) => a + b;";
</script>
Expand All @@ -12,4 +13,4 @@

<Highlight language={typescript} {code} />

<HighlightAuto langtag code={`body {\n padding: 0;\n color: red;\n}`} />
<DynamicImport />
23 changes: 23 additions & 0 deletions examples/vite/src/DynamicImport.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { onMount } from "svelte";
let component;
let styles;
onMount(async () => {
component = (await import("svelte-highlight")).HighlightAuto;
styles = (await import("svelte-highlight/styles/atom-one-dark")).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}`}
/>
5 changes: 3 additions & 2 deletions examples/webpack/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import { Highlight, HighlightAuto } from "svelte-highlight";
import { Highlight } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
import DynamicImport from "./DynamicImport.svelte";
const code = "const add = (a: number, b: number) => a + b;";
</script>
Expand All @@ -12,4 +13,4 @@

<Highlight language={typescript} {code} />

<HighlightAuto langtag code={`body {\n padding: 0;\n color: red;\n}`} />
<DynamicImport />
23 changes: 23 additions & 0 deletions examples/webpack/src/DynamicImport.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { onMount } from "svelte";
let component;
let styles;
onMount(async () => {
component = (await import("svelte-highlight")).HighlightAuto;
styles = (await import("svelte-highlight/styles/atom-one-dark")).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}`}
/>

0 comments on commit c179b55

Please sign in to comment.