egoist / esbuild-register

Transpile JSX, TypeScript and esnext features on the fly with esbuild
MIT License
990 stars 52 forks source link

Issue with import order / circular dependencies #62

Open stephenh opened 2 years ago

stephenh commented 2 years ago

I'm trying out esbuild-register in a project that uses this pattern:

https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de

To avoid circular dependency issues with TypeScript / JavaScript classes.

I.e. if you have class Foo extends Bar, but due to import ordering Bar is undefined when class Foo runs, then you get a runtime error.

I've created a reproduction of the issue, using the code from the above article by converted to typescript, here:

https://github.com/stephenh/esbuild-register-circular

There are two scripts:

$ ./esbuild-register.sh
/home/stephen/other/esbuild-register-circular/src/Node.ts:28
class Node extends import_internal.AbstractNode {
                                   ^
TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/home/stephen/other/esbuild-register-circular/src/Node.ts:3:27)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Module._compile (/home/stephen/other/esbuild-register-circular/node_modules/esbuild-register/dist/node.js:2258:26)
    at Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Object.newLoader [as .ts] (/home/stephen/other/esbuild-register-circular/node_modules/esbuild-register/dist/node.js:2262:9)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (/home/stephen/other/esbuild-register-circular/src/internal.ts:2:15)

For now I'm able to use ts-node with it's experimental swc transpiler, and that's cool, but I've used esbuild in the past via vitejs, and wouldn't mind sticking with esbuild / esbuild-register, and hopefully just skip ts-node all together.

Thanks!