router icon indicating copy to clipboard operation
router copied to clipboard

The exports file is incorrect when using vite3 SSR with esm and noExternal

Open meteorlxy opened this issue 3 years ago • 5 comments

Version

4.1.3

Reproduction link

stackblitz.com

Steps to reproduce

When setting noExternal: ['vue-router'], vite will include vue-router in the SSR bundle, too.

According to current 'exports' config, it will bundle the './dist/vue-router.prod.cjs', which will cause the error.

https://github.com/vuejs/router/blob/df836529db63360f0bf9810ccd4e8911846aef45/packages/router/package.json#L13-L17

What is expected?

Run without error

What is actually happening?

file:///home/projects/vitejs-vite-ov33pc/dist/main.js:1 import require$$0, { createSSRApp } from "vue"; ^^^^^^^^^^ SyntaxError: The requested module 'vue' does not provide an export named 'default'

meteorlxy avatar Aug 14 '22 18:08 meteorlxy

I think we could remove the production line but I'm not sure if this would affect Nuxt @danielroe

Do we need a production version of the mjs?

posva avatar Aug 16 '22 07:08 posva

We might need one. Users might use vue-router in SSR production env with node esm?

meteorlxy avatar Aug 16 '22 07:08 meteorlxy

Absolutely, ideally we would have a production ESM version for externalising vue-router. I would imagine that the current mjs build is not suitable for production use because perhaps it imports devtools, etc?

danielroe avatar Aug 16 '22 08:08 danielroe

I don't think I can just remove the devtools import because one could still want to have it in production and in node builds (like other renderers). In fact, I think it was a mistake to add the global.__VUE_PROD_DEVTOOLS__ = false to the top of the node version and it will be removed in the future. Similarly, the prod version of CJS removed the devtools but I think we will add it back in the future.

I don't think importing vue-devtools in node should error because vue renderers could exist in node. I think we should keep the behavior of being able to strip off devtools during minification though. Without minification, the imports/require of @vue/devtools-api won't be removed (even though @vue/devtools-api has a sideEffects: false.

posva avatar Aug 16 '22 09:08 posva

I'm still unsure if we need to remove the devtools import/require for node. Ideally, it should be stripped off during minification instead but still allow for it to be included with the build flag mentioned above

posva avatar Aug 16 '22 10:08 posva

@meteorlxy I published a version, let me know if it's all good

posva avatar Aug 22 '22 13:08 posva

@posva It works well now. I have removed the workaround alias :+1:

meteorlxy avatar Aug 22 '22 13:08 meteorlxy