index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. var _api$assumption, _api$assumption2;
  33. api.assertVersion(7);
  34. const {
  35. globals,
  36. exactGlobals,
  37. allowTopLevelThis,
  38. strict,
  39. strictMode,
  40. noInterop,
  41. importInterop
  42. } = options;
  43. const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
  44. const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
  45. function buildBrowserInit(browserGlobals, exactGlobals, filename, moduleName) {
  46. const moduleNameOrBasename = moduleName ? moduleName.value : (0, _path.basename)(filename, (0, _path.extname)(filename));
  47. let globalToAssign = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(moduleNameOrBasename)));
  48. let initAssignments = [];
  49. if (exactGlobals) {
  50. const globalName = browserGlobals[moduleNameOrBasename];
  51. if (globalName) {
  52. initAssignments = [];
  53. const members = globalName.split(".");
  54. globalToAssign = members.slice(1).reduce((accum, curr) => {
  55. initAssignments.push(buildPrerequisiteAssignment({
  56. GLOBAL_REFERENCE: _core.types.cloneNode(accum)
  57. }));
  58. return _core.types.memberExpression(accum, _core.types.identifier(curr));
  59. }, _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(members[0])));
  60. }
  61. }
  62. initAssignments.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", globalToAssign, _core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")))));
  63. return initAssignments;
  64. }
  65. function buildBrowserArg(browserGlobals, exactGlobals, source) {
  66. let memberExpression;
  67. if (exactGlobals) {
  68. const globalRef = browserGlobals[source];
  69. if (globalRef) {
  70. memberExpression = globalRef.split(".").reduce((accum, curr) => _core.types.memberExpression(accum, _core.types.identifier(curr)), _core.types.identifier("global"));
  71. } else {
  72. memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(source)));
  73. }
  74. } else {
  75. const requireName = (0, _path.basename)(source, (0, _path.extname)(source));
  76. const globalName = browserGlobals[requireName] || requireName;
  77. memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(globalName)));
  78. }
  79. return memberExpression;
  80. }
  81. return {
  82. name: "transform-modules-umd",
  83. visitor: {
  84. Program: {
  85. exit(path) {
  86. if (!(0, _helperModuleTransforms.isModule)(path)) return;
  87. const browserGlobals = globals || {};
  88. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  89. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  90. const {
  91. meta,
  92. headers
  93. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  94. constantReexports,
  95. enumerableModuleMeta,
  96. strict,
  97. strictMode,
  98. allowTopLevelThis,
  99. noInterop,
  100. importInterop
  101. });
  102. const amdArgs = [];
  103. const commonjsArgs = [];
  104. const browserArgs = [];
  105. const importNames = [];
  106. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  107. amdArgs.push(_core.types.stringLiteral("exports"));
  108. commonjsArgs.push(_core.types.identifier("exports"));
  109. browserArgs.push(_core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")));
  110. importNames.push(_core.types.identifier(meta.exportName));
  111. }
  112. for (const [source, metadata] of meta.source) {
  113. amdArgs.push(_core.types.stringLiteral(source));
  114. commonjsArgs.push(_core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]));
  115. browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source));
  116. importNames.push(_core.types.identifier(metadata.name));
  117. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  118. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  119. if (interop) {
  120. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  121. header.loc = meta.loc;
  122. headers.push(header);
  123. }
  124. }
  125. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
  126. }
  127. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  128. path.unshiftContainer("body", headers);
  129. const {
  130. body,
  131. directives
  132. } = path.node;
  133. path.node.directives = [];
  134. path.node.body = [];
  135. const umdWrapper = path.pushContainer("body", [buildWrapper({
  136. MODULE_NAME: moduleName,
  137. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  138. COMMONJS_ARGUMENTS: commonjsArgs,
  139. BROWSER_ARGUMENTS: browserArgs,
  140. IMPORT_NAMES: importNames,
  141. GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleName)
  142. })])[0];
  143. const umdFactory = umdWrapper.get("expression.arguments")[1].get("body");
  144. umdFactory.pushContainer("directives", directives);
  145. umdFactory.pushContainer("body", body);
  146. }
  147. }
  148. }
  149. };
  150. });
  151. exports.default = _default;