我正在创建一个包装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);
});预期行为
图书馆项目中的虚拟组件应自动导入。
目前的解决办法:
如果vuetify组件是在父项目中导入的,那么组件就会被解析。但是,这会引起问题,因为库用户必须知道如何在全局上导入或导入什么,这将产生更大的包大小。
是否有其他方法来执行和满足下列标准:
peer only)
提前谢谢你。
发布于 2022-11-28 22:25:53
只是为了为Sasank描述的解决方案创建一个答案:
如果您只想消除错误,请将组件导入父项目,如下面的链接:https://next.vuetifyjs.com/en/features/treeshaking/#manual-imports所述
https://stackoverflow.com/questions/73767137
复制相似问题