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

[vite] warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module #8644

Closed
7 tasks done
nareshbhatia opened this issue Jun 17, 2022 · 6 comments · Fixed by #8674
Closed
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@nareshbhatia
Copy link

Describe the bug

Running a React app with Vite has started producing the above error since version change of esmbuild from v0.14.44 to v0.14.45. This error occurs wherever the React app is using an arrow function. My reproduction below has created a fresh Vite app and only changed the App component from a function to a function expression using an arrow function and this error starts appearing.

[vite] hmr update /src/App.tsx
[vite] warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
23 |          lineNumber: 11,
24 |          columnNumber: 9
25 |        }, this), /*#__PURE__*/_jsxDEV("p", {
   |           ^
26 |          children: "Hello Vite + React!"
27 |        }, void 0, false, {

  Plugin: vite:esbuild
  File: /Users/naresh/projects/vite-esmbuild-issue/src/App.tsx

The issue has been discussed in the esmbuild repository here and closed: evanw/esbuild#2328. They need to know exactly what flags vite is using to invoke esmbuild.

Reproduction

https://github.com/nareshbhatia/vite-esmbuild-issue

System Info

System:
    OS: macOS 12.3.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
    Memory: 103.36 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
    Watchman: 2022.03.21.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 96.1.32.115
    Chrome: 102.0.5005.115
    Safari: 15.4
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.2
    vite: ^2.9.9 => 2.9.12

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

This is happening because plugin-react uses @babel/plugin-transform-react-jsx to transform JSX.

For a workaround, you could set esbuild.logOverride to omit the warning.

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  esbuild: {
    logOverride: { 'this-is-undefined-in-esm': 'silent' }
  }
})

@LemmyMwaura
Copy link

LemmyMwaura commented Jun 18, 2022

Yup, That worked 👍 @sapphi-red

@LemmyMwaura
Copy link

LemmyMwaura commented Jun 18, 2022

So... The solution above still doesn't work. Choosing to ignore it at the moment.

More Context: When the app rebuilds and an error is encountered, the warnings re-appear.

@Maxhaufe17
Copy link

Thanks

@ayushpaharia
Copy link

Worked for me. Thanks!

@YasirHasn9
Copy link

YasirHasn9 commented Jun 21, 2022

My coworker comes up with this work around and it works. He adds this workaround and put them along side the rest of the configuration.

export default defineConfig({
  plugins: [
    process.env.MODE !== 'production'
      ? react({
          jsxRuntime: 'classic',
        })
      : react(),
    WindiCSS(),
  ],

  ... other properties
});

sawadyecma added a commit to sawadyecma/img-conv that referenced this issue Jun 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 6, 2022
cuppachino added a commit to cuppachino/overwhelm that referenced this issue Sep 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
6 participants