docs icon indicating copy to clipboard operation
docs copied to clipboard

Documenting `vite.ssr.noExternal`

Open mayank99 opened this issue 2 years ago • 2 comments

Vite externalizes SSR dependencies by default: https://vitejs.dev/guide/ssr.html#ssr-externals

A lot of npm packages are not ESM-ready so it leads to errors when using ESM imports.

This can be fixed by forcing those packages to be transpiled by vite using vite.ssr.noExternal in astro.config

vite: {
  ssr: {
    noExternal: ['package-name']
  }
}

Since it's not obvious how to arrive at this solution (and vite's docs are a bit sparse), maybe this should be added somewhere in the astro docs, e.g. the troubleshooting section?

Also there are probably other cases for using vite.ssr.noExternal that I can't think of right now.

mayank99 avatar Sep 01 '22 23:09 mayank99

Looks like we name-drop it on the styling page, but deserves its own spot somewhere as there are other use cases. It also doesn't really explain what noExternal does

Jutanium avatar Sep 02 '22 15:09 Jutanium

Docs team discussed; the remaining use case to document is when using a non-ESM package. We can note this in Troubleshooting and link from our static assets page

Jutanium avatar Sep 23 '22 14:09 Jutanium

💡 Idea

How about we link from our “NPM Packages” section in the imports guide to the config reference section about Vite? We could then beef up the config reference to add some of the helpful context from @mayank99 and add a noExternal example.

delucis avatar Nov 03 '22 15:11 delucis

@mayank99 Would you be interested in making a PR for this, by any chance?

sarah11918 avatar Dec 29 '22 11:12 sarah11918

@sarah11918 Done! https://github.com/withastro/docs/pull/2254

Frankly, I do not fully understand what Vite does and what Astro does, so it would help if someone who better understands the vite externalization sorcery can review the PR.

mayank99 avatar Dec 29 '22 15:12 mayank99

Thank you, @mayank99 🙌

we'll let some Astro wizards handle the sorcery part. 🧙‍♂️ This is so helpful!

sarah11918 avatar Dec 29 '22 18:12 sarah11918