esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Invalid define value

Open kongyijilafumi opened this issue 1 year ago • 1 comments
trafficstars

error: Invalid define value (must be an entity name or valid JSON syntax): 'wss://'+document.domain+'/wss' when I use string value define ,The program will not report errors. but I use the expression define,The program may encounter exceptions. so, I want to use the expression define,what should I do? image image

kongyijilafumi avatar Jan 17 '24 10:01 kongyijilafumi

The define option can only replace JSON literals and a few a.b like variables. However, you can use the inject option to provide variables in a more expressive way. For example:

// create a file named shims.js
export const __WS_URL__ = 'wss://'+document.domain+'/wss'

// then inject it
esbuild.build({
  ...options,
  inject: ['./shims.js']
})

see it live

hyrious avatar Jan 17 '24 10:01 hyrious

I'm closing this issue because esbuild is working as intended here. I agree that inject is likely the best fit for your use case. The difference is that define inlines compile-time constants while inject references (but does not inline) variables from arbitrary code. They are used for different purposes.

evanw avatar Jan 17 '24 22:01 evanw

I got it.Thanks for your answer @evanw @hyrious

kongyijilafumi avatar Jan 18 '24 09:01 kongyijilafumi