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(glob): wrap glob compile output in function invocation #3682

Merged
merged 3 commits into from
May 10, 2022

Conversation

TobiasMelen
Copy link
Contributor

@TobiasMelen TobiasMelen commented Jun 5, 2021

Description

Currently, import.meta.glob and import.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 calling import.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 SyntaxError Unexpected 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:

// Input source
(() => { throw Error("Never invoke") })
import.meta.globEager("./some-dir/**")

//❌ Current output, SyntaxError
(() => { throw Error("Never invoke") })
{ "/some-dir/some-file.js": __glob_0_0 }

//❌ Parentheses wrapping output, will not do correct ASI and invoke function at previous line
(() => { throw Error("Never invoke") })
({ "/some-dir/some-file.js": __glob_0_0 })

//✅ Object.assign wrapping output, will keep syntax behaviour
(() => { throw Error("Never invoke") })
Object.assign({ "/some-dir/some-file.js": __glob_0_0 })

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?

  • 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.

@patak-dev
Copy link
Member

Thanks for the PR @TobiasMelen, sorry it took us so long to check it out.
@antfu @poyoho I think this is an interesting idea. One side effect is that the keys are mixed during build, but as the comment in the test states, this shouldn't be an issue. I had to add another isBuild branch to make it work.

@patak-dev patak-dev requested a review from antfu May 10, 2022 19:35
@antfu antfu changed the title fix(vite/importGlob): Wrap glob compile output in function invocation fix(glob): wrap glob compile output in function invocation May 10, 2022
@antfu antfu merged commit bb603d3 into vitejs:main May 10, 2022
@negezor
Copy link

negezor commented Jun 23, 2024

This is of course a settled issue, but I'd like to put my two cents in. Perhaps import side effects would be better wrapped as const _ = import.meta.glob()? Instead of wrapping the whole import in Object.assign()?

@patak-dev
Copy link
Member

@negezor probably the best here is for you to do a PR to show the benefits of the alternative you propose. A comment in a closed PR may get lost quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants