build-external-helpers.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function helpers() {
  7. const data = _interopRequireWildcard(require("@babel/helpers"));
  8. helpers = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _generator() {
  14. const data = _interopRequireDefault(require("@babel/generator"));
  15. _generator = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _template() {
  21. const data = _interopRequireDefault(require("@babel/template"));
  22. _template = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function t() {
  28. const data = _interopRequireWildcard(require("@babel/types"));
  29. t = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  35. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  36. const buildUmdWrapper = replacements => _template().default`
  37. (function (root, factory) {
  38. if (typeof define === "function" && define.amd) {
  39. define(AMD_ARGUMENTS, factory);
  40. } else if (typeof exports === "object") {
  41. factory(COMMON_ARGUMENTS);
  42. } else {
  43. factory(BROWSER_ARGUMENTS);
  44. }
  45. })(UMD_ROOT, function (FACTORY_PARAMETERS) {
  46. FACTORY_BODY
  47. });
  48. `(replacements);
  49. function buildGlobal(whitelist) {
  50. const namespace = t().identifier("babelHelpers");
  51. const body = [];
  52. const container = t().functionExpression(null, [t().identifier("global")], t().blockStatement(body));
  53. const tree = t().program([t().expressionStatement(t().callExpression(container, [t().conditionalExpression(t().binaryExpression("===", t().unaryExpression("typeof", t().identifier("global")), t().stringLiteral("undefined")), t().identifier("self"), t().identifier("global"))]))]);
  54. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().assignmentExpression("=", t().memberExpression(t().identifier("global"), namespace), t().objectExpression([])))]));
  55. buildHelpers(body, namespace, whitelist);
  56. return tree;
  57. }
  58. function buildModule(whitelist) {
  59. const body = [];
  60. const refs = buildHelpers(body, null, whitelist);
  61. body.unshift(t().exportNamedDeclaration(null, Object.keys(refs).map(name => {
  62. return t().exportSpecifier(t().cloneNode(refs[name]), t().identifier(name));
  63. })));
  64. return t().program(body, [], "module");
  65. }
  66. function buildUmd(whitelist) {
  67. const namespace = t().identifier("babelHelpers");
  68. const body = [];
  69. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().identifier("global"))]));
  70. buildHelpers(body, namespace, whitelist);
  71. return t().program([buildUmdWrapper({
  72. FACTORY_PARAMETERS: t().identifier("global"),
  73. BROWSER_ARGUMENTS: t().assignmentExpression("=", t().memberExpression(t().identifier("root"), namespace), t().objectExpression([])),
  74. COMMON_ARGUMENTS: t().identifier("exports"),
  75. AMD_ARGUMENTS: t().arrayExpression([t().stringLiteral("exports")]),
  76. FACTORY_BODY: body,
  77. UMD_ROOT: t().identifier("this")
  78. })]);
  79. }
  80. function buildVar(whitelist) {
  81. const namespace = t().identifier("babelHelpers");
  82. const body = [];
  83. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().objectExpression([]))]));
  84. const tree = t().program(body);
  85. buildHelpers(body, namespace, whitelist);
  86. body.push(t().expressionStatement(namespace));
  87. return tree;
  88. }
  89. function buildHelpers(body, namespace, whitelist) {
  90. const getHelperReference = name => {
  91. return namespace ? t().memberExpression(namespace, t().identifier(name)) : t().identifier(`_${name}`);
  92. };
  93. const refs = {};
  94. helpers().list.forEach(function (name) {
  95. if (whitelist && whitelist.indexOf(name) < 0) return;
  96. const ref = refs[name] = getHelperReference(name);
  97. const {
  98. nodes
  99. } = helpers().get(name, getHelperReference, ref);
  100. body.push(...nodes);
  101. });
  102. return refs;
  103. }
  104. function _default(whitelist, outputType = "global") {
  105. let tree;
  106. const build = {
  107. global: buildGlobal,
  108. module: buildModule,
  109. umd: buildUmd,
  110. var: buildVar
  111. }[outputType];
  112. if (build) {
  113. tree = build(whitelist);
  114. } else {
  115. throw new Error(`Unsupported output type ${outputType}`);
  116. }
  117. return (0, _generator().default)(tree).code;
  118. }