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

import scan merges script contexts causing duplicate variables and esbuild failure #5446

Closed
7 tasks done
benmccann opened this issue Oct 27, 2021 · 3 comments · Fixed by #5464
Closed
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@benmccann
Copy link
Collaborator

benmccann commented Oct 27, 2021

Describe the bug

Vite merges scripts together from .svelte files that the Svelte compiler treats as separate scripts:

js += content + '\n'

Reproduction

npm init svelte@next my-app
cd my-app

Add to src/routes/index.svelte:

<script context="module">
let top;
</script>
<script>
export let top;
</script>

And then run npm run dev or npm run build

System Info

Vite 2.6.12

Used Package Manager

npm

Logs

html:src/routes/index.svelte:59:12: error: "top" has already been declared

Validations

@benmccann benmccann added bug p2-edge-case Bug, but has workaround or limited in scope (priority) labels Oct 27, 2021
@benmccann
Copy link
Collaborator Author

benmccann commented Oct 27, 2021

One possible idea from Dominik:

You process it once and cache the module script in memory, add an import to it with a virtual id and return content from cache when that id is requested.

Prototype of that idea here: https://github.com/benmccann/vite/tree/context-module

It allows pre-bundling to succeed and SSR to succeed. However, on the client, it results in the error message Failed to fetch dynamically imported module: http://localhost:3000/src/routes/Example.svelte

I'm wondering if this is related or a separate bug. Do the results of scan.ts affect loading the .svelte file on the client? Maybe I broke something there somehow. I'm not quite sure how to debug

If I load that URL directly, it loads valid JS. Maybe it's not the correct URL format for Vite to interpret to load though?

@bluwy
Copy link
Member

bluwy commented Oct 28, 2021

The idea looks fine to me. The error message does look off though, don't think that's related to your scanning changes, but a separate bug.

@benmccann
Copy link
Collaborator Author

Thanks for taking a look and suggesting that. I've confirmed it is an unrelated bug: sveltejs/kit#2699

@github-actions github-actions bot locked and limited conversation to collaborators Nov 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants