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

"[ERROR] Invalid define value (must be an entity name or valid JSON syntax)"... when JSON object is the value #71

Closed
busticated opened this issue Jan 17, 2024 · 6 comments

Comments

@busticated
Copy link

hi there πŸ‘‹

thanks for this great tool and apologies for the english here πŸ™

with a vite configuring like this:

define: {
    'import.meta.env.__X__': JSON.stringify({ my: 'object' )),
},

i get an error:

✘ [ERROR] Invalid define value (must be an entity name or valid JSON syntax): {"BASE_URL":"/","MODE":"development","DEV":true,"PROD":false,"__X__":{\"my\":\"object\}"}

Error: Build failed with 1 error:
error: Invalid define value (must be an entity name or valid JSON syntax): {"BASE_URL":"/","MODE":"development","DEV":true,"PROD":false,"__X__":{\"my\":\"object\}"}
    at failureErrorWithLog (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:1651:15)
    at /Users/me/code/my-app/node_modules/esbuild/lib/main.js:1059:25
    at runOnEndCallbacks (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:1486:45)
    at buildResponseToResult (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:1057:7)
    at /Users/me/code/my-app/node_modules/esbuild/lib/main.js:1086:16
    at responseCallbacks.<computed> (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:704:9)
    at handleIncomingPacket (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:764:9)
    at Socket.readFromStdout (/Users/me/code/my-app/node_modules/esbuild/lib/main.js:680:7)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:545:12) {
  errors: [Getter/Setter],
  warnings: [Getter/Setter]
}

naively, i think the issue is caused by re-writing the user define entries (here). in cases where the parsed value is an object, esbuild errors.

ideally user defines wouldn't be edited, just passed thru as they were set.

also see: #31

@busticated
Copy link
Author

if i change this line to:

-        JSON.parse(val)
+        JSON.parse(val + "INVALID-JSON");

my local setup seems to work fine πŸ€”

pengzhanbo added a commit that referenced this issue Jan 17, 2024
fix:  esbuild `define` deep stringify fail (#71)
@pengzhanbo
Copy link
Owner

I appreciate your discovering this issue and providing a fix.

However, after testing, it appears that the fix only addresses part of the problem.

Currently, the plugin's processing of define is forked from vite@4 and has not yet been updated to vite@5, leading to issues with compatibility. I have re-referenced the relevant code in vite@5 and rewritten the code in this part of the plugin.

After testing, this issue and other potential scenarios have been successfully resolved.

@busticated
Copy link
Author

After testing, this issue and other potential scenarios have been successfully resolved.

great! thank you very much πŸ™

i'll test the updated code once it's released πŸ‘

@busticated
Copy link
Author

tested - works great - thanks again πŸ™πŸ™ŒπŸ‘πŸš€

@busticated
Copy link
Author

oh. i just noticed that there's a strange log item output now:

3:45:13 PM [vite:mock] No proxy was configured, mock server will not work. See https://vite-plugin-mock-dev-server.netlify.app/guide/usage

seems to be coming from here. in my case, i'm not using websockets so maybe that's causing it? regardless, everything seems to work fine despite the log message suggesting it should not πŸ€·β€β™‚οΈ

@pengzhanbo
Copy link
Owner

yes, i see. I made a mistake.

I re-published a minor version to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants