-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
force Vite to bundle component libraries in SSR #1148
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/sveltejs/kit-demo/3QjuiwcM3E3SaPxr96BcdmxoznMD |
Should remove the last two sentences of https://github.com/sveltejs/kit/blob/master/documentation/faq/70-packages.md |
I actually wonder whether this is the right fix. I don't think Svelte files inherently need to be bundled. E.g. |
I get 'Must use import to load ES Module' when trying to develop kit.svelte.dev unless these lines are present: https://github.com/sveltejs/sites/blob/fd2c3c200065d07268e61ca113d793d115f82b78/sites/kit.svelte.dev/svelte.config.cjs#L12-L13 ssr: {
noExternal:
process.env.NODE_ENV === 'development'
? ['@sveltejs/site-kit']
: ['@sveltejs/site-kit', ...Object.keys(pkg.dependencies)]
} With this fix that's no longer necessary. Not entirely sure how |
Hmm. Interesting. Do you get a stack trace too or any other details? |
Yes... stack trace
...but it's not necessary, there's no mystery. |
It still seems like a mystery to me, but maybe I'm missing something. Why is it trying to |
The heuristic is explained here: https://vitejs.dev/guide/ssr.html#ssr-externals. It's not using |
As for svelte-materialify, I have no idea what it's doing |
- [packages which use `exports` instead of `module.exports`](https://github.com/vitejs/vite/issues/2579). | ||
- [ESM library that imports a CJS library](https://github.com/vitejs/vite/issues/3024) | ||
- [some UMD libraries](https://github.com/vitejs/vite/issues/2679) | ||
|
||
Vite 2 is a relatively new library and over time we expect it to become easier to use non-ESM libraries with Vite. However, you might also consider asking the library author to distribute an ESM version of their package or even converting the source for the package entirely to ESM. ESM is now the standard way to write JavaScript libraries and while there are a lot of legacy packages out there the ecosystem will become much easier to work with as more libraries convert to ESM. | ||
|
||
The most common workarounds would be to try moving the package between `dependencies` and `devDependencies` or trying to `include` or `exclude` it in `optimizeDeps`. SvelteKit currently asks Vite to bundle all your `dependencies` for easier deployment to serverless environment. This means that by moving a dependency to `devDependencies` Vite is no longer asked to bundle it. This may sidestep issues Vite encounters in trying to bundle certain libraries. Avoiding Vite bundling especially works for `adapter-node` and `adapter-static` where the bundling isn't necessary since you're not running in a serverless environment. We are considering [better alternatives](https://github.com/sveltejs/kit/issues/1016) to make this setup easier. You should also add any Svelte components to `ssr.noExternal`. [We hope to do this automatically in the future](https://github.com/sveltejs/kit/issues/904). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add back this description except for the part after "You should also add...". It's pretty confusing about what goes where and why and I think the extra explanation as to what's going on is helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's out of date (though this did remind me to remove the vite.ssr
block from svelte.config.cjs, so thanks). We don't ask Vite to bundle dependencies for serverless deployment; it bundles the stuff it thinks need to be bundled (based on its heuristic, the heuristic implemented in this PR, and the user's noExternal
config), and adapters bundle everything else if they deem it necessary.
Technically speaking it was never correct; SvelteKit isn't asking for special treatment of dependencies
, the project template was. Now that the vite.ssr
config is removed from the template, swapping stuff between dependencies
and devDependencies
will make no difference, and nor should it — that was always a weird hack we embraced out of necessity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right that it was a bit imprecise about SvelteKit vs template
I didn't realize vite.ssr
could be removed from the templates. I thought that was still needed as a trick @GrygrFlzr had introduced for bundling for deployment to serverless environments. I didn't think it could be removed without updating the adapters as described in #1016. If I'm wrong, maybe that issue can be closed now
Anyway, I'm happy it's gone. I just hope we didn't make it hard to use the adapters
Ok, yeah, This looks good to me. I just left a few minor comments. Thanks for this!! Glad to have a bit of Vite complication handled automatically. |
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
fixes #904, by scanning all the
package.json
files of dependencies/devDependencies and adding any that have a"svelte"
field tossr.noExternal
. The user'snoExternal
is still respected, so it's not a problem if this heuristic fails — it should just make these sorts of errors rarer.This will fail in cases where a new component library is added a dependency while the dev server is running; in those cases turning it off and on again will fix it.
Finicky to add a test for this, but I verified it works with a site that uses external component libraries.
Before submitting the PR, please make sure you do the following
Tests
pnpm test
and lint the project withpnpm lint
Changesets
pnpx changeset
and following the prompts