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

fix: handle local and module scripts separately #5464

Merged
merged 2 commits into from
Nov 9, 2021

Conversation

benmccann
Copy link
Collaborator

Description

Treat Svelte's <script> and <script context="module"> as separate contexts during import scan

Fixes #5446

Additional context

This is a very annoying issue that causes prebundling to fail so that the server won't start if you use variables with the same name in different script contexts, which is a very common thing to do in Svelte


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 added the p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) label Oct 28, 2021
Copy link
Member

@yyx990803 yyx990803 left a comment

Choose a reason for hiding this comment

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

I realize this could technically happen in .vue files as well, when a user uses variables with the same name in both normal <script> and <script setup> (although this seems less common than in Svelte? I personally have never seen anyone do this)

So I think some level of hard-coding for this is unavoidable (before the refactor), but on a high level there would be two code paths:

  1. "Module" context script tags: <script> in vue, and <script context="moule"> in svelte.
  2. "Local" context script tags: <script setup> in vue, and <script> in svelte.

We should detect these two paths upfront and then apply the new virtual module logic to (2). This would allow us to limit the framework-specific part to the script context detection phase and make the virtual module handling framework agnostic.

@benmccann benmccann force-pushed the context-module branch 3 times, most recently from 6b8f8b5 to 1ef2e76 Compare November 8, 2021 18:23
@benmccann
Copy link
Collaborator Author

I'm not sure I understood 100% of that comment, but I did add support for .vue files as suggested

@patak-dev patak-dev changed the title fix: Svelte's script context=module should be separate script fix: handle local and module scripts separately Nov 9, 2021
@patak-dev patak-dev merged commit 0713446 into vitejs:main Nov 9, 2021
@benmccann benmccann deleted the context-module branch November 9, 2021 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import scan merges script contexts causing duplicate variables and esbuild failure
4 participants