Skip to content

Export objects or classes before their declaration makes them undefined (T6919) #3947

Closed

Description

@babel-bot

Issue originally made by @mqklin

Bug information

  • Babel version: 6.3.26
  • Node version: 4.0.0
  • npm version: 2.14.2

Description

I try to repeat example from Mozilla Hacks (Export lists subtitle):

//export.js
export {detectCats, Kittydar};
function detectCats() {}
class Kittydar {}

//import.js
import {detectCats, Kittydar} from "./export.js";
console.log(detectCats); // function detectCats() {}
console.log(Kittydar); // undefined

Oops: Kittydar is undefined (btw., the problem is the same with simple Object).

But if I put export after Kittydar declaration it's ok:

//export.js
class Kittydar {}
export {Kittydar};

//import.js
import {Kittydar} from "./export.js";
console.log(Kittydar); // function Kittydar() {_classCallCheck(this, Kittydar);}

Activity

  1. babel-bot commented on Jan 5, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @mqklin

    Seems like it's not a bug, but a typo in mozilla.hacks article, isn't it? Because objects and classes are not hoisted.

  2. babel-bot commented on Jan 5, 2016

    @babel-bot
    CollaboratorAuthor

    Comment originally made by @jmm

    Hello @mqklin. I think the examples are actually correct. class and let are not hoisted in the sense you mean, but I believe it's fair game to reference them in the export declaration like that, and live bindings are exported. So with real module support I think those examples should work. Perhaps the transform should be re-assigning to the export wherever the local identifier is re-assigned, e.g.:

    var obj = exports.obj = {};
    
  3. added
    outdated
    on May 4, 2018
  4. locked as resolved and limited conversation to collaborators on May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions