Skip to content

bundler error when exporting from a svelte component module context script #7304

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

Closed
Alexandre-Fernandez opened this issue Feb 22, 2022 · 5 comments

Comments

@Alexandre-Fernandez
Copy link

Alexandre-Fernandez commented Feb 22, 2022

Describe the bug

Exports ( inside <script lang="ts" context="module"></script> cannot be imported outside of a svelte component, without a @rollup/plugin-typescript error being thrown.

It doesn't stop the build by default (on rollup, it does on webpack) and it still works in the end, however a warning will still be logged for each export on every build. This is not only annoying (100+ warnings on each save when developing) but it also make real errors harder to spot.

This issue is a follow-up to #5817, which is 2 years old.
Possible fix would be to make svelte declare each .svelte context="module" exports in a .d.ts file.
I'm not very knowledgeable on how svelte works under the hood but since we're using a bundler I'm guessing this is possible with a pre-processor.

EDIT: a way to hide these specific warnings would be a great hack if this is not going to be fixed.

Reproduction

git clone https://github.com/Alexandre-Fernandez/svelte-rollup-typescript-bug
cd svelte-rollup-typescript-bug
npm i
npm run dev
check build log

Logs

(!) Plugin typescript: @rollup/plugin-typescript TS2614: Module '"*.svelte"' has no exported member 'Product'. Did you mean to use 'import Product from "*.svelte"' instead?
src/stores/products.ts: (2:10)

2 import { Product } from "../components/Product.svelte"
           ~~~~~~~

System Info

System:
    OS: Linux 5.15 Manjaro Linux
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 24.25 GB / 31.34 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
Binaries:
    Node: 17.3.0 - /usr/bin/node
    npm: 8.4.1 - /usr/bin/npm
Browsers:
    Brave Browser: unknown
    Firefox: 97.0.1
npmPackages:
    rollup: ^2.3.4 => 2.67.3 
    svelte: ^3.0.0 => 3.46.4

Severity

annoyance

@Alexandre-Fernandez Alexandre-Fernandez changed the title rollup's build throws an error when exporting from a svelte component module context script bundler error when exporting from a svelte component module context script Feb 23, 2022
@tanhauhau
Copy link
Member

are you having some .d.ts file that has something like

declare module "*.svelte" {
  export default any
}

?

@Alexandre-Fernandez
Copy link
Author

are you having some .d.ts file that has something like

declare module "*.svelte" {
  export default any
}

?

Everything I have is in my test repository.

@dummdidumm
Copy link
Member

dummdidumm commented Apr 15, 2022

Between now and then nothing has changed, everything inside that issue is still valid (the explanation why it happens). There's just nothing we can do against that other than relaxing the module declaration in Svelte core. You can add one on your own to relax it, that should work (declare module "*.svelte";)

@tv42
Copy link

tv42 commented Aug 15, 2022

I personally wouldn't want to relax it to everything being any, my whole motive for importing *.svelte is to reuse types from it.

For now, I've moved all of that stuff out of into a separate *.ts file.

@Rich-Harris
Copy link
Member

I'm going to close this as there's really nothing we can do about it. The solution is to avoid importing named bindings from .svelte files into .js and .ts modules — in general this will mean putting those exports in .js/.ts files in the first place.

<script context="module"> should be considered an escape hatch, not a place to put a meaningful amount of logic that other parts of your app depend on.

@Rich-Harris Rich-Harris closed this as not planned Won't fix, can't repro, duplicate, stale Apr 2, 2024
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

No branches or pull requests

5 participants