【问题标题】:Vite vuetify plugin doesn't load components listed in external librariesVite vuetify 插件不加载外部库中列出的组件
【发布时间】:2022-11-19 21:15:01
【问题描述】:

我正在创建一个包装 Vuetify 3 组件的库。但是当我尝试使用该库时,它会出现以下错误: [Vue warn]: Failed to resolve component: v-btn If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

图书馆vite.config.ts

import { fileURLToPath, URL } from 'node:url';
import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vuetify from 'vite-plugin-vuetify';

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    // vuetify({ autoImport: true, styles: 'none' }), // Don't export vuetify
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  build: {
    lib: {
      entry: resolve(__dirname, 'src/main.ts'),
      name: '@my/ui',
      // the proper extensions will be added
      fileName: 'my-ui',
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ['vue', 'vuetify'],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: 'Vue',
          vuetify: 'Vuetify',
        },
      },
    },
  },
});

Nuxt项目nuxt.config.ts

import { defineNuxtConfig } from 'nuxt';
import vuetify from 'vite-plugin-vuetify';

export default defineNuxtConfig({
  css: ['@/assets/css/main.css'],
  modules: [
    async (options, nuxt) => {
      nuxt.hooks.hook('vite:extendConfig', (config) =>
        config.plugins.push(vuetify({ autoImport: true }))
      );
    },
  ],
  build: {
    transpile: ['@my/ui', 'vuetify'],
  },
});

Nuxt项目app.vue

<template>
 <v-app>
   <v-main>
     <HelloWorld label="Test" primary />
   </v-main>
 </v-app>
</template>

<script lang="ts" setup>
import { HelloWorld } from '@my/ui';
</script>

Nuxt项目插件vuetify.ts

import 'vuetify/styles';
import { createVuetify } from 'vuetify';
import * as components from 'vuetify/components';
import * as directives from 'vuetify/directives';

export default defineNuxtPlugin((nuxtApp) => {
  const vuetify = createVuetify({
    // components, if imported components getting resolved but treeshaking doesn't work. 
    // directives
  });
  nuxtApp.vueApp.use(vuetify);
});

预期行为

来自 Library 项目的 Vuetify 组件应该被自动导入。

当前解决方法:

如果在父项目中导入了 vuetify 组件,那么这些组件将被解析。但这会导致问题,因为库用户必须知道要导入什么或在全局上导入什么,这会创建更大的包大小。

是否有替代方法来实施并满足以下标准:

  • 包装模块不依赖于 vuetify(仅限 Peer dep)
  • 消费应用程序可以自动导入并获得 tree shaking 的所有好处
  • 使用应用程序不需要导入包装模块的任何对等依赖项。

非常感谢你提前。

【问题讨论】:

    标签: vue.js vuetify.js vite nuxtjs3


    【解决方案1】:

    你解决了这个问题吗?我想创建一个扩展 vuetify 的库。我将 vuetify 添加为对等依赖项,但是当我安装库时,我将这个问题发送给[Vue warn]: Failed to resolve component: v-btn If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2022-11-10
      • 2021-10-30
      • 2020-08-30
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多