web 3d图形渲染器
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.

177 lines
6.7 KiB

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