You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.3 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _path = require("path");
  8. var _helperModuleTransforms = require("@babel/helper-module-transforms");
  9. var _core = require("@babel/core");
  10. const buildPrerequisiteAssignment = (0, _core.template)(`
  11. GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}
  12. `);
  13. const buildWrapper = (0, _core.template)(`
  14. (function (global, factory) {
  15. if (typeof define === "function" && define.amd) {
  16. define(MODULE_NAME, AMD_ARGUMENTS, factory);
  17. } else if (typeof exports !== "undefined") {
  18. factory(COMMONJS_ARGUMENTS);
  19. } else {
  20. var mod = { exports: {} };
  21. factory(BROWSER_ARGUMENTS);
  22. GLOBAL_TO_ASSIGN;
  23. }
  24. })(
  25. typeof globalThis !== "undefined" ? globalThis
  26. : typeof self !== "undefined" ? self
  27. : this,
  28. function(IMPORT_NAMES) {
  29. })
  30. `);
  31. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  32. api.assertVersion(7);
  33. const {
  34. globals,
  35. exactGlobals,
  36. loose,
  37. allowTopLevelThis,
  38. strict,
  39. strictMode,
  40. noInterop
  41. } = options;
  42. function buildBrowserInit(browserGlobals, exactGlobals, filename, moduleName) {
  43. const moduleNameOrBasename = moduleName ? moduleName.value : (0, _path.basename)(filename, (0, _path.extname)(filename));
  44. let globalToAssign = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(moduleNameOrBasename)));
  45. let initAssignments = [];
  46. if (exactGlobals) {
  47. const globalName = browserGlobals[moduleNameOrBasename];
  48. if (globalName) {
  49. initAssignments = [];
  50. const members = globalName.split(".");
  51. globalToAssign = members.slice(1).reduce((accum, curr) => {
  52. initAssignments.push(buildPrerequisiteAssignment({
  53. GLOBAL_REFERENCE: _core.types.cloneNode(accum)
  54. }));
  55. return _core.types.memberExpression(accum, _core.types.identifier(curr));
  56. }, _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(members[0])));
  57. }
  58. }
  59. initAssignments.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", globalToAssign, _core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")))));
  60. return initAssignments;
  61. }
  62. function buildBrowserArg(browserGlobals, exactGlobals, source) {
  63. let memberExpression;
  64. if (exactGlobals) {
  65. const globalRef = browserGlobals[source];
  66. if (globalRef) {
  67. memberExpression = globalRef.split(".").reduce((accum, curr) => _core.types.memberExpression(accum, _core.types.identifier(curr)), _core.types.identifier("global"));
  68. } else {
  69. memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(source)));
  70. }
  71. } else {
  72. const requireName = (0, _path.basename)(source, (0, _path.extname)(source));
  73. const globalName = browserGlobals[requireName] || requireName;
  74. memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(globalName)));
  75. }
  76. return memberExpression;
  77. }
  78. return {
  79. name: "transform-modules-umd",
  80. visitor: {
  81. Program: {
  82. exit(path) {
  83. if (!(0, _helperModuleTransforms.isModule)(path)) return;
  84. const browserGlobals = globals || {};
  85. let moduleName = this.getModuleName();
  86. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  87. const {
  88. meta,
  89. headers
  90. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  91. loose,
  92. strict,
  93. strictMode,
  94. allowTopLevelThis,
  95. noInterop
  96. });
  97. const amdArgs = [];
  98. const commonjsArgs = [];
  99. const browserArgs = [];
  100. const importNames = [];
  101. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  102. amdArgs.push(_core.types.stringLiteral("exports"));
  103. commonjsArgs.push(_core.types.identifier("exports"));
  104. browserArgs.push(_core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")));
  105. importNames.push(_core.types.identifier(meta.exportName));
  106. }
  107. for (const [source, metadata] of meta.source) {
  108. amdArgs.push(_core.types.stringLiteral(source));
  109. commonjsArgs.push(_core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]));
  110. browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source));
  111. importNames.push(_core.types.identifier(metadata.name));
  112. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  113. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  114. if (interop) {
  115. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  116. header.loc = meta.loc;
  117. headers.push(header);
  118. }
  119. }
  120. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, loose));
  121. }
  122. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  123. path.unshiftContainer("body", headers);
  124. const {
  125. body,
  126. directives
  127. } = path.node;
  128. path.node.directives = [];
  129. path.node.body = [];
  130. const umdWrapper = path.pushContainer("body", [buildWrapper({
  131. MODULE_NAME: moduleName,
  132. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  133. COMMONJS_ARGUMENTS: commonjsArgs,
  134. BROWSER_ARGUMENTS: browserArgs,
  135. IMPORT_NAMES: importNames,
  136. GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleName)
  137. })])[0];
  138. const umdFactory = umdWrapper.get("expression.arguments")[1].get("body");
  139. umdFactory.pushContainer("directives", directives);
  140. umdFactory.pushContainer("body", body);
  141. }
  142. }
  143. }
  144. };
  145. });
  146. exports.default = _default;