index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. const moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  89. let moduleNameLiteral;
  90. if (moduleName) moduleNameLiteral = _core.types.stringLiteral(moduleName);
  91. const {
  92. meta,
  93. headers
  94. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  95. constantReexports,
  96. enumerableModuleMeta,
  97. strict,
  98. strictMode,
  99. allowTopLevelThis,
  100. noInterop,
  101. importInterop,
  102. filename: this.file.opts.filename
  103. });
  104. const amdArgs = [];
  105. const commonjsArgs = [];
  106. const browserArgs = [];
  107. const importNames = [];
  108. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  109. amdArgs.push(_core.types.stringLiteral("exports"));
  110. commonjsArgs.push(_core.types.identifier("exports"));
  111. browserArgs.push(_core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")));
  112. importNames.push(_core.types.identifier(meta.exportName));
  113. }
  114. for (const [source, metadata] of meta.source) {
  115. amdArgs.push(_core.types.stringLiteral(source));
  116. commonjsArgs.push(_core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]));
  117. browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source));
  118. importNames.push(_core.types.identifier(metadata.name));
  119. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  120. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  121. if (interop) {
  122. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  123. header.loc = meta.loc;
  124. headers.push(header);
  125. }
  126. }
  127. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
  128. }
  129. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  130. path.unshiftContainer("body", headers);
  131. const {
  132. body,
  133. directives
  134. } = path.node;
  135. path.node.directives = [];
  136. path.node.body = [];
  137. const umdWrapper = path.pushContainer("body", [buildWrapper({
  138. MODULE_NAME: moduleNameLiteral,
  139. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  140. COMMONJS_ARGUMENTS: commonjsArgs,
  141. BROWSER_ARGUMENTS: browserArgs,
  142. IMPORT_NAMES: importNames,
  143. GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleNameLiteral)
  144. })])[0];
  145. const umdFactory = umdWrapper.get("expression.arguments")[1].get("body");
  146. umdFactory.pushContainer("directives", directives);
  147. umdFactory.pushContainer("body", body);
  148. }
  149. }
  150. }
  151. };
  152. });
  153. exports.default = _default;