如何使VUE和VITE与Web组件一起使用?

发布于 2025-02-08 07:27:33 字数 874 浏览 3 评论 0 原文

我想将我的 vue 2 项目从webpack迁移到 vite 。 并且必须使用使用

这些组件在运行时(通过VUE)丢弃错误:

未知自定义元素:< foo-component> - 您注册了 组件正确?对于递归组件,请确保提供 “名称”选项。

还有(通过 lit-element

未能在“ Shadowroot”上设置“采用的列表”属性: 无法将值转换为“ CSSSTYLESHEET”。

据我所知,那些第三方Web组件仅在其索引文件中执行此操作( node_modules ):

import FooComponent from './FooComponent';
customElements.define('foo-component', FooComponent);

因此,(使用WebPack设置)之前,我只是导入了它们,并且所有用来工作的东西。好吧,实际上对于WebPack lit-scss-loader 也用于这些组件。

我认为Vite可能需要一些其他配置,或者在这里需要类似于“ WebPack”加载程序的东西,但不确定我必须移动什么方向。

我在做什么错?

I want to migrate my Vue 2 project from webpack to Vite.
And have to use 3rd party web components that built with lit-element.

Those components throws errors during the runtime (by vue):

Unknown custom element: < foo-component > - did you register the
component correctly? For recursive components, make sure to provide
the "name" option.

And also (by lit-element)

Failed to set the 'adoptedStyleSheets' property on 'ShadowRoot':
Failed to convert value to 'CSSStyleSheet'.

As far as I can see those 3rd party web components do only this in theirs index files (inside node_modules):

import FooComponent from './FooComponent';
customElements.define('foo-component', FooComponent);

So before (with webpack setup) I just imported them and everything used to work. Well, actually for webpack lit-scss-loader was used also for those components.

I assume that Vite perhaps needs some additional configuration, or maybe something similar to "webpack" loader is needed here, but not sure what direction I have to move.

What I'm doing wrong?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2025-02-15 07:27:33

configure @vite/plugin/plugin-vue 点亮的元素,例如,以 my-lit 开头的元素,其注册名称:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // treat all components starting with `my-lit` as custom elements
          isCustomElement: tag => tag.startsWith('my-lit'),
        },
      },
    }),
  ],
})

demo

Configure @vite/plugin-vue to ignore Lit elements, e.g., elements starting with my-lit in their registered name:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // treat all components starting with `my-lit` as custom elements
          isCustomElement: tag => tag.startsWith('my-lit'),
        },
      },
    }),
  ],
})

demo

つ低調成傷 2025-02-15 07:27:33

需要几个步骤。

想象一下,我有第三方网络操作人员名为“ Foo”。因此,所有这些都在 node_modules/@foo 中。

我需要执行以下步骤:

  1. 告诉VITE,所有组件以“ foo-”开头是Web Components。

    iscustomelement :( tag)=&gt; tag.startswith('foo-')

  2. 添加“ “插件可帮助Vite为WebComponents准备CSS。


  3. 告诉Vite如何处理WebComponents的CSS路径。

    '〜@foo':fileurltopath(new url('./ node_modules/@foo',import.meta.url))

这是完整的配置:

//vite.config.ts

import postcssLit from 'rollup-plugin-postcss-lit';

export default defineConfig({
  plugins: [
    vue(
      {
        template: {
          compilerOptions: {
            // 1. Tell Vite that all components starting with "foo-" are webcomponents
            isCustomElement: (tag) => tag.startsWith('foo-')
          }
        }
      }
    ),
    vueJsx(),
    // 2. This "postcssLit" plugin helps prepare CSS for the webcomponents
    postcssLit()
  ],
  resolve: {
    alias: {
      // 3. Tell Vite how to treat CSS paths for webcomponents
      '~@foo': fileURLToPath(new URL('./node_modules/@foo', import.meta.url))
    }
  }
});

A couple of steps are needed.

Imagine I have 3rd party webcomponents named "foo". So all of them are in node_modules/@foo.

I need to do these steps:

  1. Tell Vite that all components starting with "foo-" are webcomponents.

    isCustomElement: (tag) => tag.startsWith('foo-')

  2. Add the "postcssLit" plugin to help vite prepare the CSS for the webcomponents.

  3. Tell Vite how to treat CSS paths for webcomponents.

    '~@foo': fileURLToPath(new URL('./node_modules/@foo', import.meta.url))

Here is the full config:

//vite.config.ts

import postcssLit from 'rollup-plugin-postcss-lit';

export default defineConfig({
  plugins: [
    vue(
      {
        template: {
          compilerOptions: {
            // 1. Tell Vite that all components starting with "foo-" are webcomponents
            isCustomElement: (tag) => tag.startsWith('foo-')
          }
        }
      }
    ),
    vueJsx(),
    // 2. This "postcssLit" plugin helps prepare CSS for the webcomponents
    postcssLit()
  ],
  resolve: {
    alias: {
      // 3. Tell Vite how to treat CSS paths for webcomponents
      '~@foo': fileURLToPath(new URL('./node_modules/@foo', import.meta.url))
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文