import-builder.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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._importedSource = void 0;
  27. this._scope = scope;
  28. this._hub = hub;
  29. this._importedSource = importedSource;
  30. }
  31. done() {
  32. return {
  33. statements: this._statements,
  34. resultName: this._resultName
  35. };
  36. }
  37. import() {
  38. this._statements.push(importDeclaration([], stringLiteral(this._importedSource)));
  39. return this;
  40. }
  41. require() {
  42. this._statements.push(expressionStatement(callExpression(identifier("require"), [stringLiteral(this._importedSource)])));
  43. return this;
  44. }
  45. namespace(name = "namespace") {
  46. const local = this._scope.generateUidIdentifier(name);
  47. const statement = this._statements[this._statements.length - 1];
  48. _assert(statement.type === "ImportDeclaration");
  49. _assert(statement.specifiers.length === 0);
  50. statement.specifiers = [importNamespaceSpecifier(local)];
  51. this._resultName = cloneNode(local);
  52. return this;
  53. }
  54. default(name) {
  55. const id = 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 = [importDefaultSpecifier(id)];
  60. this._resultName = cloneNode(id);
  61. return this;
  62. }
  63. named(name, importName) {
  64. if (importName === "default") return this.default(name);
  65. const id = this._scope.generateUidIdentifier(name);
  66. const statement = this._statements[this._statements.length - 1];
  67. _assert(statement.type === "ImportDeclaration");
  68. _assert(statement.specifiers.length === 0);
  69. statement.specifiers = [importSpecifier(id, identifier(importName))];
  70. this._resultName = cloneNode(id);
  71. return this;
  72. }
  73. var(name) {
  74. const id = this._scope.generateUidIdentifier(name);
  75. let statement = this._statements[this._statements.length - 1];
  76. if (statement.type !== "ExpressionStatement") {
  77. _assert(this._resultName);
  78. statement = expressionStatement(this._resultName);
  79. this._statements.push(statement);
  80. }
  81. this._statements[this._statements.length - 1] = variableDeclaration("var", [variableDeclarator(id, statement.expression)]);
  82. this._resultName = cloneNode(id);
  83. return this;
  84. }
  85. defaultInterop() {
  86. return this._interop(this._hub.addHelper("interopRequireDefault"));
  87. }
  88. wildcardInterop() {
  89. return this._interop(this._hub.addHelper("interopRequireWildcard"));
  90. }
  91. _interop(callee) {
  92. const statement = this._statements[this._statements.length - 1];
  93. if (statement.type === "ExpressionStatement") {
  94. statement.expression = callExpression(callee, [statement.expression]);
  95. } else if (statement.type === "VariableDeclaration") {
  96. _assert(statement.declarations.length === 1);
  97. statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]);
  98. } else {
  99. _assert.fail("Unexpected type.");
  100. }
  101. return this;
  102. }
  103. prop(name) {
  104. const statement = this._statements[this._statements.length - 1];
  105. if (statement.type === "ExpressionStatement") {
  106. statement.expression = memberExpression(statement.expression, identifier(name));
  107. } else if (statement.type === "VariableDeclaration") {
  108. _assert(statement.declarations.length === 1);
  109. statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name));
  110. } else {
  111. _assert.fail("Unexpected type:" + statement.type);
  112. }
  113. return this;
  114. }
  115. read(name) {
  116. this._resultName = memberExpression(this._resultName, identifier(name));
  117. }
  118. }
  119. exports.default = ImportBuilder;
  120. //# sourceMappingURL=import-builder.js.map