npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@288-toolkit/component-loader

v1.0.2

Published

```sh npm i @288-toolkit/component-loader ```

Downloads

134

Readme

Component-loader

npm i @288-toolkit/component-loader

createComponentLoader()

Creates a function that allows you to import Svelte components dynamically and add them to a data object as a svelteComponent property.

It takes a DynamicImports array that contains objects of the following form:

{
	key: string;
	getImport: (entry) => void | Promise<SvelteComponent>;
}

key

The key property is a string describing the path, in dot notation, of the property you want dynamically imported components for on the data object passed to the loader.

For example:

  • The key for a modules property on an entry property on the data object would be entry.modules.
  • The key for a dynamicEntries property inside of a module would be entry.modules.dynamicEntries and so on.
  • An empty string represents the data object itself.

getImport

The getImport function receives the object corresponding to the key as an argument and returns the import() call for the component.

If the value of the key is an object, getImport is called for that object only, and he svelteComponent property is added directly to it.

If the value is an array of objects, getImport is called for every objects of the array, and the svelteComponent property is added to all of them.

[!IMPORTANT] Do NOT await the import, it will not work. The 'awaiting' happens later.

[!IMPORTANT] You can return null or undefined if you don't want to import anything. For example, if you know the entry will not have a component.

Because of how Vite processes dynamic imports, there are several limitations to keep in mind when writing the import path. They are listed here: https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations.

Example

import { createComponentLoader } from '@288-toolkit/component-loader';

const loadComponents = createComponentLoader([
	{
		key: 'relatedItems',
		getImport: (entry: CardEntry) => import(`../cards/${entry.type}.svelte`)
	},
	// Dynamically import all modules components
	{
		key: 'entry.modules',
		getImport: (entry: ModuleEntry) => import(`../modules/${entry.section}.svelte`)
	},
	// Dynamically import card components on a module
	{
		key: 'entry.modules.cardItems',
		getImport: (entry: CardEntryInsideAModule) =>
			import(`../cards/${entry.unique.property}.svelte`)
	},
	// The properties can be at any level
	{
		key: 'entry.content.blocks.items.icons',
		getImport: (entry: IconEntry) => import(`../icons/${entry.iconName}.svelte`)
	}
]);

You can now load components from a universal load function:

+page.ts

import { loadComponents } from './loadComponents';

export const load = async (data) => {
	return loadComponents(data);
};

This component can then be rendered in the templates, usually with the ComponentSelector.svelte helper.

ComponentSelector.svelte

After having dynamically loaded the components, we need to render them. To do so, you can use the ComponentSelector.svelte component.

By default, ComponentSelector renders all components by passing them an entry prop corresponding to their associated data.

You can also use the default slot to render the components in any way you like. The slot receives the component and entry props.

Example

<script lang="ts">
	import ComponentSelector from '@288-toolkit/component-loader';

	export let data;

	const { modules } = data.entry;
</script>

<ComponentSelector entries={modules} />

<ComponentSelector entries={modules} let:component let:entry>
	<section>
		<svelte:component this={component} data={entry} darkMode />
	</section>
</ComponentSelector>