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

@web-component-wrapper/core

v0.3.1

Published

> [!WARNING] > CAUTION: This package is currently in the early stages of development, and its API may undergo changes in the future.

Downloads

3

Readme

@web-component-wrapper/core

[!WARNING] CAUTION: This package is currently in the early stages of development, and its API may undergo changes in the future.

@web-component-wrapper/core extracts metadata (properties, events, slots, CSS custom properties) and types (TypeScript interfaces, enums, etc.) from the source files of web components. This information is then passed to the generator selected by user, which is responsible for converting the metadata into framework-specific code and files.

Installation

npm i @web-component-wrapper/core -D

Generators

Select which generator you want to use:

Create custom generator

To create a custom generator, you need to implement the ComponentsGenerator interface.

import { ComponentsGenerator, ComponentMetadata, Config } from '@web-component-wrapper/core';

export class MyGenerator implements ComponentsGenerator {
    generate(componentMetadata: ComponentMetadata[], config: Config): Promise<void> | void {
        // Your implementation
    }
}

Configuration

export interface Config {
  /**
   * The source files to process. It should point to web-component source files.
   * I.e "src/**.ts" or ["src/my-button.ts", "src/my-input.ts"]
   */
  src: string | string[]
  /**
   * The destination directory, where the generated files will be placed.
   */
  dist: string,
  /**
   * Function for filtering web components for wrapper generation.
   * Use it to exclude components that are not ready or intended for internal use only.
   * By default, all components are included.
   * Example usage:
   *   filter: (filePath, componentMetadata) => !componentMetadata.sourceFile.text.includes('// web-component-wrapper-disable')
   */
  filter?: (filePath: string, componentMetadata: any) => boolean,
  /**
   * The generator to use. Use framework specific generator.
   */
  generator: ComponentsGenerator,
  webComponentProvider: (componentMetadata: ComponentMetadata) => {
    /**
     * Function that provides the specific web component from your library and integrates it into the window context.
     * The implementation depends on how your library exposes web components. It may involve:
     * - Importing the component and defining it using customElements.define,
     * - Importing a file with side effects for auto-registration,
     * - No action required if the component is already available globally.
     *
     * Examples:
     * #1: Library with named exports that require manual component registration
     * ```typescript
     * ({ className }) => `
     * import { ${className} } from "my-design-system";
     *
     * try {
     *   customElements.define("${kebabCase(className)}", class extends ${className} {});
     * } catch (e) {}
     * `
     * ```
     *
     * #2: Library with auto-registered components that require importing a file with side effects
     * ```typescript
     * ({ className }) => `
     * import "my-design-system/${className}";
     * `
     * ```
     *
     * #3: Library with globally available components that do not require additional code
     * ```typescript
     * ({ className }) => ``
     * ```
     */
    code: string,
    /**
     * Tag name of the mounted web component
     */
    tagName: string
  },
  /**
   * The TypeScript configuration, if your web-components are written in TypeScript.
   */
  typescript?: {
    /**
     * Path to the tsconfig.json file.
     */
    project?: string,
    /**
     * Additional files to include in the TypeScript program. For example globals files.
     * Please ensure that files listed in `includes` field exports something, otherwise the program will end with an error.
     * The easiest way to do it is to add `export {};` at the end of the file.
     */
    includes?: string[]
  }
}

Usage

Generate config file

const { processProject } = require('@web-component-wrapper/core');
const { paramCase } = require('change-case');

processProject({
  src: '../../my-component-library/src/**/*.ts',
  dist: './generated-components',
  webComponentProvider: ({ className }) => {
    // Example: Library with named exports that require manual component registration
    const tagName = paramCase(className);

    return {
      code: `
        import { ${className} } from "my-component-library";
        
        try {
          customElements.define("${tagName}", class extends ${className} {});
        } catch (e) {}
      `,
      tagName
    }
  },
  typescript: {
    project: '../../my-component-library/tsconfig.json',
    includes: [
      '../../my-component-library/src/globals.ts'
    ]
  },
  generator: new FrameworkSpecificGenerator(options) // select right generator
});

Run generator

node ./path/to/config/file.js