import-builder.js 4.1 KB

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