Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.14.8 to 0.14.10 possibly introduced regression with named exports #1903

Closed
johnnybenson opened this issue Jan 3, 2022 · 3 comments
Closed

Comments

@johnnybenson
Copy link

Module A

class MyMoreSpecificClass {
    ...
}

export {
    MyMoreSpecificClass as SpecificClass,
};

Module B

import * as A from './module-a'

new A.SpecificClass();

Build / Runtime Error

Uncaught ReferenceError: MyMoreSpecificClass is not defined

The above compiles and runs in 0.14.8 but it outputs an error after upgrading to 0.14.10 that references the original class instead of the named export.

@evanw
Copy link
Owner

evanw commented Jan 7, 2022

I can't reproduce this. Here's what I tried:

$ cat module-a.js 
class MyMoreSpecificClass {
    // ...
}
export {
    MyMoreSpecificClass as SpecificClass,
};

$ cat module-b.js 
import * as A from './module-a'
new A.SpecificClass();
console.log('works');

$ esbuild --bundle module-b.js | node
works

@johnnybenson
Copy link
Author

I'm sorry, I gave you bad information. I thought I was working with a class and taking a second look, it's an Enum in Typescript.

This should reproduce what I am seeing:

// a.ts

enum MySpecificEnum {
    Value = 1,
}

export {
  MySpecificEnum as MyEnum,
}
// b.ts

import * as A from './a';

// Works
// console.log(A.MyEnum);

// Doesn't Work
console.log(A.MyEnum.Value);
./node_modules/.bin/esbuild --bundle b.ts | node
// Build output from `console.log(A.MyEnum);`

(() => {
  // a.ts
  var MySpecificEnum = /* @__PURE__ */ ((MySpecificEnum2) => {
    MySpecificEnum2[MySpecificEnum2["Value"] = 1] = "Value";
    return MySpecificEnum2;
  })(MySpecificEnum || {});

  // b.ts
  console.log(MySpecificEnum);
})();

vs.

// Build output from `console.log(A.MyEnum.Value);`

(() => {
  // b.ts
  console.log(MySpecificEnum.Value);
})();
➜  repro ./node_modules/.bin/esbuild --bundle b.ts | node
[stdin]:3
  console.log(MySpecificEnum.Value);
              ^

ReferenceError: MySpecificEnum is not defined
    at [stdin]:3:15
    at [stdin]:4:3
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:305:38)
    at node:internal/process/execution:75:19
    at [stdin]-wrapper:6:22
    at evalScript (node:internal/process/execution:74:60)
    at node:internal/main/eval_stdin:29:5
    at Socket.<anonymous> (node:internal/process/execution:206:5)
    at Socket.emit (node:events:402:35)

@evanw
Copy link
Owner

evanw commented Jan 8, 2022

Thanks. Looks like this is a bug with the new enum inlining behavior. The export {} syntax currently isn't handled correctly. You can do this as a workaround in the meantime:

export enum MyEnum {
    Value = 1,
}

@evanw evanw closed this as completed in 08c9aa9 Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants