Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3628,13 +3628,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
// only allow export default at a source file level
if (modulekind === ModuleKind.CommonJS || modulekind === ModuleKind.AMD || modulekind === ModuleKind.UMD) {
if (!isEs6Module) {
if (languageVersion === ScriptTarget.ES5) {
// default value of configurable, enumerable, writable are `false`.
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
if (languageVersion === ScriptTarget.ES3) {
write("exports.__esModule = true;");
writeLine();
}
else if (languageVersion === ScriptTarget.ES3) {
write("exports.__esModule = true;");
else {
// default value of configurable, enumerable, writable are `false`.
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
writeLine();
}
}
Expand Down Expand Up @@ -5190,6 +5190,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(`exports.default = `);
emitDeclarationName(node);
write(";");
writeLine();
emitEs6ExportDefaultCompat(node);
}
}
else if (node.parent.kind !== SyntaxKind.SourceFile || (modulekind !== ModuleKind.ES6 && !(node.flags & NodeFlags.Default))) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ namespace ts {
* Serialize an object graph into a JSON string. This is intended only for use on an acyclic graph
* as the fallback implementation does not check for circular references by default.
*/
export const stringify: (value: any) => string = JSON && JSON.stringify
export const stringify: (value: any) => string = typeof JSON !== "undefined" && JSON.stringify
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this was supposed to be part of this PR (though it certainly seems correct!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you add that then in your PR?

Copy link
Member

Choose a reason for hiding this comment

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

Can I just open a new PR for it? :3

? JSON.stringify
: stringifyFallback;

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/anonymousDefaultExportsAmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ define(["require", "exports"], function (require, exports) {
class default_1 {
}
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
define(["require", "exports"], function (require, exports) {
"use strict";
function default_1() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
});
2 changes: 2 additions & 0 deletions tests/baselines/reference/anonymousDefaultExportsCommonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default function() {}
class default_1 {
}
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
//// [b.js]
"use strict";
function default_1() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
2 changes: 2 additions & 0 deletions tests/baselines/reference/anonymousDefaultExportsUmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function() {}
class default_1 {
}
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
(function (factory) {
Expand All @@ -31,5 +32,6 @@ export default function() {}
})(function (require, exports) {
"use strict";
function default_1() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ define(["require", "exports"], function (require, exports) {
decorator
], Foo);
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
Expand All @@ -46,4 +47,5 @@ define(["require", "exports"], function (require, exports) {
decorator
], default_1);
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Foo = __decorate([
decorator
], Foo);
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
//// [b.js]
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
Expand All @@ -43,3 +44,4 @@ default_1 = __decorate([
decorator
], default_1);
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
decorator
], Foo);
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
Expand All @@ -60,4 +61,5 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
decorator
], default_1);
exports.default = default_1;
Object.defineProperty(exports, "__esModule", { value: true });
});
2 changes: 2 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedAmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ define(["require", "exports"], function (require, exports) {
class Foo {
}
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
define(["require", "exports"], function (require, exports) {
"use strict";
function foo() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = foo;
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default function foo() {}
class Foo {
}
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
//// [b.js]
"use strict";
function foo() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = foo;
2 changes: 2 additions & 0 deletions tests/baselines/reference/defaultExportsGetExportedUmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function foo() {}
class Foo {
}
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
});
//// [b.js]
(function (factory) {
Expand All @@ -32,5 +33,6 @@ export default function foo() {}
})(function (require, exports) {
"use strict";
function foo() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = foo;
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var x1: number = m;
exports.a = 10;
exports.x = exports.a;
exports.m = exports.a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {};
//// [es6ImportDefaultBindingFollowedWithNamedImport_1.js]
"use strict";
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/esModuleEmitForDefaultExport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/esModuleEmitForDefaultExport.ts] ////

//// [a.ts]

class C { }
export default C;

//// [b.ts]
var x = 0;
export default x;

//// [c.ts]
function f() { }
export default f;


//// [a.js]
"use strict";
class C {
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = C;
//// [b.js]
"use strict";
var x = 0;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = x;
//// [c.js]
"use strict";
function f() { }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = f;
22 changes: 22 additions & 0 deletions tests/baselines/reference/esModuleEmitForDefaultExport.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/a.ts ===

class C { }
>C : Symbol(C, Decl(a.ts, 0, 0))

export default C;
>C : Symbol(C, Decl(a.ts, 0, 0))

=== tests/cases/compiler/b.ts ===
var x = 0;
>x : Symbol(x, Decl(b.ts, 0, 3))

export default x;
>x : Symbol(x, Decl(b.ts, 0, 3))

=== tests/cases/compiler/c.ts ===
function f() { }
>f : Symbol(f, Decl(c.ts, 0, 0))

export default f;
>f : Symbol(f, Decl(c.ts, 0, 0))

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

class C { }
>C : C

export default C;
>C : C

=== tests/cases/compiler/b.ts ===
var x = 0;
>x : number
>0 : number

export default x;
>x : number

=== tests/cases/compiler/c.ts ===
function f() { }
>f : () => void

export default f;
>f : () => void

1 change: 1 addition & 0 deletions tests/baselines/reference/exportsAndImports4-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export { a, b, c, d, e1, e2, f1, f2 };

//// [t1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = "hello";
//// [t3.js]
"use strict";
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/outFilerootDirModuleNamesAmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export default function foo() { new Foo(); }
define("b", ["require", "exports", "a"], function (require, exports, a_1) {
"use strict";
function foo() { new a_1.default(); }
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = foo;
});
define("a", ["require", "exports", "b"], function (require, exports, b_1) {
"use strict";
class Foo {
}
exports.default = Foo;
Object.defineProperty(exports, "__esModule", { value: true });
b_1.default();
});
14 changes: 14 additions & 0 deletions tests/cases/compiler/esModuleEmitForDefaultExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @target: ES6
// @module: commonjs

// @filename: a.ts
class C { }
export default C;

// @filename: b.ts
var x = 0;
export default x;

// @filename: c.ts
function f() { }
export default f;