Vite vuetify插件不加载外部库中列出的组件

fdx2calv  于 2022-11-30  发布在  Vue.js
关注(0)|答案(1)|浏览(305)

我正在创建一个 Package 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.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'],
  },
});

下一个项目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);
});

预期行为

应自动导入库项目中的Vuetify组件。

当前解决方法:

如果在父项目中导入vuetify组件,则会解析这些组件。但这会导致问题,因为库用户必须知道导入什么或在全局上导入什么,这将创建更大的捆绑包大小。
是否有替代方式实施并满足以下标准:

  • Package 模块不依赖于vuetify(仅限对等部署)
  • 消费应用可以自动导入并获得摇树的所有好处
  • 消费应用不需要导入 Package 模块的任何对等依赖项。

先谢谢你了。

2uluyalo

2uluyalo1#

为了给Sasank描述的变通办法找到答案:
如果您只想消 debugging 误,请将组件导入父项目,如以下链接中所述:https://next.vuetifyjs.com/en/features/treeshaking/#manual-imports

相关问题