Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TypeScript bundled file header for an isomorphic single file node module.
// # TypeScript compilation and bundling
// $ tsc -t es5 -m amd --moduleResolution node --outFile bundle.js mylib.ts src/a.ts src/b.ts
// ^~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~
// # terminal
// $ node
// > require('./bundle.js')
// 1 2
// { default: 3 }
//
// # browser console
// 1 2
// > mylib
// { default: 3 }
//
// # sample product
// https://github.com/falsandtru/arch-stream
// $ npm i arch-stream
//
define = typeof define === 'function' && define.amd
? define
: (function () {
'use strict';
var name = 'mylib',
workspace = {};
return function define(m, rs, f) {
return !f
? void define(name, m, rs)
: void f.apply(this, rs.map(function (r) {
switch (r) {
case 'require': {
return typeof require === 'function' ? require : void 0;
}
case 'exports': {
return m.indexOf('/') === -1
? workspace[m] = typeof exports === 'undefined' ? self[m] = self[m] || {} : exports
: workspace[m] = workspace.hasOwnProperty(m) ? workspace[m] : {};
}
default: {
return r.slice(-2) === '.d' && {}
|| workspace.hasOwnProperty(r) && workspace[r]
|| typeof require === 'function' && require(r)
|| self[r];
}
}
}));
};
})();
// src/a.ts
export default 1;
// src/b.ts
import a from './a';
export var b = a + 1;
// mylib.ts
import a from './src/a';
import {b} from './src/b';
export default a + b;
console.log(a, b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment