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