通过vite+vue3打包umd库,通过cdn引入发现报错
Cannot read properties of undefined (reading 'openBlock')
发现是打包之后global拿不到Vue,不知如何解决?
具体操作如下:
vite.config.js
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
build: {
lib: {
entry: "src/core.js",
name: "fcl",
formats: ["es", "umd", "cjs"],
},
rollupOptions: {
external: ["vue"],
output: {
globals: {
vue: "Vue",
},
},
},
},
});
打包入口
import YourComponent from "./YourComponent.vue";
const components = [YourComponent];
const install = (Vue, options = {}) => {
if (!options.store) console.log("Please provide a store!!");
components.forEach((component) => {
Vue.component(component.name, component);
});
export default {
install,
YourComponent,
};
引入
<script src="/fcl.umd.js"></script>
使用