Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not running on Vue 2 App #145

Closed
datrinh opened this issue Mar 17, 2022 · 3 comments
Closed

Not running on Vue 2 App #145

datrinh opened this issue Mar 17, 2022 · 3 comments

Comments

@datrinh
Copy link

datrinh commented Mar 17, 2022

I'm trying to set up a component library with vue-demi (Vite + Vue 3 + Setup Script). Importing thing work fine in Vue 3 apps, but fail for Vue 2. The resulting bundle looks like this:

BaseButton.vue to be imported by Vue2/3 App

<script setup lang="ts">
import { ref } from "vue-demi";

defineProps({
  label: {
    type: String,
    default: "",
  },
});

const counter = ref(0);
</script>

<template>
  <label>
    {{ label }}
    {{ counter }}
    <button v-bind="$attrs" @click="counter++">
      <slot />
    </button>
  </label>
</template>

Build output:

import { defineComponent, ref, openBlock, createElementBlock, createTextVNode, toDisplayString, createElementVNode, mergeProps, renderSlot } from "vue";
const _sfc_main = /* @__PURE__ */ defineComponent({
  props: {
    label: {
      type: String,
      default: ""
    }
  },
  setup(__props) {
    const counter = ref(0);
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("label", null, [
        createTextVNode(toDisplayString(__props.label) + " " + toDisplayString(counter.value) + " ", 1),
        createElementVNode("button", mergeProps(_ctx.$attrs, {
          onClick: _cache[0] || (_cache[0] = ($event) => counter.value++)
        }), [
          renderSlot(_ctx.$slots, "default")
        ], 16)
      ]);
    };
  }
});
export { _sfc_main as BaseButton };

In Vue 2 I run into Uncaught TypeError: Object(...) is not a function error with defineComponent(). I suppose it should be importing from "vue-demi" instead of "vue"?

Package.json

"dependencies": {
    "vue-demi": "^0.12.4"
  },
  "devDependencies": {
    "@types/node": "^17.0.21",
    "@vitejs/plugin-vue": "^2.2.0",
    "@vue/composition-api": "^1.4.9",
    "typescript": "^4.5.4",
    "vite": "^2.8.0",
    "vue": "^3.2.25",
    "vue-tsc": "^0.29.8"
  },
  "peerDependencies": {
    "@vue/composition-api": "^1.0.0-rc.1",
    "vue": "^2.0.0 || >=3.0.0"
  },
  "peerDependenciesMeta": {
    "@vue/composition-api": {
      "optional": true
    }
  },

vite.config.ts

export default defineConfig({
  plugins: [vue()],
  build: {
    lib: {
      entry: path.resolve(__dirname, "src/lib.ts"),
      name: "myLib",
      fileName: (format) => `lib.${format}.js`,
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ["vue"],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: "Vue",
        },
      },
    },
  },
  optimizeDeps: {
    exclude: ["vue-demi"],
  },
});
@wobsoriano
Copy link
Contributor

#136 (comment)

@GUGIG
Copy link

GUGIG commented Jul 4, 2022

As you mentioned, I think this is a matter of the output file importing stuffs from "vue" instead of "vue-demi".
Currently I'm thinking this could be solved by tinkering the vite.config.js(ts)file but have no idea how..

@sxzz
Copy link
Member

sxzz commented Sep 4, 2022

let's track #152

@sxzz sxzz closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants