build-external-helpers.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. var _file = _interopRequireDefault(require("../transformation/file/file"));
  35. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  36. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  37. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  38. const buildUmdWrapper = replacements => (0, _template().default)`
  39. (function (root, factory) {
  40. if (typeof define === "function" && define.amd) {
  41. define(AMD_ARGUMENTS, factory);
  42. } else if (typeof exports === "object") {
  43. factory(COMMON_ARGUMENTS);
  44. } else {
  45. factory(BROWSER_ARGUMENTS);
  46. }
  47. })(UMD_ROOT, function (FACTORY_PARAMETERS) {
  48. FACTORY_BODY
  49. });
  50. `(replacements);
  51. function buildGlobal(allowlist) {
  52. const namespace = t().identifier("babelHelpers");
  53. const body = [];
  54. const container = t().functionExpression(null, [t().identifier("global")], t().blockStatement(body));
  55. 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"))]))]);
  56. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().assignmentExpression("=", t().memberExpression(t().identifier("global"), namespace), t().objectExpression([])))]));
  57. buildHelpers(body, namespace, allowlist);
  58. return tree;
  59. }
  60. function buildModule(allowlist) {
  61. const body = [];
  62. const refs = buildHelpers(body, null, allowlist);
  63. body.unshift(t().exportNamedDeclaration(null, Object.keys(refs).map(name => {
  64. return t().exportSpecifier(t().cloneNode(refs[name]), t().identifier(name));
  65. })));
  66. return t().program(body, [], "module");
  67. }
  68. function buildUmd(allowlist) {
  69. const namespace = t().identifier("babelHelpers");
  70. const body = [];
  71. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().identifier("global"))]));
  72. buildHelpers(body, namespace, allowlist);
  73. return t().program([buildUmdWrapper({
  74. FACTORY_PARAMETERS: t().identifier("global"),
  75. BROWSER_ARGUMENTS: t().assignmentExpression("=", t().memberExpression(t().identifier("root"), namespace), t().objectExpression([])),
  76. COMMON_ARGUMENTS: t().identifier("exports"),
  77. AMD_ARGUMENTS: t().arrayExpression([t().stringLiteral("exports")]),
  78. FACTORY_BODY: body,
  79. UMD_ROOT: t().identifier("this")
  80. })]);
  81. }
  82. function buildVar(allowlist) {
  83. const namespace = t().identifier("babelHelpers");
  84. const body = [];
  85. body.push(t().variableDeclaration("var", [t().variableDeclarator(namespace, t().objectExpression([]))]));
  86. const tree = t().program(body);
  87. buildHelpers(body, namespace, allowlist);
  88. body.push(t().expressionStatement(namespace));
  89. return tree;
  90. }
  91. function buildHelpers(body, namespace, allowlist) {
  92. const getHelperReference = name => {
  93. return namespace ? t().memberExpression(namespace, t().identifier(name)) : t().identifier(`_${name}`);
  94. };
  95. const refs = {};
  96. helpers().list.forEach(function (name) {
  97. if (allowlist && allowlist.indexOf(name) < 0) return;
  98. const ref = refs[name] = getHelperReference(name);
  99. helpers().ensure(name, _file.default);
  100. const {
  101. nodes
  102. } = helpers().get(name, getHelperReference, ref);
  103. body.push(...nodes);
  104. });
  105. return refs;
  106. }
  107. function _default(allowlist, outputType = "global") {
  108. let tree;
  109. const build = {
  110. global: buildGlobal,
  111. module: buildModule,
  112. umd: buildUmd,
  113. var: buildVar
  114. }[outputType];
  115. if (build) {
  116. tree = build(allowlist);
  117. } else {
  118. throw new Error(`Unsupported output type ${outputType}`);
  119. }
  120. return (0, _generator().default)(tree).code;
  121. }