Skip to content
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

default export from svelte-kit package #1856

Closed
janosh opened this issue Jul 8, 2021 · 2 comments · Fixed by #1735
Closed

default export from svelte-kit package #1856

janosh opened this issue Jul 8, 2021 · 2 comments · Fixed by #1735

Comments

@janosh
Copy link
Contributor

janosh commented Jul 8, 2021

How do I create a default export in a svelte-kit package? Would be cool if the docs included some guidance on that. If I add an index file that re-exports a Svelte component as default,

// src/lib/index.js
export { default } from './MyComponent.svelte'

and then install the package and try

import MyComponent from 'my-package'

I get

Must use import to load ES Module: my-package/index.js
require() of ES modules is not supported.
require() of my-package/index.js from repo/node_modules/vite/dist/node/chunks/dep-0ed4fbc0.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from my-package/package.json.

The easiest solution to this in the past was to use the "svelte" field in package.json but sveltekit package does not copy that field into its output.

I asked this question here as well.

@ignatiusmb
Copy link
Member

There's nothing wrong with your export code, it's still a vite problem that can't properly handle it, #1148 patches it by forcing packages with "svelte" to be bundled in ssr.noExternal. Since library published with svelte-kit package have a predefined package keys, there's no other way to fix it (currently) other than manually adding "svelte": "index.js" after packaging, or your library name to ssr.noExternal in the svelte config file.

#1735 would solve this as it will preserve the original package.json as is, so you can write "svelte" key yourself and point it to any file you want.

@janosh
Copy link
Contributor Author

janosh commented Jul 21, 2021

@ignatiusmb Thanks, that was helpful! Which vite issue is the one that tracks correctly handling (Svelte?) exports?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants