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

@laosb/archive-of-sb-banner

v1.0.2

Published

A web component inserted to archived sites on Archive.of.sb

Downloads

112

Readme

svelte-web-components-template

A base template for building a shareable web components library with Svelte.

How to use this template

You can directly create a new GitHub repo from this template by selecting the Use this template button on GitHub.

You can also clone it locally with the following commands:

npx degit sinedied/svelte-web-components-template#main my-component-lib
cd my-component-lib
npm install # or yarn

Your components source code lives in lib/ folder. Only components with the .wc.svelte extension will be exported as web components and available in your library. This means that you can also use regular Svelte components with the .svelte extension as child components for your implementation details.

You can add additional components by adding them to the lib folder and editing lib/index.js.

Testing your components

You can start a development server with:

npm run dev

Then open your browser to localhost:5000.

This will build the demo application located in the demo/ folder, in which you can use and test your components during development.

If you want to add unit tests, you can take a look at Jest and Jest testing library.

Using the built web components with the demo app

The demo application is provided for development and testing of your components, that's why it imports the .svelte files from the lib/ folder directly by default.

If you prefer, you can import the built web components from the dist/ folder instead, by editing demo/src/App.svelte and replacing the import '../../lib'; statement with import '../../dist'; if you have the bundleComponents option enabled, or individually import your components with import import '../../dist/MyComponent.wc.js'; otherwise.

You'll also have to make sure to run the npm run build script to generate the dist/ folder first.

Building the library

The command npm run build will create the web components library in the dist/ folder. It creates both a JavaScript module (dist/index.mjs) and a regular UMD script (dist/index.js).

The build is automatically called when executing npm publish or npm pack to distribute your library, thanks to the prepublishOnly scripts in package.json.

Notes and limitations

This template does not provide any web components polyfills for older browsers support. It's usually best to leave that task to the host application, hence why they're left out.

Props

Any props accepted by your web component are automatically transformed to element attributes. Since camelCase or PascalCase does not work in HTML, you have to make sure to name your props in lowercase.

<script>
  export let myvalue = "Default";
</script>

Events

The Svelte syntax event for listening to events like on:myevent doesnt work with events dispatched from a Svelte web component (#3119).

You need to use a workaround for that, by creating a CustomEvent and dispatching it with the composed: true option to cross the shadow DOM boundary.

Here's an example:

// MyComponent.wc.svelte
<svelte:options tag="my-component" />
<script>
  import { get_current_component } from "svelte/internal";
  
  const component = get_current_component();
  
  // example function for dispatching events
  const dispatchEvent = (name, detail) => {
    component?.dispatchEvent(new CustomEvent(name, {
      detail,
      composed: true  // propagate across the shadow DOM
    }));
  };
</script>
<button on:click={() => dispatchEvent("test", "Hello!")}>
  Click to dispatch event
</button>

Building each component into its own module

By default this template will build all components into a single module.

If you prefer to build each component into its own module, you can do so by editing rollup.config.js and setting the bundleComponents option to false.

Then you also need to replace the content of lib/index.js with:

export default () => {
  import('./MyComponent.wc.svelte');
  // Import each of your component this way
};

This will enable code-splitting and will generate an ES module for each component in the dist/ folder.

As you changed the way components are exported, you also need to replace the import '../../lib'; statement in demo/src/App.svelte to import import '../../lib/MyComponent.wc.svelte';.