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.

138 lines
4.8 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. 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; }
  9. const assert = require("assert");
  10. class ImportBuilder {
  11. constructor(importedSource, scope, hub) {
  12. this._statements = [];
  13. this._resultName = null;
  14. this._scope = null;
  15. this._hub = null;
  16. this._importedSource = void 0;
  17. this._scope = scope;
  18. this._hub = hub;
  19. this._importedSource = importedSource;
  20. }
  21. done() {
  22. return {
  23. statements: this._statements,
  24. resultName: this._resultName
  25. };
  26. }
  27. import() {
  28. this._statements.push(t.importDeclaration([], t.stringLiteral(this._importedSource)));
  29. return this;
  30. }
  31. require() {
  32. this._statements.push(t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(this._importedSource)])));
  33. return this;
  34. }
  35. namespace(name = "namespace") {
  36. const local = this._scope.generateUidIdentifier(name);
  37. const statement = this._statements[this._statements.length - 1];
  38. assert(statement.type === "ImportDeclaration");
  39. assert(statement.specifiers.length === 0);
  40. statement.specifiers = [t.importNamespaceSpecifier(local)];
  41. this._resultName = t.cloneNode(local);
  42. return this;
  43. }
  44. default(name) {
  45. name = this._scope.generateUidIdentifier(name);
  46. const statement = this._statements[this._statements.length - 1];
  47. assert(statement.type === "ImportDeclaration");
  48. assert(statement.specifiers.length === 0);
  49. statement.specifiers = [t.importDefaultSpecifier(name)];
  50. this._resultName = t.cloneNode(name);
  51. return this;
  52. }
  53. named(name, importName) {
  54. if (importName === "default") return this.default(name);
  55. name = this._scope.generateUidIdentifier(name);
  56. const statement = this._statements[this._statements.length - 1];
  57. assert(statement.type === "ImportDeclaration");
  58. assert(statement.specifiers.length === 0);
  59. statement.specifiers = [t.importSpecifier(name, t.identifier(importName))];
  60. this._resultName = t.cloneNode(name);
  61. return this;
  62. }
  63. var(name) {
  64. name = this._scope.generateUidIdentifier(name);
  65. let statement = this._statements[this._statements.length - 1];
  66. if (statement.type !== "ExpressionStatement") {
  67. assert(this._resultName);
  68. statement = t.expressionStatement(this._resultName);
  69. this._statements.push(statement);
  70. }
  71. this._statements[this._statements.length - 1] = t.variableDeclaration("var", [t.variableDeclarator(name, statement.expression)]);
  72. this._resultName = t.cloneNode(name);
  73. return this;
  74. }
  75. defaultInterop() {
  76. return this._interop(this._hub.addHelper("interopRequireDefault"));
  77. }
  78. wildcardInterop() {
  79. return this._interop(this._hub.addHelper("interopRequireWildcard"));
  80. }
  81. _interop(callee) {
  82. const statement = this._statements[this._statements.length - 1];
  83. if (statement.type === "ExpressionStatement") {
  84. statement.expression = t.callExpression(callee, [statement.expression]);
  85. } else if (statement.type === "VariableDeclaration") {
  86. assert(statement.declarations.length === 1);
  87. statement.declarations[0].init = t.callExpression(callee, [statement.declarations[0].init]);
  88. } else {
  89. assert.fail("Unexpected type.");
  90. }
  91. return this;
  92. }
  93. prop(name) {
  94. const statement = this._statements[this._statements.length - 1];
  95. if (statement.type === "ExpressionStatement") {
  96. statement.expression = t.memberExpression(statement.expression, t.identifier(name));
  97. } else if (statement.type === "VariableDeclaration") {
  98. assert(statement.declarations.length === 1);
  99. statement.declarations[0].init = t.memberExpression(statement.declarations[0].init, t.identifier(name));
  100. } else {
  101. assert.fail("Unexpected type:" + statement.type);
  102. }
  103. return this;
  104. }
  105. read(name) {
  106. this._resultName = t.memberExpression(this._resultName, t.identifier(name));
  107. }
  108. }
  109. exports.default = ImportBuilder;