davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
301 stars 22 forks source link

Uncaught SyntaxError: Unexpected string #47

Closed BenjaminLesne closed 9 months ago

BenjaminLesne commented 1 year ago

I have a react app with the lib mode activated in my vite config. I use import.meta.env.mode then build the app. When I use the library in an other app I get : Uncaught SyntaxError: Unexpected string

reproducible example repo

steps to reproduce: 1- install dependencies

pnpm install

2- build the library :

pnpm build

3- In main.jsx, uncomment line 4 and comment line 3

4- start the dev server

pnpm dev

5- check the console in Chrome Uncaught SyntaxError: Unexpected string and the webpage shows nothing

Expected behaviour : The web page displays "yay production" in prod and "not production" in dev mode.

I reproduced it with a Firefox and Chrome.

davidmyersdev commented 11 months ago

@BenjaminLesne it looks like this is related to #41. The error is happening because the built library has an object called something like ie.process.env.MODE, and that object is then partially replaced by the plugin as something like ie."development" which is invalid. I still need to come up with a solution, but I have a better idea of the cause now.

davidmyersdev commented 11 months ago

I think this static replacement might be the cause. https://vitejs.dev/guide/env-and-mode.html#production-replacement

BenjaminLesne commented 11 months ago

@davidmyersdev yeh I saw the Production Replacement thing from vite then I found this

It looks like this related to rollup (see this answer)