Chart.js does not work with Vite / SvelteKit out of the box. This is because Chart.js provides a build via the main field in package.json which provides just a default export. Meanwhile it provides a build in module which provides named exports. Vite tries to use the module build on the client and the main build on the server which causes the build to fail because you can't write code that is compatible with both these packages. To work around this, you can specify the option vite.ssr.noExternal: ['chart.js'], but this is hard for users to figure out and it'd be much nicer to provide builds that are the same shapte
There are a couple things we could do:
- We should put a build in
main that uses the same entry file as our ESM build and add "type": "module" to package.json. This is probably the best option
- We could remove
main. Chart.js isn't really that useful on the server-side, so we could just stop telling Vite that we have a server-side build. It would still try to build it on the server, but would fallback to the ESM implementation
Chart.js does not work with Vite / SvelteKit out of the box. This is because Chart.js provides a build via the
mainfield inpackage.jsonwhich provides just a default export. Meanwhile it provides a build inmodulewhich provides named exports. Vite tries to use themodulebuild on the client and themainbuild on the server which causes the build to fail because you can't write code that is compatible with both these packages. To work around this, you can specify the optionvite.ssr.noExternal: ['chart.js'], but this is hard for users to figure out and it'd be much nicer to provide builds that are the same shapteThere are a couple things we could do:
mainthat uses the same entry file as our ESM build and add"type": "module"topackage.json. This is probably the best optionmain. Chart.js isn't really that useful on the server-side, so we could just stop telling Vite that we have a server-side build. It would still try to build it on the server, but would fallback to the ESM implementation