You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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};functionfoo(){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.
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};classBase{// exports correctly}
derived.ts
import{Base}from"./base";export{MyEnum,Derived};enumMyEnum{// exports correctlyA=0,B=1,C=2}classDerivedextendsBase{// 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.
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.
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?
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.
Activity
sebmck commentedon Oct 13, 2015
Your example will work correctly.
foois hoisted as it's a function declaration. Do you have a more complete example/reproduction?cherrydev commentedon Oct 13, 2015
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
derived.ts
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
Baseimport statement.cherrydev commentedon Oct 13, 2015
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.
jamiebuilds commentedon Oct 13, 2015
Okay, so to simplify:
The generated code for extended classes are not hoisted.
TypeScript gets around this by moving the
exports.X = Xbelow the class definitions.Note: This works with the optional
es6.spec.blockScopingtransformer.sebmck commentedon Oct 13, 2015
Module formatter remaps aren't taking into consideration declarations that can cause a binding such as non assignment/update expressions.
cherrydev commentedon Oct 13, 2015
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?
cherrydev commentedon Oct 13, 2015
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 zoneIt appears that all it does is insert guards that throw an error instead of fixing it.
jamiebuilds commentedon Oct 13, 2015
@cherrydev Right, this isn't working currently. The point was that the
es6.spec.blockScopingtransformer will do this:Sorry for not being clear