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

improve module loading interoperability for babel #3586

Merged
merged 6 commits into from
Jul 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,17 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
}
else {
if (node.flags & NodeFlags.Default) {
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also want to check that this module does not have any exported name called "__esModule" so as not to step on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write("Object.defineProperty(exports, \"__esModule\", {");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.defineProperty is NOT available in ES3.
If compile target is ES3, you may not use it.
Just exports.__esModule = true; like babel's loose mode.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to emulate this automatically, putting the define property behind a languageVersion >= ScriptTarget.ES5 and adding a write("exports.__esModule = true"); for es3 would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! applied.

writeLine();
increaseIndent();
// default value of configurable, enumerable, writable are `false`.
write("value: true");
writeLine();
decreaseIndent();
write("};");
writeLine();
}
if (languageVersion === ScriptTarget.ES3) {
write("exports[\"default\"]");
} else {
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/es5-commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [es5-commonjs.ts]

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}


//// [es5-commonjs.js]
var A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = A;
17 changes: 17 additions & 0 deletions tests/baselines/reference/es5-commonjs.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/es5-commonjs.ts ===

export default class A
>A : Symbol(A, Decl(es5-commonjs.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(B, Decl(es5-commonjs.ts, 6, 5))
{
return 42;
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/es5-commonjs.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/es5-commonjs.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/es5-system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [es5-system.ts]

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}


//// [es5-system.js]
System.register([], function(exports_1) {
var A;
return {
setters:[],
execute: function() {
A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();
exports_1("default", A);
}
}
});
17 changes: 17 additions & 0 deletions tests/baselines/reference/es5-system.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/es5-system.ts ===

export default class A
>A : Symbol(A, Decl(es5-system.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(B, Decl(es5-system.ts, 6, 5))
{
return 42;
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/es5-system.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/es5-system.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}

3 changes: 3 additions & 0 deletions tests/baselines/reference/es5-umd3.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ export default class A
};
return A;
})();
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = A;
});
3 changes: 3 additions & 0 deletions tests/baselines/reference/es5ExportDefaultClassDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var C = (function () {
C.prototype.method = function () { };
return C;
})();
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = C;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var default_1 = (function () {
default_1.prototype.method = function () { };
return default_1;
})();
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = default_1;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var C = (function () {
};
return C;
})();
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = C;
var after = new C();
var t = C;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default function f() { }

//// [es5ExportDefaultFunctionDeclaration.js]
function f() { }
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = f;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default function () { }

//// [es5ExportDefaultFunctionDeclaration2.js]
function default_1() { }
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = default_1;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var before = func();
function func() {
return func;
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = func;
var after = func();

Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/exportAndImport-es3-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export default function f2() {
define(["require", "exports"], function (require, exports) {
function f1() {
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports["default"] = f1;
});
//// [m2.js]
define(["require", "exports", "./m1"], function (require, exports, m1_1) {
function f2() {
m1_1["default"]();
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports["default"] = f2;
});
6 changes: 6 additions & 0 deletions tests/baselines/reference/exportAndImport-es3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ export default function f2() {
//// [m1.js]
function f1() {
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports["default"] = f1;
//// [m2.js]
var m1_1 = require("./m1");
function f2() {
m1_1["default"]();
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports["default"] = f2;
6 changes: 6 additions & 0 deletions tests/baselines/reference/exportAndImport-es5-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export default function f2() {
define(["require", "exports"], function (require, exports) {
function f1() {
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = f1;
});
//// [m2.js]
define(["require", "exports", "./m1"], function (require, exports, m1_1) {
function f2() {
m1_1.default();
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = f2;
});
6 changes: 6 additions & 0 deletions tests/baselines/reference/exportAndImport-es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ export default function f2() {
//// [m1.js]
function f1() {
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = f1;
//// [m2.js]
var m1_1 = require("./m1");
function f2() {
m1_1.default();
}
Object.defineProperty(exports, "__esModule", {
value: true
};
exports.default = f2;
17 changes: 17 additions & 0 deletions tests/cases/compiler/es5-commonjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @target: ES5
// @sourcemap: false
// @declaration: false
// @module: commonjs

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}
17 changes: 17 additions & 0 deletions tests/cases/compiler/es5-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @target: ES5
// @sourcemap: false
// @declaration: false
// @module: system

export default class A
{
constructor ()
{

}

public B()
{
return 42;
}
}