fix(glob): wrap glob compile output in function invocation #3682
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Currently,
import.meta.glob
andimport.meta.globEager
are compiled into objects declarations{}
. This does not have the same syntax properties as a function invocation and can cause valid source input to fail at runtime. Probably the most obvious example is callingimport.meta.globEager
for side-effect code without assignment, in which case the curly brace output is inferred as a block scope and will cause the SyntaxErrorUnexpected token :
.This PR passes the output as target to Object.assign without additional source parameters. This is effectively doing nothing but outputting syntax with the same properties as the source.
Wrapping the object in only parentheses fixes the syntax error but will not insert automatic semicolons the same way as a function invocation and can also cause confusing errors at runtime.
This is better explained by example:
Additional context
I noticed this when importing old and non module based code. Guessing the problem would mostly occur in those circumstances. It's trivially fixed by assigning the ouput of
import.meta.glob
to a placeholder variable. However, the problem is confusing, with valid JS (or even TS) causing syntax errors at runtime and requires looking into compiled output to understand what's going on.I did look into actually stripping the import object from output if not used, but this requires changes outside of importGlob adding complexity and bug risk in sourcemap generation etc. Another option could be to force special syntax and throw a more descriptive error. This also looks non-trivial, so i deemed this good enough.
I have little knowledge if the change could cause issues in node/SSR contexts. A reviewers eye on this would be highly appreciated.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).