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

Regression: Optimized dependencies in 2.6.x cannot access import.meta #5270

Closed
7 tasks done
frandiox opened this issue Oct 12, 2021 · 4 comments · Fixed by #7644 or #8665
Closed
7 tasks done

Regression: Optimized dependencies in 2.6.x cannot access import.meta #5270

frandiox opened this issue Oct 12, 2021 · 4 comments · Fixed by #7644 or #8665
Labels
pending triage regression The issue only appears after a new release

Comments

@frandiox
Copy link
Contributor

Describe the bug

Dependencies optimized by Vite are getting a hardcoded empty object instead of import.meta since 2.6.0-beta.0. This means such dependencies cannot access import.meta.hot API or any environment variable like import.meta.env.SSR.

This is a workaround implemented in vite-ssr. However, I hit a wall when trying to workaround import.meta.hot.on(...) in Vitedge :/

Thanks!

Reproduction

https://github.com/frandiox/vite-260-meta-issue

System Info

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 152.89 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.18.0 - /usr/local/bin/node
    Yarn: 1.22.0 - /usr/local/bin/yarn
    npm: 6.14.15 - /usr/local/bin/npm
    Watchman: 2021.09.06.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 91.1.25.72
    Chrome: 94.0.4606.71
    Edge: 94.0.992.47
    Firefox: 89.0.2
    Safari: 15.0
  npmPackages:
    vite: 2.6.7 => 2.6.7

Used Package Manager

yarn

Logs

No response

Validations

@calebbergman
Copy link

calebbergman commented Oct 26, 2021

I posted this on the Vue Land Discord under [#Tools -> Vite] just yesterday. I have a feeling they're related 😅

Discord post

I have a small Vue 2 + @vue/composition-api component bundled with rollup. Within setup() is a function that attempts to access import.meta to construct a URL for an image in a local assets directory. This is all bundled as an npm package.

setup() {
  const getPath = (name: string) => new URL(name, import.meta.url).href
  return {
    getPath
  }
}

I have a Vue 2 + vite application with the above npm package installed as a dependency. When installed locally as a file: reference everything works great (debugging; I didn't try building). When I published the package and installed it normally, now import.meta is an empty object with no url property; url is undefined and new URL() explodes (also just debugging).

import.meta properly logs to the console as an object with a url property from a <script type="module"> in index.html.

Is there an easy explanation as to why import.meta would be an empty object within the bundled node module, but not from a script module defined on index.html? I can only assume vite is doing something, but ¯\(ツ)

attachment

@nrgnrg
Copy link

nrgnrg commented Nov 20, 2021

I'm also running into this, looks like the regression was introduced in 2.6.0-beta.3

@gaomeng1900
Copy link
Contributor

gaomeng1900 commented Mar 11, 2022

This is caused by the esbuild target config. import.meta is a es2020 feature but vite@2 set default target under es2019. The following will fix this issue.

module.exports = defineConfig({
	optimizeDeps: {
		esbuildOptions: {
			target: 'es2020',
		},
	},
})

Remind you that this will also happen in production mode. You will always have to set esbuild target manually.

Which is ridiculous because this usage is right in the official document but not supported by default.

I would recommend vite to upgrade its esbuild config. Even esbuild use es2020 by default. Why downgrade it?

@gaomeng1900
Copy link
Contributor

PR #7279

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pending triage regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants