如何将VUE3组件库作为独立的Web组件?

发布于 2025-02-09 19:36:51 字数 1040 浏览 1 评论 0原文

我在VITE架构中有一组VUE3组件,并希望将它们作为纯Web组件的Lib发布。如何使用我的构建文件进行?

我跟随Vite文档建立了一个自由。它说要将其添加到vite.config.js文件中:

const path = require('path');
const {defineConfig} = require('vite');
const vue = require('@vitejs/plugin-vue');

// https://vitejs.dev/config/
export default defineConfig({
    build: {
        lib: {
            entry: path.resolve(__dirname, 'src/components/index.js'),
            name: 'TykaynLib',
            fileName: (format) => `tykayn-lib.${format}.js`,
        },
        rollupOptions: {
            external: ['vue'],
            output: {
                // Provide global variables to use in the UMD build
                // Add external deps here
                globals: {
                    vue: 'Vue',
                    TykaynLib : 'TykaynLib'
                },
            },
        },
    },
    plugins: [vue()],
});

因此,在构建后,我将获得包含我组件的ES和UMD文件,但它们不是独立的,他们要求Vuejs能够工作。

我想要纯Web组件,只需加载脚本并只需要在HTML文件中添加我的自定义HTML标签即可使用它们。我不想为VUE制作插件,也不想要求组件的最终用户必须使用VUE。 有办法建立这个吗?

I have a set of Vue3 components in a Vite architecture and want to publish them as a lib of pure web components. how can i use my built files to do so ?

I followed the Vite docs to build a lib. It says to add this in the vite.config.js file:

const path = require('path');
const {defineConfig} = require('vite');
const vue = require('@vitejs/plugin-vue');

// https://vitejs.dev/config/
export default defineConfig({
    build: {
        lib: {
            entry: path.resolve(__dirname, 'src/components/index.js'),
            name: 'TykaynLib',
            fileName: (format) => `tykayn-lib.${format}.js`,
        },
        rollupOptions: {
            external: ['vue'],
            output: {
                // Provide global variables to use in the UMD build
                // Add external deps here
                globals: {
                    vue: 'Vue',
                    TykaynLib : 'TykaynLib'
                },
            },
        },
    },
    plugins: [vue()],
});

So after the build i get ES and UMD files containing my components, but they are not standalone, they require VueJS to be able to work.

I wanted pure web components that i could use by just loading a script and having to just add my custom HTML tags inside an HTML file. I do NOT want to make a plugin for vue or require the end users of my components to have to use Vue.
Is there a way to build this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文