microsoft / microsoft/TypeScript

Proposal: `export as namespace` for UMD module output

Open
#26,532 4 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Current problem

TypeScript supports UMD output but does not support exporting as a global namespace.

Syntax

  NamespaceExportDeclaration:
   exportasnamespaceIdentifierPath

Behavior

export var x = 0;
export function y() {}
export default {};
export as namespace My.Custom.Namespace;

// emits:

(function (global, factory) {
  if (typeof module === "object" && typeof module.exports === "object") {
    var v = factory(require, exports);
    if (v !== undefined) module.exports = v;
  }
  else if (typeof define === "function" && define.amd) {
    define(["require", "exports"], factory);
  }
  else {
    global.My = global.My || {};
    global.My.Custom = global.My.Custom || {};
    global.My.Custom.Namespace = global.My.Custom.Namespace || {};
    var exports = global.My.Custom.Namespace;
    factory(global.require, exports);
  }
})(this, function (require, exports) {
  "use strict";
  exports.__esModule = true;
  exports.x = 0;
  function y() { }
  exports.y = y;
  exports["default"] = {};
});

Note

  • This proposal basically follows Babel behavior.
  • Importing any module without a module loader will throw in this proposal. A further extension may use global namespaces as Babel does.
  • Babel overwrites on the existing namespace whereas this proposal extends the existing one, as TS namespace does.
  • Rollup has a special behavior where export default X works like CommonJS module.exports = X whereas this proposal does not.

Prior arts: Babel exactGlobals, Webpack multi part library, Rollup output.name option with namespace support

See also

#8436
#10907
#20990

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source files, tests, or entry points. Begin by comparing the proposed NamespaceExportDeclaration and UMD behavior with related issues #8436, #10907, and #20990; done requires an agreed design and implementation criteria for global namespace output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.