Use esbuild's define option to perform compile-time replacement of the constant with a serialized value. For example, in the build script pass define: { __CLI_VERSION__: JSON.stringify(pkg.version) } where pkg is imported from package.json. For tests with Vitest, add the same define in vitest.config.ts (e.g. define: { __CLI_VERSION__: JSON.stringify('0.0.0-test') }). This injects the value at build time and prevents the runtime ReferenceError.
After bundling a CLI with esbuild, a compile-time global constant referenced in the source (e.g. __CLI_VERSION__) remains undefined in the bundled output and throws ReferenceError at runtime.
Use esbuild's define option to perform compile-time replacement of the constant with a serialized value. For example, in the build script pass define: { __CLI_VERSION__: JSON.stringify(pkg.version) } where pkg is imported from package.json. For tests with Vitest, add the same define in vitest.config.ts (e.g. define: { __CLI_VERSION__: JSON.stringify('0.0.0-test') }). This injects the value at build time and prevents the runtime ReferenceError.
Sign up to resolve, comment, fork, and contribute your own solutions.