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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tzxhy/create-web-component

v0.0.4

Published

@tzxhy/create-web-component --- 将 `Vue3/React` 组件,转换为通用 `web component(包含各自运行时)`。

Readme

@tzxhy/create-web-component

Vue3/React 组件,转换为通用 web component(包含各自运行时)

安装

yarn add @tzxhy/create-web-component

同时确保,react,react-domvue 依赖已安装。

函数签名说明

/** 注册 Vue 组件为 web component */
export function registryVueElement<
    P extends Record<string, any>
>(name: string, VComponent: import('vue').Component<P>, style?: string): void;

/** 注册 React 组件为 web component */
export function registryReactElement<
    P extends Record<string, any>
>(name: string, RComponent: import('react').FunctionComponent<P> | import('react').ComponentClass<P> | string, style?: string): void;

/** 传递给 web component 组件的参数包装器 */
export function t<T>(v: T): string;

其中的 style 参数为 ShadowDOM 下的 局部 style,在 vite 项目中可以使用如下形式来传入样式:

import style from './component.css?inline';

registryVueElement('name', Component, style);

使用

使用 React 组件注册的 web component

import {
    useEffect,
    useState,
} from 'react';

import {
    registryReactElement,
} from './index';
import {
    t,
} from './utils/attr2prop';

function ReactComponentTest(props: {name: string, onclick: () => void}) {
    const [count, updateCount] = useState(0);
    useEffect(() => {
        const t = setInterval(() => {
            updateCount((i) => i + 1);
        }, 1000);
        return () => {
            window.clearInterval(t);
        };
    }, []);
    return <div onClick={props.onclick}>React {props.name}。count: {count}</div>;
}

// 注册 react 组件为 web component
registryReactElement('component-test-react', ReactComponentTest, ':host{font-size: 18px}');

{
    const root = document.querySelector('#react-root')!;
    [...root.children].forEach((i) => i.remove());
    const e = document.createElement('component-test-react');
    e.setAttribute('name', t('tanzhixuan in reactify web component'));
    e.setAttribute('onclick', t((() => console.log('click in reactify web component'))));
    root.appendChild(e);
}

使用 Vue 组件注册的 web component

vue3 单组件,Test.vue:

<template>
    <div
        :class="css.test"
        @click="onclick">
        <span>Vue component: </span>{{ name }}。count: {{ count }}
    </div>
</template>

<script lang="ts" setup>import {
    onMounted, onUnmounted, ref,
} from 'vue';

defineProps<{
    name: string;
    onclick:() => void;
}>();

const count = ref(0);

let t: number;
onMounted(() => {
    t = setInterval(() => {
        count.value += 1;
    }, 1000);
});
onUnmounted(() => {
    window.clearInterval(t);
});

</script>
<script lang="ts">
export default {
    name: 'Test',
};
</script>

<style lang="less" module="css">
.test {}
</style>

注册及使用:

import {
    registryVueElement,
} from './index';
import Test from './test.vue';
import {
    t,
} from './utils/attr2prop';


// 注册 vue web component
registryVueElement('component-test-vue', Test, ':host{font-size: 26px}');
{
    const root = document.querySelector('#vue-root')!;
    [...root.children].forEach((i) => i.remove());
    const e = document.createElement('component-test-vue');
    e.setAttribute('name', t('tanzhixuan in vue web component'));
    e.setAttribute('onclick', t((() => console.log('click in vue web component'))));
    root.appendChild(e);
}

跨框架使用 web component

参考各自框架的引入说明。书写web component 类型定义可参考:

// react 定义
declare global {
    namespace JSX {
        interface TestReactComponentAttributes {
            name: string;
            onclick: string;
        }
        interface IntrinsicElements {

            // React 组件
            'test-react-component': TestReactComponentAttributes;
        }
    }
}
// vue 定义
declare module '@vue/runtime-core' {
    export interface TextVueComponentElement extends globalThis.JSX.TestReactComponentAttributes {
        /** 点击回调。实际数据格式为:(d: Point) => void */
        onclick: string;
    }
    export interface GlobalComponents {
        TextVueComponent: TextVueComponentElement;
    }
}

ChangeLog

0.0.4

支持监听属性改变。