Typescript: Conditionalize `__esModule` emit?

Created on 16 Aug 2019  路  3Comments  路  Source: microsoft/TypeScript

Numerous issues pointed to from https://github.com/microsoft/TypeScript/pull/32083 have people running in environments where it's not safe to emit the __esModule property for whatever reason.

Instead of adding a flag, we could just emit (typeof exports !== 'undefined') && Object.defineProperty(...

Thoughts?

Needs Investigation

Most helpful comment

one use case for this is, if you use Typescript to generate mongodb atlas stitch code, it won't deploy, unless you manually remove the Object.defineProperty(exports, "__esModule", { value: true }); at the top.
the reason is simple, it expects always one exports = () => {} per file at the top, additional functions on the same file may come at the bottom of the function. so, that conditional check won't help in this case, you should be able to completely suppress that helper function (someone mentioned Closure Compiler, the scenario is alike)

All 3 comments

one use case for this is, if you use Typescript to generate mongodb atlas stitch code, it won't deploy, unless you manually remove the Object.defineProperty(exports, "__esModule", { value: true }); at the top.
the reason is simple, it expects always one exports = () => {} per file at the top, additional functions on the same file may come at the bottom of the function. so, that conditional check won't help in this case, you should be able to completely suppress that helper function (someone mentioned Closure Compiler, the scenario is alike)

Any updates? Almost one year since this issue was created.

Hi, I also ran into the same problem...

Was this page helpful?
0 / 5 - 0 ratings