vite icon indicating copy to clipboard operation
vite copied to clipboard

fix: avoid using `import.meta.url` for relative assets if output is not ESM (fixes #9297)

Open sapphi-red opened this issue 1 year ago • 1 comments

Description

We inject import.meta.url in renderChunk hook when relative base. Rollup replaces them depending on the output module type (ESM, CJS, System, ...). But that replace happens before renderChunk hook (during resolveImportMeta hook). So if we inject import.meta.url in renderChunk these won't be replaced by Rollup.

This PR changes the injected value depending on the output module type.

fixes #9297

Additional context

Related rollup implementation: https://github.com/rollup/rollup/pull/2164 Related rollup hook: https://rollupjs.org/guide/en/#resolveimportmeta (this is called before renderChunk hook)


What is the purpose of this pull request?

  • [x] Bug fix
  • [ ] New Feature
  • [ ] Documentation update
  • [ ] Other

Before submitting the PR, please make sure you do the following

  • [x] Read the Contributing Guidelines.
  • [x] Read the Pull Request Guidelines and follow the Commit Convention.
  • [x] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [x] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • [x] Ideally, include relevant tests that fail without this PR but pass with it.

sapphi-red avatar Jul 26 '22 16:07 sapphi-red

~~Instead of this, maybe we should inject a different value for import.meta.url depending on output format.~~ done

sapphi-red avatar Jul 26 '22 17:07 sapphi-red

I think this is important enough to get in a patch, we can test with this and other related PRs with vite-ecosystem-ci before moving forward.

patak-dev avatar Aug 09 '22 16:08 patak-dev

Hey 👋! @patak-dev Planning the next release? 😄

This PR was not found in 3.0.5

image

caoxiemeihao avatar Aug 10 '22 01:08 caoxiemeihao

Yes, we merged this one after 3.0.5, we will do another patch in the next days

patak-dev avatar Aug 10 '22 05:08 patak-dev

@patak-dev is there any timeline when will this fix be released?

kishore-rajendran avatar Aug 10 '22 06:08 kishore-rajendran

Before the end of the week @kishore-rajendran

patak-dev avatar Aug 10 '22 07:08 patak-dev

thanks for the update @patak-dev 😁

kishore-rajendran avatar Aug 10 '22 07:08 kishore-rajendran