antfu-collective / vitesse-webext

⚡️ WebExtension Vite Starter Template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncaught ReferenceError: __DEFINES__ is not defined

enigmazack opened this issue · comments

I just clone the template and run 'pnpm i' and 'pnpm dev', and load the extension to chrome, but I get this error

Uncaught ReferenceError: __DEFINES__ is not defined

for the backgound script. This error introduced from this file
https://github.com/vitejs/vite/blob/fa30f76e49de0ca8abd96c1fd866da0512ed29b9/packages/vite/src/client/env.ts
I try to use yarn and firefox, same error.

back to vite 2.4.3 works

2.4.3

Worked, a lot thanks

I'm still facing it, even with Vite 2.5.1

image

image

commented

It's because that the background script imports /@vite/client since this PR, and then Vite will pre-bundle the deps, but, pre-bundling will not replace magic constants such as __DEFINES__.

You can try this workaround:

 optimizeDeps: {
   include: ['vue', '@vueuse/core'],
-   exclude: ['vue-demi'],
+   exclude: ['vue-demi', '@vite/client', '@vite/env'],
 },

and then delete Vite's cache dir node_modules/.vite

This will be solved in vitejs/vite#4716

I have the some issue with vite v2.9.14
image
the dev env is ok but the product build is not work.
image

I met the same problem in another project. I reinstall a different verion of nodejs may cause the problem.I guess the cache does not match the nodejs I used
I solved the problem by deleting the node_modules and the package-lock.json and rebuild the program

Just wanted to contribute a bit here since I ran into the same issue and was able to finally figure it out. It turns out that it was because I had this in my index.html: <script type="module" src="/@vite/client"></script>

I had initially updated my vite config file with this solution from above:

` optimizeDeps: {
include: ['vue', '@vueuse/core'],

  • exclude: ['vue-demi'],
  • exclude: ['vue-demi', '@vite/client', '@vite/env'],
    },`

But this didn't make a difference for me for whatever reason. I decided to instead simply delete the script tag and I was able to get it all to work.

The clue for me was this post: vitejs/vite#12344 (comment)

I realized that having <script type="module" src="/@vite/client"></script> in index.html was somehow causing this script tag to be recognized: <script type="module" src="./src/main.jsx"></script>

I don't fully understand why, but I hope this is helpful for anyone else.