ESM and CJS don't play well together
Closed this issue · 0 comments
mscharley commented
Describe the bug.
As an example, if you're writing an ESM application but import a CJS library which exports classes which are decorated with DOT annotations then that CJS library will import the CJS version of the DOT annotations. This is completely compatible and mostly works fine, but the WeakMap
s used to store metadata about annotated classes are module-local variables which at runtime are seperate modules in the ESM and CJS builds.
Steps to reproduce
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const dotCjs = require('../dot.cjs');
const dotEsm = await import('../dot.js');
@dotCjs.injectable()
class A {}
@dotEsm.injectable()
class B {}
const container = dotEsm.createContainer();
container.bind(A).toSelf();
container.bind(B).toSelf();
container.validate(); // Fails, can't find @injectable() annotation for A.
Expected behaviour
No response
DOT
1.4.8
TypeScript
5.2.2
Decorator type
Typescript Experimental
Node
18
Add any other context about the problem here.
No response