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

@wjfe/single-spa-svelte

v0.6.0

Published

> NPM package capable of creating single-spa lifecycle functions for Svelte v5 components.

Downloads

68

Readme

@wjfe/single-spa-svelte

NPM package capable of creating single-spa lifecycle functions for Svelte v5 components.

Svelte v5 is fundamentally different to previous versions. Pertaining the functionality found in the equivalent package from the single-spa team, the relevant changes are:

  1. Components are no longer classes. Instantiation of components now require the use of the mount() function from Svelte.
  2. Unmounting components is no longer done with the component's $destroy() method and instead it requires the use of the unmount() function from Svelte.
  3. Component properties are no longer updated with $set() and instead the props object is reactive state built with the $state() rune.

Quickstart

Install the NPM package:

npm i @wjfe/single-spa-svelte

Now use it to create lifecycle functions, almost as with the v4 version from the single-spa team:

import { singleSpaSvelte } from '@wjfe/single-spa-svelte';
import App from './App.svelte';

const lcc = singeSpaSvelte(App /*, domElementGetter, { options } */);

export const bootstrap = lcc.bootstrap;
export const mount = lcc.mount;
export const unmount = lcc.unmount;
export const update = lcc.update;

Migrating from Svelte v4

As seen in the previous code snippet, it is almost identical. The differences are in the arguments passed to the function. The NPM package from the single-spa team receives a single argument, while this version can receive up to three arguments.

Before (single-spa-svelte package from the single-spa team):

import singleSpaSvelte from 'single-spa-svelte';

const lcc = singleSpaSvelte({
    component: App
});

With this package, the export is not default, and the component is not part of the options. Other than this, the resulting functions should be conformant to what you are used to with single-spa.

The Options Parameter

Since v0.4.0

As seen in the Quickstart, singleSpaSvelte's third parameter is named "options". It accepts 3 properties:

  • preMount: Optional function that is run just before mounting the Svelte component.
  • postUnmount: Optionsl function that is run immediately after unmounting the Svelte component.
  • mountOptions: Optional set of options for Svelte's mount function.

For details on the last one, refer to Svelte's documentation. All properties are accepted, except for target.

The other two are optional functions that receive as only argument the target HTML element where the component will be mounted in (or it was mounted in). This is useful if you need to manipulate this element in any way. The use case that gave birth to this feature was to add CSS classes to the element, which need to be removed upon unmounting:

const lcc = singeSpaSvelte(App , undefined, {
    preMount: (target) => target.classList.add('flex-fill'),
    postUnmount: (target) => target.classList.remove('flex-fill'),
});

The target parameter is guaranteed to be defined.

The SspaParcel Component

This package also provides a SspaParcel component that should help Svelters out there to consume single-spa parcels in Svelte v5 projects. It works quite similarly to <svelte:component>, where the component is placed in markup and then via props, the component and its properties are set.

<SspaParcel sspa={{ config: parcelConfig }} {...restOfParcelProperties} />

You can collect the parcel properties in an object and then spread them as in the example above, or you may individually specify them in markup as it is normally done with other components.

The sspa Property

For a single-spa parcel to be successfully mounted using SspaParcel, the parcel configuration object or a function that returns it must be provided. The purpose of the sspa property is to allow passing this requirement.

The sspa property is an object in order to avoid reserving property names that may collide with the property names of the parcel component being mounted. It only has one property: sspa.config. In the future and if required, any new properties will be defined inside this sspa object property that serves as namespace.

[!IMPORTANT] It is recommended to implement the factory pattern for the lifecycle functions when it comes to parcels. See this GitHub issue opened for the Svelte v4 version of this package for details on how to implement a factory function.

The single-spa Context

Since v0.5.0

The entire single-spa library instance and the mountParcel function are available via context. If needed, import
getSingleSpaContext and call it to obtain the context. Remember to use this function in the initialization code of a component.