denoland / deno_emit

Transpile and bundle JavaScript and TypeScript under Deno and Deno Deploy
https://jsr.io/@deno/emit
MIT License
222 stars 23 forks source link

Multiple `const mod =` definitions? #136

Open jflatow opened 1 year ago

jflatow commented 1 year ago

I'm bundling up some files and ending up with several definitions of const mod. Sometimes its a function that returns the exports from an imported module, and other times it's the exports directly. Is this a known issue, and if so, is there any workaround?

jflatow commented 1 year ago

Ok, I believe I tracked this down to an issue with eval, which I know is evil, but still part of the language. I noticed that there can be various issues when bundling code with eval in it, but I've created a minimal example which reproduces the one I am reporting here (multiple mod definitions).

Consider three files: multi-mod.A.ts, multi-mod.B.ts, and multi-mod.importer.ts:

export class A {}
export class B {}
import * as m1 from './multi-mod.A.ts';
import * as m2 from './multi-mod.B.ts';
const { A } = m1;
const { B } = m2;
eval('');

If you bundle the importer script, you get multiple definitions of mod.

If you remove B from the picture you can see a different but related issue that Identifier 'A' has already been declared.

jflatow commented 1 year ago

As a workaround for anyone else running into this issue: you can replace eval with window.eval if the code doesn't need access to local state.

jflatow commented 1 year ago

There's a separate but related bug if you export the name mod in user-space code yourself, the emitted module gets overwritten.