Skip to content

Order of export and definition #2536

Closed

Description

@cherrydev

I don't know if this is really a bug in babel or simply a difference in how Typescript and babel transpiles ES6-style module syntax into common.

The typescript compiler happily works with this when it is targeting ES5:

fooModule.ts

export {foo};

function foo() {
console.log("foo");
}
import {foo} from "./fooModule";
foo();

Will output "foo";

Instead, when I have typescript target ES6 and THEN have babel transpile the ES6 modules to CommonJS (for Browserify) foo will be undefined because places the exports.foo = foo statement literally where the export {foo}; statement is, instead of what Typescript does, which is placing it at the bottom of the module after everything has been defined.

Would you say this Typescript compiler is contrary to the ES6 spec by 'de-hoisting' the export statements and a smart compiler would generate an error? Or is Typescript behaviour 'correct'? The ES6 spec DOES specifically say that imports are always hoisted to the top because (like var) import is a magic keyword.

Activity

  1. sebmck commented on Oct 13, 2015

    @sebmck
    Contributor

    Your example will work correctly. foo is hoisted as it's a function declaration. Do you have a more complete example/reproduction?

  2. cherrydev commented on Oct 13, 2015

    @cherrydev
    Author

    None at the moment that isn't propriety code for my work…

    I have an enum in the module that DOES export correctly, because the transpiled code ends up with an extra explicit assignment.

    Roughly, the actual structure is as follows:

    base.ts

    export { Base };
    class Base { // exports correctly
    }

    derived.ts

    import { Base } from "./base";
    
    export { MyEnum, Derived };
    
    enum MyEnum { // exports correctly
    A = 0,
    B = 1,
    C = 2
    }
    
    class Derived extends Base { // does not export correctly
    }

    The problem seems to happen only when I'm exporting a class that derives from one that's been imported AND the export statement for the derived class happens BEFORE the definition of it.

    ED: Corrected Base import statement.

  3. cherrydev commented on Oct 13, 2015

    @cherrydev
    Author

    FYI, I have not dug into the output of the Typescript compiler, but I imagine when it's outputting ES6 the above code would be unchanged, except for MyEnum being turned into an object literal which seems irrelevant to the conversation. I've considered the possibility of browserify being the culprit, but I don't think browserify actually modifies the CommonJS export code itself, just the glue between the modules. If this doesn't reproduce as above trivially, please let me know and I'll start digging deeper.
    At least I know that there's a bug in some piece of my pipeline and the export syntax I was using was perfectly valid.

  4. jamiebuilds commented on Oct 13, 2015

    @jamiebuilds
    Contributor

    Okay, so to simplify:

    The generated code for extended classes are not hoisted.

    export { Foo, Bar };
    class Foo {}
    class Bar extends Foo {}
    exports.Foo = Foo;
    exports.Bar = Bar;
    var Foo = function Foo() { ... };
    var Bar = (function (_Foo) { ... })(Foo);

    TypeScript gets around this by moving the exports.X = X below the class definitions.

    Note: This works with the optional es6.spec.blockScoping transformer.

  5. sebmck commented on Oct 13, 2015

    @sebmck
    Contributor

    Module formatter remaps aren't taking into consideration declarations that can cause a binding such as non assignment/update expressions.

  6. cherrydev commented on Oct 13, 2015

    @cherrydev
    Author

    Yes, that's exactly the form that I'm seeing in the outputted ES5! I'm glad you were able to reproduce this so quickly. So for now using the es6.spec.blockScoping transformer is a workaround?

  7. cherrydev commented on Oct 13, 2015

    @cherrydev
    Author

    When I use your link to "Try It Out" and evaluate the output in node, I get:
    Uncaught ReferenceError: Foo is not defined - temporal dead zone
    It appears that all it does is insert guards that throw an error instead of fixing it.

  8. jamiebuilds commented on Oct 13, 2015

    @jamiebuilds
    Contributor

    @cherrydev Right, this isn't working currently. The point was that the es6.spec.blockScoping transformer will do this:

    exports.Foo = Foo = function Foo() { ... }

    Sorry for not being clear

  9. added
    outdated
    on May 4, 2018
  10. 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