named-exports.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (function () {
  2. /*
  3. * Named exports support for legacy module formats in SystemJS 2.0
  4. *
  5. * Note: This extra is deprecated as the behaviour is now the default in core,
  6. * so will be removed in the next major.
  7. */
  8. (function (global) {
  9. var systemJSPrototype = global.System.constructor.prototype;
  10. // hook System.register to know the last declaration binding
  11. var lastRegisterDeclare;
  12. var systemRegister = systemJSPrototype.register;
  13. systemJSPrototype.register = function (name, deps, declare) {
  14. lastRegisterDeclare = typeof name === 'string' ? declare : deps;
  15. systemRegister.apply(this, arguments);
  16. };
  17. var getRegister = systemJSPrototype.getRegister;
  18. systemJSPrototype.getRegister = function () {
  19. var register = getRegister.call(this);
  20. // if it is an actual System.register call, then its ESM
  21. // -> dont add named exports
  22. if (!register || register[1] === lastRegisterDeclare || register[1].length === 0)
  23. return register;
  24. // otherwise it was provided by a custom instantiator
  25. // -> extend the registration with named exports support
  26. var registerDeclare = register[1];
  27. register[1] = function (_export, _context) {
  28. // hook the _export function to note the default export
  29. var defaultExport, hasDefaultExport = false;
  30. var declaration = registerDeclare.call(this, function (name, value) {
  31. if (typeof name === 'object' && name && name.__useDefault)
  32. defaultExport = name.default, hasDefaultExport = true;
  33. else if (name === 'default')
  34. defaultExport = value;
  35. else if (name === '__useDefault')
  36. hasDefaultExport = true;
  37. _export(name, value);
  38. }, _context);
  39. // hook the execute function
  40. var execute = declaration.execute;
  41. if (execute)
  42. declaration.execute = function () {
  43. execute.call(this);
  44. // do a bulk export of the default export object
  45. // to export all its names as named exports
  46. if (hasDefaultExport)
  47. for (var exportName in defaultExport) {
  48. if (
  49. Object.prototype.hasOwnProperty.call(defaultExport, exportName) // Check if epoxrt name is not inherited, safe for Object.create(null)
  50. && exportName !== 'default' // default is not a named export
  51. ) {
  52. _export(exportName, defaultExport[exportName]);
  53. }
  54. }
  55. };
  56. return declaration;
  57. };
  58. return register;
  59. };
  60. })(typeof self !== 'undefined' ? self : global);
  61. })();