I have been using TypeScript at work where we use RequireJS as our AMD loader, and it's working quite well. I'd like to convert some of my personal JS libraries to TypeScript and am wondering - Is it possible using TypeScript's built in module system to export a global AND define using require/amd if the end user happens to be using it?
I'd like to avoid breaking compatibility with my existing users / forcing them into Require.
My Existing JS file is laid out as such that it defines a global AND if define is defined, defines TickerGraph:
var TickerGraph = (function () { …class code… });
if( typeof define == "function" ) {
define([], function() {
return TickerGraph;
});
}
In my TypeScript rewrite, I've laid it out with the module type defined as UMD as follows:
export = class TickerGraph { …class code… }
That outputs
(function (dependencies, 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(dependencies, factory);
}
})(["require", "exports"], function (require, exports) {
"use strict";
return (function () {
Which checks for define / exports but does NOT define a global…
I've experimented and eliminating the export all together exports the global, but of course no longer defines. Any help here would be greatly appreciated.