Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(scanner): skip svelte components with #snippet #20562

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

Draft
Copilot wants to merge 8 commits into main
base: main
Choose a base branch
Loading
from copilot/fix-30fb8ddb-e23f-4a2c-ba29-02144fbd2479

Conversation

Copy link
Contributor

Copilot AI commented Aug 7, 2025
edited
Loading

Fixes #20511

When a Svelte file exports snippets through its <script module> block, Vite's dependency scanner fails with an esbuild error:

✘ [ERROR] "foo" is not declared in this file
 script:/path/to/Component.svelte?id=0:2:12:
 2 │ export { foo };
 ╵ ~~~

This happens because snippets are defined in the template using {#snippet foo()} syntax, but the scanner only sees the script block where export { foo } references an undeclared identifier.

Solution

This PR adds special handling for Svelte files containing #snippet:

  1. Detection: When scanning Svelte files with <script module> blocks, check if the file content contains #snippet
  2. Virtual Module Export: For files with snippets, add export * from 'svelte-snippet-dummy:filename' to handle snippet exports that aren't defined in the script block
  3. Externalization: Add an esbuild onResolve handler to externalize any modules starting with svelte-snippet-dummy

Example

Before this fix, this Svelte file would cause scanning to fail:

<script module>
 export { mySnippet };
</script>
{#snippet mySnippet()}
 <p>Hello from snippet!</p>
{/snippet}

After the fix, the scanner generates:

export * from 'svelte-snippet-dummy:/path/to/Component.svelte'
import "virtual:Component.svelte?id=0"
export default {}

The virtual module is externalized, preventing esbuild from trying to resolve snippet exports that don't exist in the script block.

Testing

  • Added integration test with actual Svelte file containing snippets
  • Test verifies that scanning completes without errors for Svelte files with #snippet syntax
  • All existing tests continue to pass
  • Manual verification confirms the fix resolves the scanning error

This change is backward compatible and only affects Svelte files that contain #snippet syntax.


💬 Share your feedback on Copilot coding agent for the chance to win a 200ドル gift card! Click here to start the survey.

- Detect #snippet in Svelte files and add virtual module export
- Externalize virtual-module-named-svelte-dummy modules
- Add tests for Svelte snippet handling
- Fixes issue #20511 where Svelte snippet exports caused scan failures
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Copilot AI changed the title (削除) [WIP] To fix @vitejs/vite/issues/20511 , add export * from 'virtual-module-named-svelte-dummy:filename' if the svelte file has #snippet string. externalize modules that starts with virtual-module-named-svelte-dummy-. (削除ここまで) (追記) fix(deps): handle Svelte #snippet exports in dependency scanner (追記ここまで) Aug 7, 2025
Copilot AI requested a review from sapphi-red August 7, 2025 14:22
Copilot finished work on behalf of sapphi-red August 7, 2025 14:22
@sapphi-red sapphi-red changed the title (削除) fix(deps): handle Svelte #snippet exports in dependency scanner (削除ここまで) (追記) fix(scanner): skip svelte components with #snippet (追記ここまで) Aug 8, 2025
@sapphi-red sapphi-red added has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) feat: deps optimizer Esbuild Dependencies Optimization labels Aug 8, 2025
Copy link
Member

@copilot update the PR description

Copy link
Contributor Author

Copilot AI commented Aug 8, 2025

@copilot update the PR description

Updated the PR description to match the actual implementation. The main change was correcting virtual-module-named-svelte-dummy to svelte-snippet-dummy to reflect what was actually implemented in the code.

Copilot finished work on behalf of sapphi-red August 8, 2025 02:26
@sapphi-red sapphi-red marked this pull request as ready for review August 8, 2025 02:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a Vite dependency scanner error that occurs when Svelte files export snippets through their <script module> block. The scanner fails because snippets are defined in the template using {#snippet foo()} syntax, but the scanner only sees the script block where export { foo } references an undeclared identifier.

  • Added special handling for Svelte files containing #snippet syntax to prevent scanning errors
  • Introduced a virtual module export mechanism for snippet exports that aren't defined in the script block
  • Added an esbuild onResolve handler to externalize svelte-snippet-dummy modules

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/vite/src/node/optimizer/scan.ts Adds detection for #snippet syntax and virtual module handling to prevent scanner errors
packages/vite/src/node/tests/scan.spec.ts Adds integration test for Svelte files with snippets and refactors existing test helpers
packages/vite/src/node/tests/fixtures/scan-svelte/svelte-snippet.svelte Test fixture demonstrating a Svelte file with snippet exports

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Member

/ecosystem-ci run

github-actions[bot] reacted with thumbs up emoji

Copy link

pkg-pr-new bot commented Sep 22, 2025
edited
Loading

Open in StackBlitz

npm i https://pkg.pr.new/vite@20562

commit: c98504f

Copy link

Copy link
Member

/ecosystem-ci run

github-actions[bot] reacted with thumbs up emoji

Copy link

@sapphi-red sapphi-red marked this pull request as draft September 22, 2025 05:30
Copy link
Member

/ecosystem-ci run

github-actions[bot] reacted with thumbs up emoji

Copy link

📝 Ran ecosystem CI on 1a8ee07: Open

suite result latest scheduled
histoire success failure
analogjs failure failure
qwik success failure
laravel failure failure
storybook success failure
vite-environment-examples failure failure
react-router success ⏹️ cancelled
vite-plugin-rsc failure failure
vitest failure failure
vite-plugin-cloudflare failure failure

ladle, marko, one, nuxt, astro, rakkas, vite-plugin-pwa, unocss, vite-plugin-svelte, quasar, vite-plugin-vue, vike, waku, sveltekit, vuepress, vitepress, vite-plugin-react, vite-setup-catalogue

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

Reviewers

Copilot code review Copilot Copilot left review comments

+1 more reviewer

@sapphi-red sapphi-red sapphi-red approved these changes

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Labels

feat: deps optimizer Esbuild Dependencies Optimization has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) trigger: preview

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

vite scanner trips on exported svelte snippets

2 participants

AltStyle によって変換されたページ (->オリジナル) /