build-external-helpers.js 4.5 KB

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