Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 7, 2025

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 <[email protected]>
@Copilot 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 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
@sapphi-red
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: deps optimizer Esbuild Dependencies Optimization has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vite scanner trips on exported svelte snippets
2 participants