vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.2k stars 6.04k forks source link

Vue is externalized no matter the `ssr.noExternal` value #9117

Closed paraboul closed 2 years ago

paraboul commented 2 years ago

Describe the bug

I'm migrating a project from vite 2 to 3, and just facing a little issue with SSR externals.

It vite 2, I was just using something like noExternal: /./ to make sure nothing is externalised, which worked well.

In Vite 3, it seems that vue and @vue/server-renderer are not externalised (the generated output imports vue methods from the vue package). All others deps are bundled into the mjs output.

I tried to set ssr.noExternal to true, but then I got various error like Cannot bundle Node.js built-in "http" imported from "node_modules/axios/lib/adapters/http.js". Consider disabling ssr.noExternal or remove the built-in dependency

I also tried to manually exclude 'vue' package with something like noExternal: [/./, 'vue'] with no luck

Another weird thing is that the import output looks broken too. I tried to add vue as a deps as a workaround but got this

import require$$0$1, { defineComponent, onBeforeUnmount, watchEffect, ref as ref$1, inject, reactive as reactive$1, computed as computed$1, markRaw, mergeProps, unref, createVNode, resolveDynamicComponent, useSSRContext, openBlock, createElementBlock, createElementVNode, watch, onActivated, onMounted, shallowRef, resolveComponent, withCtx, createBlock, toDisplayString, createTextVNode, createCommentVNode, getCurrentInstance, h as h$4, isRef as isRef$1, readonly as readonly$1, onBeforeUpdate, onErrorCaptured, Suspense, nextTick, createSSRApp, createApp as createApp$1, createStaticVNode, onUnmounted, toRef, Fragment, renderList, withModifiers, renderSlot, defineAsyncComponent, useSlots, withDirectives, vModelText, vShow, provide, withAsyncContext, resolveDirective, cloneVNode, Teleport, toRaw as toRaw$1, Transition, triggerRef, TransitionGroup, effectScope } from "vue";
       ^^^^^^^^^^^^
SyntaxError: The requested module 'vue' does not provide an export named 'default'

Reproduction

https://stackblitz.com/edit/vitejs-vite-7k3ode?file=dist/server/entry-server.js

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 647.03 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.5.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.12.1 - /usr/local/bin/npm

Used Package Manager

yarn

Logs

No response

Validations

ydcjeff commented 2 years ago

It worked with extra plugin mutating config.ssr.external tho. repro link

paraboul commented 2 years ago

It worked with extra plugin mutating config.ssr.external tho. repro link

Indeed, thanks 👍. This feels a bit hackish though. Is there any particular reason that Vue won't be externalised despite the ssr.noExternal rule?

ydcjeff commented 2 years ago

I think Vite checks external first then noExternal, not sure how this should be properly handled?

https://github.com/vitejs/vite/blob/d12d469dfb8d7466615b085aac0d461ff18a4465/packages/vite/src/node/ssr/ssrExternal.ts#L123-L130

sapphi-red commented 2 years ago

I think this part is affecting this. Since vite 3 externalizes everything, maybe this should be removed. https://github.com/vitejs/vite/blob/a2b313126abdf2e0652502cbcd4b94353c37f91a/packages/plugin-vue/src/index.ts#L126-L128