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: url with non-existent assets in dynamic URLs should fallback to rawUrl (#11157) #11233

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Jevon617
Copy link
Contributor

@Jevon617 Jevon617 commented Dec 7, 2022

Description

Fix #11157.

Additional context


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.

@benmccann
Copy link
Collaborator

@Jevon617 would you be able to update this PR? It had a merge conflict which I tried to address, but the tests are failing now

@Jevon617
Copy link
Contributor Author

Certainly, I will work on addressing the issue and get back to you as soon as possible.

@benmccann
Copy link
Collaborator

@Jevon617 I just noticed that I lost part of your change while addressing the merge conflict. I fixed that and the PR is passing now. Sorry for any trouble

@Jevon617
Copy link
Contributor Author

@benmccann Got it, glad to hear it's working now!

Co-authored-by: Ben McCann <[email protected]>
Comment on lines +113 to +120
s.update(
index,
index + exp.length,
`new URL(
import.meta.glob(${JSON.stringify(pattern)},
{ eager: true, import: 'default', as: 'url' }
)[${rawUrl}] || \`${builtUrl}\`, self.location)`,
)
Copy link
Member

Choose a reason for hiding this comment

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

This is a clever way to support the old behavior. But needing to do this for all cases seems wrong to me. The generated code would not be clean. I think that #11157 may not be an issue, but we need to clarify if needed the docs to add a vite-ignore in this case. @antfu what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

To me personally, I feel the statement of return new URL(./dir/${name}.png, import.meta.url).href making Vite bundle all png files under that directory is a bit too implicit. So I also consider this is not a good solution. To me, I feel we should resolve it to the correct path like v3.1 but not maybe not bundle all the assets by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In version 3.1, the assetImportMetaUrlPlugin was not introduced in dev mode, so undefined will not appear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but in build mode, undefined will appear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a clever way to support the old behavior. But needing to do this for all cases seems wrong to me. The generated code would not be clean. I think that #11157 may not be an issue, but we need to clarify if needed the docs to add a vite-ignore in this case. @antfu what do you think?

Do you mean adding the logic to handle vite-ignore in assetImportMetaUrlPlugin?

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.

URL with import.meta.url fails with template string and nonexistent asset
4 participants