title: UMD syntax with fallback global
body: <h2>My Setup</h2>
  
  <p>I'm compiling my <code>.ts</code> module 'FooModule' into an <a href="https://github.com/Microsoft/TypeScript/wiki/What&#39;s-new-in-TypeScript#support-for-umd-and-system-module-output" rel="nofollow">UMD module</a> in Visual Studio 2015. I would like to extend this UMD syntax, so that the module will be injected into the global object as fallback when no module loading system is present, and I would like to automate this task as part of the build-process.</p>
  
  <hr>
  
  <h2>The UMD Syntax</h2>
  
  <p>When compiling a TypeScript module with the UMD syntax (Universal Module Definition syntax), modules that are compatible with both RequireJS and NodeJS are emitted:</p>
  
  <p><strong>foo-module.ts</strong></p>
  
  <pre><code>export class Foo {
      // ...
  }
  </code></pre>
  
  <p>... is compiled into:</p>
  
  <p><strong>foo-module.js</strong></p>
  
  <pre><code>(function (factory) {
      if (typeof module === 'object' &amp;&amp; typeof module.exports === 'object') {
          var v = factory(require, exports); if (v !== undefined) module.exports = v;
      } else if (typeof define === 'function' &amp;&amp; define.amd) {
          define(["require", "exports"], factory);
      }
  })(function (require, exports) {
      "use strict";
      var Foo = (function () {
          function Foo() {
          }
          return Foo;
      }());
      exports.Foo = Foo;
  });
  </code></pre>
  
  <hr>
  
  <h2>Fallback to Global Inject</h2>
  
  <p>However, as part of the <em>Universal</em> part of UMD, I would like my module to be usable without any module loading system as well. <strong><em>My module has no dependencies.</em></strong></p>
  
  <p>This is usually done by adding a third case as a fallback for when neither the RequireJS nor the NodeJS module system are present, which will effectively inject into the global object. For <code>foo-module</code>, this would look like:</p>
  
  <pre><code>(function (global, factory) {
      if (typeof module === 'object' &amp;&amp; typeof module.exports === 'object') {
          var v = factory(require, exports); if (v !== undefined) module.exports = v;
      } else if (typeof define === 'function' &amp;&amp; define.amd) {
          define(["require", "exports"], factory);
      } else {
          // *****************************
          factory(null, global.FooModule || (global.FooModule = {})); // &lt;- This part.
          // *****************************
      }
  })(this, function (require, exports) {
      "use strict";
      var Foo = (function () {
          function Foo() {
          }
          return Foo;
      }());
  
      // In browsers, this means 'window.FooModule.Foo = Foo'.
      exports.Foo = Foo;
  });
  </code></pre>
  
  <p>I believe this would suffice in all cases when there are no external dependencies (assuming no two modules with the same name have been loaded this way).</p>
  
  <p>I understand I can just rewrite manually after each build, but <strong>how could I automate this task (or something with identical results) as part of a build-process in Visual Studio (2015)?</strong></p>