-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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: avoid scan failures in .svelte and .astro files #5193
Conversation
Could this be added to vite-plugin-svelte? Maybe as an esbuild plugin? We should try to avoid adding more conditional depending on a framework. If not, maybe a regex could be added that doesn't have this false positive? (ping @dominikg) |
I agree it's not the best long-term fix to add checks for a specific template type. I'm afraid that trying to put it in I'm not sure it's really possible to catch these cases with a better regex for the most part. The only way to really do this reliably is to parse the page |
cc2e5f7
to
393ca10
Compare
In that case, I would suggest directly to implement such an option instead of patch it that way |
Is there an example of a similar option I could follow? I'm not quite sure how to make the change and have only seen checks for the file extension elsewhere |
I think a patch is the better choice here, either way an option would feel hack-ish as well. So having some form of fix before the anticipated improved dependency handling is fine IMO. Also it looks like the "additional context" is a bit "out of context" (maybe). I think it is that we had a template string of |
The reason there is a naive string check for So we are looking for a condition that tells us if we have to do that naive check or if it is safe to unconditionally inject the export. An additional option in vite config that can be set by plugins sounds better than hardcoding it, but @bluwy is right. If this is just a stop-gap measure and the option will become useless soon, patching it in place seems less invasive. The question then becomes if we want to use inclusion or exclusion const TYPES_WITH_DEFAULT_EXPORT =['.vue',/*...*/];
if(!TYPES_WITH_DEFAULT_EXPORT.some(t => path.endsWith(t)) {
...
} const TYPES_WITHOUT_DEFAULT_EXPORT =['.svelte',/*...*/];
if(TYPES_WITHOUT_DEFAULT_EXPORT.some(t => path.endsWith(t)) {
...
} cc @drwpow for .astro, it may need additional changes (there are more hardcoded checks for .svelte and .vue in this file) |
I think for now the logic can be refactored to: Only skip appending
This would technically still have a slight chance of false positive if the js code contains strings with |
7d89ef5
to
b2221bc
Compare
Updated per the suggestions. Thanks for the review! |
Co-authored-by: Bjorn Lu <34116392+bluwy@users.noreply.github.com>
Description
The dev server would crash with a hard-to-diagnose error if
export default
was contained anywhere in a.svelte
fileThe code comment explains the fix pretty well
Additional context
I hit this while working on conversion of svelte.dev to SvelteKit. Thanks to @bluwy for tracking it down!
This is pretty easy to hit in SvelteKit. The following line will do it:
process.env.NODE_ENV
gets replace with "development" causing a failure because there's a sourcemap embedded in the template the double quotes aren't escaped causing an invalid syntax.I imagine this could easily be triggered in other template types as well though perhaps not quite as often and with less severe effects. E.g. if you said:
That will probably lead to unexpected results in any templating language
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).