Skip to content

Prettier in Svelte 5 does not recognize <script module>, preferring the deprecated <script context='module'> #484

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

Open
hmprt opened this issue Jan 28, 2025 · 2 comments

Comments

@hmprt
Copy link

hmprt commented Jan 28, 2025

Svelte allows for components with two script tags, so long as one is a <script module>.

<script lang='ts' module>
...
<script lang='ts'>

If you do this however, prettier complains and will stop formatting documents, saying:
ParseError: A component can only have one instance-level <script> element

The solution is to use deprecated syntax to achieve the same result:

<script lang='ts' context='module'>
<script lang='ts'> 

However this produces a svelte linter warning at the top of the documents:
context="module" is deprecated, use the 'module' attribute instead
After making this change, prettier formatting starts working again.

@dummdidumm
Copy link
Member

This sounds like prettier is using a different Svelte version to format than what you have in your project. Make sure to follow these instructions in case you did install prettier or prettier-plugin-svelte into your local project.

Otherwise I need a reproduction to properly look into this.

@hmprt
Copy link
Author

hmprt commented Jan 29, 2025

Appreciate your swift response!

I've tried pegging my prettier-plugin-svelte version to 3.3.3 with no effect. It seems specifically to have issues with svelte 5 features like runes, too.

First message should be minimum viable reproduction if used in a blank .svelte file. Does that format nicely for you?

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

No branches or pull requests

2 participants