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

vue-demi adds @vue/composition-api to bundle, even if it is declared as external #151

Closed
johannes-z opened this issue Apr 5, 2022 · 5 comments

Comments

@johannes-z
Copy link

Issue

My app breaks when adding any library that has vue-demi as a dependency, as it adds @vue/composition-api code to the final bundle, although I declared @vue/composition-api as external.

I resolve @vue/composition-api in an UMD/AMD environment automatically. Because there are two instances of @vue/composition-api present, the app breaks.

// relevant vite.config.ts

build: {
    rollupOptions: {
      external: [
        'vue',
        '@vue/composition-api'
      ],
    }
}

Repro

  • Create a vue 2 app,
  • add @vue/composition-api and @vueuse/core
  • import any function from e.g. @vueuse/core
  • declare @vue/composition-api as external
  • run npx vite build
  • parts of @vue/composition-api end up in the bundle.

Also see this repro: https://github.com/johannes-z/vue-demi-repro. See https://github.com/johannes-z/vue-demi-repro/blob/main/dist/mylib.js#L1014

@johannes-z
Copy link
Author

johannes-z commented Apr 5, 2022

Using a regex for externals revealed the following:

// vite.config.ts
      external: id => {
        if (id === 'vue') return true
        return /\@vue\/composition-api/gi.test(id)
      },

results in

define(["@vue/composition-api", "vue", "@vue/composition-api/dist/vue-composition-api.mjs"], factory)

and the following warning

No name was provided for external module '@vue/composition-api/dist/vue-composition-api.mjs' in output.globals – guessing 'VueCompositionAPI'

@antfu
Copy link
Member

antfu commented Apr 5, 2022

You need to external vue-demi as well

@johannes-z
Copy link
Author

I was able to find a temporary fix, by adding an alias:

  resolve: {
    alias: [
      { find: '@vue/composition-api/dist/vue-composition-api.mjs', replacement: '@vue/composition-api' },
    ],
  },

@renatodeleao
Copy link

renatodeleao commented Jul 11, 2022

👋 Same issue here.

I use (and maintain) a package that depends on vue-demi@^0.13.2(it's the only one for now). The package itself is bundled via microbundle, which externalizes vue-demi by default.

After installing this package on another project — which is itself bundled via webpack@5 — it starts throwing errors due to duplicated instances of @vue/composition-api.

I've workaround it using a webpack alias that points @vue/composition-api references to the same module as suggested above by @johannes-z.

// webpack.config.js
resolve: {
  alias: {
     '@vue/composition-api$': 'path/to/node_modules/@vue/composition-api/dist/vue-composition-api.esm.js' // the one you want
  }
}

EDIT: i had a similar issue not related with vue-demi, when installing vue@2.7.0 and had to alias vue as well => vuejs/vue#12626 could this isssue be related with dual package hazard reported there?

@antfu
Copy link
Member

antfu commented Jul 15, 2022

external: [
   'vue',
   '@vue/composition-api',
+  'vue-demi'
]

And you should import everything from vue-demi instead of vue nor @vue/composition-api

@antfu antfu closed this as completed Jul 15, 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

3 participants