Unable to create temporal connection with esbuild

I am trying to integration @temporalio/client into my ts node project. When I am trying to import Connection from the client lib, my app starts crashing with the following error

<app-dir>/node_modules/@temporalio/client/src/types.ts:62
export const { WorkflowService } = proto.temporal.api.workflowservice.v1;
                                                  ^
TypeError: Cannot read properties of undefined (reading 'api')

Our team uses an esbuild command also which might be the culprit here but I am not able to debug this. Attached the esbuild command below.

#!/usr/bin/env bash

set -o errexit

cd "$(dirname "$0")"

yarn install --immutable
yarn run tsc --noEmit

rm -rf dist
yarn run esbuild src/app.ts \
  --bundle \
  --minify \
  --sourcemap \
  --platform=node \
  --target="$(node --version | sed s/v/node/)" \
  --outdir=dist/bundle \
  --external:dtrace-provider

To trigger the js file we use

#!/usr/bin/env bash

set -o errexit

cd "$(dirname "$0")"
sh build.sh

set -o allexport
exec node --require source-map-support/register dist/bundle/app.js

My app is crashing even if I just write the function and not trigger it also. Can someone please help me understand what am I doing wrong here?

More info on the stacktrace after removing the minify command.

TypeError: Cannot read properties of undefined (reading 'api')
    at <workDir>/samples/test_ts_project/node_modules/@temporalio/client/src/types.ts:62:51
    at <workDir>/samples/test_ts_project/dist/bundle/app.js:1:245
    at <workDir>/samples/test_ts_project/node_modules/@temporalio/client/src/connection.ts:9:1
    at <workDir>/samples/test_ts_project/dist/bundle/app.js:1:245
    at <workDir>/samples/test_ts_project/node_modules/@temporalio/client/src/base-client.ts:4:1
    at <workDir>/samples/test_ts_project/dist/bundle/app.js:1:245
    at <workDir>/samples/test_ts_project/node_modules/@temporalio/client/src/async-completion-client.ts:10:1
    at <workDir>/samples/test_ts_project/dist/bundle/app.js:1:245
    at <workDir>/samples/test_ts_project/node_modules/@temporalio/client/src/index.ts:30:1
    at <workDir>/samples/test_ts_project/dist/bundle/app.js:1:245

For your info, that works for me if I don’t minimize the bundle.

I nailed down the problem (or at least, part of it) to the fact that some constructor names get mangled. I fixed that specific issue in this PR.

Note that we provide no guarantee regarding the inclusion of @temporalio/* packages as part of bundled and/or minimized applications. We do not test such usage at this time. We strongly recommend configuring your bundler to treat these packages as external.

1 Like

@jwatkins thanks for the help. I added the temporalio package as external and it worked! Adding --external:@temporalio/* at the end of my buiild.sh file fixed it! Really appreciate the quick resolution!

PS: Maybe the docs should be updated with this info.

Hey @jwatkins , we tried another solution which worked for us. Instead of adding temporal as external lib, we used --keep-names which also gave us the desired solution here.