mattjennings / mdsvexamples

Render your Svelte code blocks in MDSveX
31 stars 2 forks source link

imports from $lib #18

Closed jjagielka closed 1 year ago

jjagielka commented 1 year ago

All works perfectly with:

<script>
import Component from "$lib/Component.svelte";
import { Component2 } from "$lib"; // if you created 'index.js' in src/lib
</script>

However, if you're building a svelte package and want to supply nice documentation for it, using $lib is not the best option. Examples should show that you import from your just being built package. Is there a trick then to replace $lib with the package_name in the rendered examples?

<script>
import Component from "my-package-name/Component.svelte";
import { Component2 } from "my-package-name"; // if you created 'index.js' in src/lib
</script>
mattjennings commented 1 year ago

Easiest way would be to create an alias in vite (and tsconfig if applicable) of my-package-name -> src/lib. I do this for my svelte-modals package

https://github.com/mattjennings/svelte-modals/blob/main/vite.config.js#L12

jjagielka commented 1 year ago

Perfect!