Skip to content

Hoist initial assignment to exported names in cjs to they are not blocked by bindings made by __exportStar - #37093

Merged
Wesley Wigham (weswigham) merged 4 commits into
microsoft:masterfrom
weswigham:exportstar-skip-locals
Feb 28, 2020
Merged

Hoist initial assignment to exported names in cjs to they are not blocked by bindings made by __exportStar#37093
Wesley Wigham (weswigham) merged 4 commits into
microsoft:masterfrom
weswigham:exportstar-skip-locals

Conversation

@weswigham

@weswigham Wesley Wigham (weswigham) commented Feb 27, 2020

Copy link
Copy Markdown
Member

We need this because now that __createBinding is used by __exportStar to create live-updating getters, if you had input like

export * from "mod";
export const nameFromMod = 0;

we'd output

__exportStar(exports, require("mod"));
exports.nameFromMod = 0;

the assignment will throw because the __exportStar made a getter which can't be overridden with a simple assignment. By emitting

exports.nameFromMod = void 0;
__exportStar(exports, require("mod"));
exports.nameFromMod = 0;

we prevent __exportStar from overwriting nameFromMod with a getter (so the assignment succeeds).

This also neatly ensures all export names are available to reexport when resolving within a circular module graph.

@rbuckton Ron Buckton (rbuckton) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

May need to discuss in design meeting.

@YowaiCoder

Copy link
Copy Markdown

Missed the case of exported namespace (export * as foo from 'foo').

@ericjeney