-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosThis issue has compiler-backed repros: https://aka.ms/ts-reprosRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
TypeScript Version: 4.2.0-beta
Search Terms:
- hoisted function
- exports hoisted function
- export hoisted function
- export hoist function
Code
// @module: CommonJS
// @showEmit
// @showEmittedFile: b.js
// @filename: a.ts
import { b } from "./b.js";
export function a() {
return b();
}
// @filename: b.ts
import { a } from "./a.js";
// Crashes when transpiled by TypeScript:
a();
export function b() {
return 123;
}
Expected behavior:
The exports.*
assignment should be done immediately after __esModule
, just like how Babel does it.
// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = a;
const b_1 = require("./b.js");
function a() {
return b_1.b();
}
// b.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = b;
const a_js_1 = require("./a.js");
// Crashes when transpiled by TypeScript:
a_js_1.a();
function b() {
return 123;
}
Actual behavior:
The exports assignment is performed after the function declaration, which causes behaviour differences between native ES modules (where hoisted function declarations are initialised before any code begins executing) and transpiled CommonJS modules (where hoisted function declarations are added to the exports
object after require(…)
calls)
// a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
const b_1 = require("./b.js");
function a() {
return b_1.b();
}
exports.a = a;
// b.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
const a_js_1 = require("./a.js");
// Crashes when transpiled by TypeScript:
a_js_1.a();
function b() {
return 123;
}
exports.b = b;
Related Issues: #37093
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosThis issue has compiler-backed repros: https://aka.ms/ts-reprosRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone