Available for hire

Could not find any match !

remark-svelte-auto-import -

What is this?

This plugin is part of the remark plugin infrastructure and meant to be used in conjunction with mdsvex . mdsvex allows to use Markdown to write your pages within the Svelte framework. Obviously it’s nice to use components within these Markdown files which is support but requires to add the corresponding import into the Markdown file. For instance:

<script>
    import { Chart } from '@chartfactory';
</script>
# Title

Here is my Chart:

<Chart />

Though feasible the script tag is somewhat annoying here as it’s only reason for existence is to satisfy technical requirements. This is where this plugin remark-svelte-auto-import comests into action. It integrates with the remark infrastructure and generates these imports. Using it would change the above section to this one:

# Title

Here is my Chart:

<Chart />

When should I use this?

This plugin is meant to be used in conjunction with mdsvex and only needed if you intend to use Svelte components within your Markdown.

Install

This package is ESM only . In Node.js (version 18+), install with pnpm :

pnpm install @kasisoft/remark-svelte-auto-import

Usage

Configuration

The configuration is fully typed using Typescript . RemarkSvelteAutoImportOptions is defined as followed:

export interface RemarkSvelteAutoImportOptions {
    
    debug               : Debug,
    
    /* generate ts lang attribute for non existent script nodes */
    scriptTS?           : boolean;

    /* scan for components */
    directories?       : string[];
    patterns?          : string[];
    
    /* alternatively/additionally provide a mapping between components and modules  */
    componentMap?      : {[component: string]: string};

} /* ENDINTERFACE */

You can import import the default settings DEFAULT_OPTIONS with the following setup:

export const DEFAULT_OPTIONS: RemarkSvelteAutoImportOptions = {
    debug               : Debug.None,
    scriptTS            : true,
    directories         : [
        'node_modules/'
    ],
    patterns            : [
        '**/*.svelte'
    ]
};

Examples

If all components you are using are provided though npm dependencies you can just add the node_modules directory to the configuration:

const config = {
    ...
    directories         : [
        'node_modules/'
    ],
    patterns            : [
        '**/*.svelte'
    ]
    ...
}

Let’s say you have the component Alert from the [flowbite svelte][flowbite-alert] this will cause an import such as this:

<script>
    import { Alert } from 'flowbite-svelte';
</script>

However if you intend to use your own components you need to provide a corresponding mapping to refer to such a component. Let’s say you have a component Garlic within src/lib/components/Garlic.svelte you need to provide a corresponding path mapping such as this:

const config = {
    ...
    localComponents: {
        'src/lib': '$lib'
    }
    ...
}

This causes the location src/lib/components/Garlic.svelte to be mapped to $lib/components/Garlic resulting in this script:

<script>
    import { Garlic } from '$lib/components/Garlic.svelte';
</script>