from @1000hz : the parser webpack 4 uses (acorn) doesn't support optional chaining / nullish coalescing operators. wranglerjs uses its own version of webpack (4.x). TypeScript target ES2020/ESNext doesn't transpile these operators
webpack 5 (which has been in beta forever) uses a newer version of acorn with support for these operators
but there's no way to override what version of webpack wrangler will use
I think the long-term solution to this problem is to have wrangler defer building to project scripts, i.e.
if there's a package.json file
npm run wrangler:build
npm run build # if above doesn't exist
or if there's a cargo.toml file
cargo run wrangler:build
cargo build # if above doesn't exist
and only fallback to using a bundled version of webpack if none of these exist.
Supporting arbitrary scripts seems like a really good idea actually, and would allow people to choose their own JS bundling tools (e.g. rollup, etc).
Most helpful comment
I think the long-term solution to this problem is to have wrangler defer building to project scripts, i.e.
if there's a package.json file
or if there's a cargo.toml file
and only fallback to using a bundled version of webpack if none of these exist.